Welcome to the Python Inheritance and Polymorphism Python Interview Questions! Inheritance is a key concept in object-oriented programming that allows a new class to inherit attributes and methods from an existing class. Polymorphism, on the other hand, refers to the ability of a method to behave differently based on the object it is called on. These questions will test your understanding of these important concepts in Python, including how inheritance works, how to create subclasses, and how polymorphism enhances code flexibility. Each question is multiple-choice, with only one correct answer. Take your time to carefully read each question and choose the best option. Let’s explore Python inheritance and polymorphism together!
a) It allows a class to inherit attributes and methods from another class
b) It allows a class to create multiple instances
c) It allows a class to override its parent class methods
d) It allows a class to hide its attributes and methods
Answer: a
Explanation: Inheritance in Python allows a class (subclass) to inherit attributes and methods from another class (superclass).
a) super
b) base
c) derive
d) extends
Answer: d
Explanation: The extends
keyword is used to indicate that a class is inheriting from another class in Python.
a) It allows a class to have multiple constructors
b) It allows a class to have multiple methods with the same name but different parameters
c) It allows a class to have multiple instances
d) It allows a class to have multiple inheritance
Answer: b
Explanation: Polymorphism in Python allows a class to have multiple methods with the same name but different parameters, which can be invoked based on the input arguments.
a) It allows a subclass to have its own method with the same name as in the superclass
b) It allows a subclass to inherit all methods from the superclass
c) It allows a superclass to have methods with different names
d) It allows a subclass to have its own constructor
Answer: a
Explanation: Method overriding is the ability of a subclass to provide a specific implementation of a method that is already defined in its superclass.
a) The init() method is used to initialize the object’s state.
b) The init() method is used to define class variables.
c) The init() method is used to define instance methods.
d) The init() method is used to call another method.
Answer: a
Explanation: The init() method in Python is a special method that is automatically called when a new object of a class is instantiated. It is used to initialize the object’s state.
class Animal:
def sound(self):
print("Animal sound")
class Dog(Animal):
def sound(self):
print("Bark")
class Cat(Animal):
def sound(self):
print("Meow")
a = Animal()
d = Dog()
c = Cat()
a.sound()
d.sound()
c.sound()
What will be the output?
a) Animal sound Bark Meow
b) Bark Meow Animal sound
c) Animal sound Animal sound Animal sound
d) Bark Bark Bark
Answer: a
Explanation: Each object calls its respective sound
method. a.sound()
calls the sound
method of the Animal
class, d.sound()
calls the sound
method of the Dog
class, and c.sound()
calls the sound
method of the Cat
class.
class Shape:
def area(self):
pass
class Square(Shape):
def __init__(self, side):
self.side = side
def area(self):
return self.side * self.side
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * self.radius * self.radius
s = Square(5)
c = Circle(2)
print("Area of Square:", s.area())
print("Area of Circle:", c.area())
a) Area of Square: 25 Area of Circle: 12.56
b) Area of Square: 10 Area of Circle: 6.28
c) Area of Square: 20 Area of Circle: 10
d) Area of Square: 12.56 Area of Circle: 25
Answer: a
Explanation: The Square
class has an area
method that calculates the area of a square, and the Circle
class has an area
method that calculates the area of a circle. When objects s
and c
are created and their area
methods are called, the output will be Area of Square: 25 and Area of Circle: 12.56.
class A:
def show(self):
print("A")
class B(A):
def show(self):
print("B")
class C(A):
def show(self):
print("C")
class D(B, C):
pass
obj = D()
obj.show()
a) A
b) B
c) C
d) None of the above
Answer: b
Explanation: Since D
does not have its own show
method, it inherits from B
which is the first superclass listed in the class definition. Therefore, obj.show()
will print “B”.
class A:
def show(self):
print("A")
class B(A):
pass
class C(A):
def show(self):
print("C")
class D(B, C):
pass
obj = D()
obj.show()
a) A
b) C
c) Error: Cannot determine which show method to call
d) None of the above
Answer: b
Explanation: The class D
inherits from B
and C
. Since C
is the first superclass listed in D
, the show
method of C
is invoked. Therefore, obj.show()
will print “C”.
class A:
def __init__(self):
print("A")
class B(A):
def __init__(self):
print("B")
class C(A):
def __init__(self):
print("C")
class D(B, C):
pass
obj = D()
a) A
b) B
c) C
d) None of the above
Answer: b
Explanation: When an object of class D
is created, the __init__
of the first superclass in the MRO is called, which is B
. Therefore, Init B
will be printed.
class A:
def __init__(self):
print("A")
class B(A):
def __init__(self):
print("B")
super().__init__()
class C(A):
def __init__(self):
print("C")
super().__init__()
class D(B, C):
def __init__(self):
print("D")
super().__init__()
obj = D()
a) A B C D
b) D B C A
c) D B A
d) D C A
Answer: b
Explanation: When an object of class D
is created, D
‘s __init__
method explicitly calls B
‘s __init__
method, which in turn calls A
‘s __init__
method. Then, D
‘s __init__
method also explicitly calls C
‘s __init__
method, which calls A
‘s __init__
method again. So the order of output is D, B, C, A.
class A:
def __init__(self):
print("A")
class B(A):
def __init__(self):
print("B")
super().__init__()
class C(A):
def __init__(self):
print("C")
super().__init__()
class D(B, C):
pass
obj = D()
a) A B C D
b) D B C A
c) D B A
d) D C A
Answer: b
Explanation: When an object of class D
is created, D
‘s __init__
method explicitly calls B
‘s __init__
method, which in turn calls A
‘s __init__
method. Then, D
‘s __init__
method also explicitly calls C
‘s __init__
method, which calls A
‘s __init__
method again. So the order of output is D, B, C, A.
class A:
def __init__(self):
print("A")
class B(A):
def __init__(self):
print("B")
super().__init__()
class C(A):
def __init__(self):
print("C")
super().__init__()
class D(B, C):
def __init__(self):
print("D")
B.__init__(self)
C.__init__(self)
obj = D()
a) A B C D
b) D B C A
c) D B A
d) D C A
Answer: b
Explanation: In this case, D
‘s __init__
method explicitly calls B
‘s __init__
method, which in turn calls A
‘s __init__
method. Then, D
‘s __init__
method also explicitly calls C
‘s __init__
method, which calls A
‘s __init__
method again. So the order of output is D, B, C, A.
class A:
def __init__(self):
print("A")
class B(A):
def __init__(self):
print("B")
A.__init__(self)
class C(A):
def __init__(self):
print("C")
A.__init__(self)
class D(B, C):
def __init__(self):
print("D")
B.__init__(self)
C.__init__(self)
obj = D()
a) A B C D
b) D B C A
c) D B A
d) D C A
Answer: b
Explanation: In this case, D
‘s __init__
method explicitly calls B
‘s __init__
method, which calls A
‘s __init__
method. Then, D
‘s __init__
method also explicitly calls C
‘s __init__
method, which calls A
‘s __init__
method again. So the order of output is D, B, C, A.
a) The “pass” statement is used to indicate that the method is not implemented and will do nothing.
b) The “pass” statement is used to exit a loop.
c) The “pass” statement is used to define a class.
d) The “pass” statement is used to call another method.
Answer: a
Explanation: The “pass” statement in Python is used as a placeholder when a statement is required syntactically but you do not want any command or code to execute.
class A:
def __init__(self):
print("A")
class B(A):
def __init__(self):
print("B")
super().__init__()
class C(A):
def __init__(self):
print("C")
super().__init__()
class D(B, C):
pass
obj = D()
a) A B C D
b) D B C A
c) D B A
d) D C A
Answer: b
Explanation: When an object of class D
is created, D
‘s __init__
method calls B
‘s __init__
method, which in turn calls A
‘s __init__
method. Then, D
‘s __init__
method calls C
‘s __init__
method, which also calls A
‘s __init__
method. So the order of output is D, B, C, A.
a) An abstract class is a class that cannot be instantiated and is used as a blueprint for other classes.
b) An abstract class is a class that is instantiated multiple times.
c) An abstract class is a class with only static methods.
d) An abstract class is a class with only private methods.
Answer: a
Explanation: An abstract class in Python is a class that cannot be instantiated and is meant to be used as a base class for other classes. It may contain abstract methods that must be implemented by its subclasses.
class A:
def __init__(self):
print("A")
class B(A):
def __init__(self):
print("B")
super().__init__()
class C(A):
def __init__(self):
print("C")
super().__init__()
class D(B, C):
pass
class E(D):
pass
obj = E()
a) A B C D
b) D B C A
c) D B A
d) D C A
Answer: b
Explanation: When an object of class E
is created, E
‘s __init__
method calls D
‘s __init__
method, which in turn calls B
‘s __init__
method, then C
‘s __init__
method, and finally A
‘s __init__
method. So the order of output is D, B, C, A.
a) By using the “super()” function
b) By using the “new” keyword
c) By using the “staticmethod” decorator
d) By creating a subclass and implementing the abstract methods
Answer: d
Explanation: To create an object of an abstract class in Python, you need to create a subclass that inherits from the abstract class and implement all the abstract methods defined in the abstract class.
class A:
def show(self):
print("A")
class B(A):
def show(self):
print("B")
class C(A):
def show(self):
print("C")
class D(B, C):
pass
obj = D()
obj.show()
a) A
b) B
c) C
d) None of the above
Answer: b
Explanation: Since D
does not have its own show
method, it inherits from B
which is the first superclass listed in the class definition. Therefore, obj.show()
will print “B”.
class A:
def show(self):
print("A")
class B(A):
pass
class C(A):
def show(self):
print("C")
class D(B, C):
pass
obj = D()
obj.show()
a) A
b) C
c) Error: Cannot determine which show method to call
d) None of the above
Answer: b
Explanation: The class D
inherits from B
and C
. Since C
is the first superclass listed in D
, the show
method of C
is invoked. Therefore, obj.show()
will print “C”.
a) It allows for code reusability and reduces redundancy.
b) It makes the code more complex and harder to maintain.
c) It enforces encapsulation of data.
d) It prevents the creation of new classes.
Answer: a
Explanation: Inheritance promotes code reusability by allowing new classes to use existing code from parent classes.
a) Polymorphism allows methods to be implemented in different ways in child classes.
b) Polymorphism is used to make all methods private.
c) Polymorphism is a way to prevent inheritance.
d) Polymorphism is used to restrict access to attributes.
Answer: a
Explanation: Polymorphism allows methods in different classes to have the same name but behave differently.
a) Method overloading allows a class to have multiple methods with the same name but different signatures, while method overriding allows a child class to provide a specific implementation of a method that is already defined in its parent class.
b) Method overloading is not supported in Python, only method overriding.
c) Method overloading and method overriding are the same thing.
d) Method overriding allows a class to have multiple methods with the same name but different signatures, while method overloading allows a child class to provide a specific implementation of a method that is already defined in its parent class.
Answer: a
Explanation: Method overloading involves having multiple methods with the same name but different parameters within a class, while method overriding involves redefining a method in a subclass that is already defined in a parent class.
a) MRO defines the order in which methods are resolved or looked up in the class hierarchy.
b) MRO is a Python built-in function to define methods.
c) MRO is the process of defining methods inside a class.
d) MRO is not relevant in Python.
Answer: a
Explanation: MRO defines the order in which methods are resolved or looked up in the class hierarchy, especially in cases of multiple inheritance.
a) Python allows a class to inherit from multiple parent classes.
b) Python does not support multiple inheritance.
c) Python only allows a class to inherit from one parent class.
d) Python randomly selects a parent class when multiple inheritance is used.
Answer: a
Explanation: Python supports multiple inheritance by allowing a class to inherit attributes and methods from multiple parent classes.
a) super() allows a child class to call methods of the parent class without explicitly naming them.
b) super() prevents inheritance.
c) super() makes all methods private.
d) super() enforces encapsulation of data.
Answer: a
Explanation: The super()
function allows a child class to call methods of the parent class without explicitly naming the parent class, promoting code reusability.
a) Diamond problem occurs when two child classes inherit from the same parent class.
b) Diamond problem occurs when a child class inherits from multiple parent classes, and both parent classes have a common ancestor.
c) Diamond problem does not exist in Python.
d) Diamond problem occurs when a class has a diamond shape in its class hierarchy.
Answer: b
Explanation: The diamond problem occurs in multiple inheritance when a class inherits from two classes that have a common ancestor, creating an ambiguous situation for the compiler to resolve.
del
keyword do in Python? a) Deletes a method from a class
b) Deletes an instance of a class
c) Deletes an attribute from an instance of a class
d) Deletes a class from memory
Answer: c
Explanation: The del
keyword in Python is used to delete an attribute from an instance of a class.
a) Class variables are defined within methods and can be accessed only by class methods, while instance variables are defined outside methods and can be accessed by instance methods.
b) Class variables are shared among all instances of a class, while each instance of a class has its own copy of instance variables.
c) Class variables can only be accessed using the class name, while instance variables can only be accessed using the instance name.
d) Class variables are immutable, while instance variables can be modified.
Answer: b
Explanation: Class variables are shared among all instances of a class, meaning they are the same for every instance. Instance variables, on the other hand, are unique to each instance of a class.
@property
decorator in Python? a) It allows a method to be called without parentheses.
b) It allows a method to be overridden in a subclass.
c) It allows a method to be accessed as an attribute.
d) It allows a method to be a class method.
Answer: c
Explanation: The @property
decorator in Python allows a method to be accessed like an attribute, providing a cleaner syntax for getters and setters.
a) A function that takes an iterable and returns an iterator
b) A method that modifies the behavior of an instance attribute
c) An object attribute with “binding behavior”, one whose attribute access has been overridden by methods in the descriptor protocol
d) A special method used for creating class objects
Answer: c
Explanation: A descriptor in Python is an object attribute with “binding behavior”, meaning its attribute access has been overridden by methods in the descriptor protocol. Descriptors are used to create properties and other managed attributes.
__new__
method do in Python classes? a) Initializes the object’s state
b) Creates a new object instance
c) Deletes the object
d) Calls the parent class constructor
Answer: b
Explanation: The __new__
method in Python classes is responsible for creating a new object instance. It is called before __init__
and is used to create and return a new object.
__slots__
attribute in Python classes? a) It specifies the attributes that a class instance can have
b) It restricts the number of instances that can be created from a class
c) It defines the methods available in a class
d) It specifies the base classes of a class
Answer: a
Explanation: The __slots__
attribute in Python classes specifies the attributes that a class instance can have. It is used to optimize memory usage and restrict the attributes of instances.
Congratulations on completing the Python Inheritance and Polymorphism MCQs! Inheritance and polymorphism are powerful features in object-oriented programming that promote code reusability and flexibility. By mastering these concepts, you gain the ability to create class hierarchies, specialize classes, and create more dynamic and adaptable code. Keep practicing and experimenting with Python’s inheritance and polymorphism functionalities to become proficient in building robust and extensible applications. If you have any questions or want to delve deeper into any topic, don’t hesitate to continue your learning journey. Happy coding!
You can also enroll in our free Python Course Today!
Read our more articles related to MCQs in Python: