30+ MCQs on Python Inheritance and Polymorphism

Ayushi Trivedi Last Updated : 06 Mar, 2024
11 min read

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!

Python Inheritance and Polymorphism

30+ Python Interview Questions on Python Inheritance and Polymorphism

Q1. What is inheritance in Python?

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).

Q2. Which keyword is used to inherit a class in Python?

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.

Q3. What is the concept of polymorphism 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.

Q4. What is method overriding in Python?

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.

Q5. What is the purpose of the init() method in Python classes?

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.

Q6. Consider the following code:

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.

Q7. What is the output of the following code?

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.

Q8. What is the output of the following code?

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”.

Q9. What is the output of the following code?

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”.

Q10. What is the output of the following code?

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.

Q11. What is the output of the following code?

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.

Q12. What is the output of the following code?

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.

Q13. What is the output of the following code?

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.

Q14. What is the output of the following code?

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.

Q15. What is the purpose of the “pass” statement in Python?

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.

Q16. What is the output of the following code?

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.

Q17. What is an abstract class in Python?

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.

Q18. What is the output of the following code?

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.

Q19. How can you create an object of an abstract class in Python?

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.

Q20. What is the output of the following code?

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”.

Q21. What is the output of the following code?

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”.

Q22. What is the main advantage of using inheritance in Python?

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.

Q23. What is the purpose of polymorphism in Python?

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.

Q24. What is the difference between method overloading and method overriding in Python?

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.

Q25. What is method resolution order (MRO) in Python?

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.

Q26. How does Python support 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.

Q27. What is the main advantage of using super() in Python?

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.

Q28. Which of the following statements about diamond problem in Python is correct?

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.

Q29. What does the 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.

Q30. What is the difference between class variables and instance variables in Python?

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.

Q31. What is the purpose of the @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.

Q32. What is a descriptor in Python?

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.

Q33. What does the __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.

Q34. What is the purpose of the __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:

My name is Ayushi Trivedi. I am a B. Tech graduate. I have 3 years of experience working as an educator and content editor. I have worked with various python libraries, like numpy, pandas, seaborn, matplotlib, scikit, imblearn, linear regression and many more. I am also an author. My first book named #turning25 has been published and is available on amazon and flipkart. Here, I am technical content editor at Analytics Vidhya. I feel proud and happy to be AVian. I have a great team to work with. I love building the bridge between the technology and the learner.

Responses From Readers

Clear

Congratulations, You Did It!
Well Done on Completing Your Learning Journey. Stay curious and keep exploring!

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details