Prepare these questions and answers from OOPs

Santhanalakshmi Santhanalakshm Published on 15 July 2020

Question 1:

How is a static method different from an instance method?

Answer:

Static method has to be defined outside a class. It can be called without an object.

Instance method is defined within a class and has to be invoked on an object.

Question 2:

Explain Data Hiding with respect to OOP.

Answer:

Data hiding can be defined as the mechanism of hiding the data of a class from the outside world or to be precise, from other classes. Data hiding is achieved by making the members of the class private. Access to private members is restricted and is only available to the member functions of the same class. However the public part of the object is accessible outside the class.

Question 3:

What is Object Oriented Programming? List some of its advantages.

Answer:

OOP allows decomposition of a problem into a number of entities called objects and then builds data and functions around these objects. Advantages:

  1. Simplicity
  2. Modifiability
  3. Extensibility and Maintainability
  4. Reusability
  5. Security

Question 4:

Differentiate between an object and a class.

Answer:

A class is a collection of objects of similar type.

For example, mango, apple and orange are members of the class fruit. Classes are user-defined data types and behave like the built-in types of a programming language.


The syntax used to create an object is not different than the syntax used to create an integer object in C. If fruit has been defined as a class, then the statement fruit mango; will create an object mango belonging to the class fruit.

Question 5:

Explain polymorphism with an example.

Answer:

Polymorphism is the ability for a message or data to be processed in more than one form. An operation may exhibit different behaviors in different instances. For example, consider the operation of addition of two numbers, the operation will generate a sum. If the operands are strings, then the operation would produce a third string by concatenation.

Question 6:

List three features that make an important charac-teristic of OOP.

Answer:

  1. Capability to express closeness with the real- world models.
  2. Reusability-allows addition of new features to an existing one.
  3. Transitivity-changes in one class get automatically reflected across.

Question 7:

How do we implement abstract method in python? Give an example for the same.

Answer:

Abstract Method can be used to enable parent class method execution.

Class Shape (object):

def findArea (self): pass

Class Square (Shape):

def init (self, side):

self, side = side def findArea self.side*self.side

Question 8:

List few disadvantages of OOP.

Answer:

  1. Classes tend to be overly generalized.
  2. Relationship among classes might become artificial.
  3. Program design is tricky and complicated.
  4. More skills and thinking in terms of objects is required.

Question 9:

Explain Function overloading with an example.

Answer:

When several function declarations are specified for a single function name in the same scope, the function is said to be overloaded. In other languages, the same function name can be used to define multiple functions with different number and type of arguments, def test(): #function 1 print “hello”

def test(a, b): #function 2

return a+b

def test(a, b, c): #function 3

return a+b+c 2

Question 10:

What is inheritance? Explain with an example.

Answer:

Inheritance is the capability of one class to acquire the properties or capabilities of another class. For example, the bird ‘Parrot’ is a part of the class ‘Flying Bird’ which is again a part of the class ‘Bird’.