Top 30 Python Inheritance Quiz Questions for Practice
Welcome to this challenging Python inheritance quiz! In this quiz, we have compiled the top 30 multiple-choice questions based on Python inheritance, MRO, super(), diamond problem, and method overriding. These 30 MCQ quizzes will test your knowledge from basic to advanced levels, as well as level up your understanding and knowledge in inheritance.
Whether youβre a beginner or an experienced Python programmer, this quiz is perfect for interviews preparation, competitive exams, and enhancing your knowledge in inheritance. So, are you ready to score 30/30? Letβs see, how well do you really understand inheritance concepts in Python? Letβs begin with question number 1.π
1. What is inheritance in Python?
A. A way to create multiple constructors.
B. A mechanism to reuse code from a parent class
C. A mechanism where a new class inherits attributes and methods from an existing class.
D. Both A and B
d
Inheritance is a process by which a subclass acquires all the properties (i.e. attributes) and behaviors (i.e. methods) of the superclass.
2. Which of the following syntax correctly defines inheritance in Python?
A. class Child inherits Parent:
B. class Parent(Child):
C. class Child(Parent)
D. class Child(Parent):
d
The correct syntax to create subclass in Python is class Child(Parent):, where Child inherits from Parent.
3. Which type of inheritance is represented by the below code?
class A: pass class B(A): pass class C(B): pass
A. Single inheritance
B. Multiple inheritance
C. Multilevel inheritance
D. Hierarchical inheritance
c
This is multilevel inheritance where class C inherits from B, which inherits from A.
4. What gets inherited by a child class from its parent in Python?
A. Only public methods
B. All attributes and methods except private ones (starting with __)
C. Only class variables
D. Only instance methods
b
In Python, child classes inherit all attributes and methods from their parent class except those whose names start with double underscores (“__”) because they are considered as private members.
5. What will be the output of the following code?
class A:
def show(self):
print("A")
class B(A):
def show(self):
print("B")
obj = B()
obj.show()A. A
B. B
C. A B
D. Error
b
Since class B overrides the show() method of A, calling obj.show() will execute the show() method of class B.
6. Can a class inherit from multiple classes in Python?
A. No, Python does not allow this.
B. Yes, Python supports multiple inheritance.
C. Only if both parent classes have the same methods.
D. None of the above
b
Python supports multiple inheritance, meaning a class can inherit from more than one parent class.
7. Which method is automatically called when an object is created?
A. __del__()
B. __new__()
C. __init__()
D. __call__()
c
The __init__() method is the constructor in Python. It is automatically invoked when a new object is instantiated.
8. What will be the output of the below code?
class A:
def __init__(self):
print("A init")
class B(A):
def __init__(self):
super().__init__()
print("B init")
B()A. A init
B. B init A init
C. A init B init
D. Both A and B
c
super().__init__() calls the parent class constructor, so both parent and child class constructors are executed in order.
9. What will be the output of this multiple inheritance scenario in Python?
class A:
def method(self):
print("A")
class B:
def method(self):
print("B")
class C(A, B):
pass
obj = C()
obj.method()A. A
B. B
C. A B
D. Error: Python does not support multiple inheritance.
a
In multiple inheritance, Python uses Method Resolution Order (MRO), so class A’s method is called first.
10. What is the MRO (Method Resolution Order) in Python?
A. The order in which methods are executed.
B. The order in which Python searches for methods and attributes in a class hierarchy.
C. The order in which constructors are executed.
D. Both B and C
b
Method Resolution Order (MRO) in Python defines the order in which Python searches for methods and attributes in a class hierarchy, especially in the case of multiple inheritance. When a method or attribute is called on an object, Python follows the MRO to determine which class’s implementation should be used.
11. What is the expected output of the following Python code?
class A:
def greet(self):
print("Hello from A")
class B(A):
def greet(self):
print("Hello from B")
class C(A):
def greet(self):
print("Hello from C")
class D(B, C):
pass
d = D()
d.greet()A. Hello from A
B. Hello from B
C. Hello from C
D. Both B and C
b
Python follows MRO (left to right). Since class D inherits class B before class C, and class B has greet() method, it is executed.
12. Which function in Python is used to view the method resolution order (MRO) of a class?
A. resolve()
B. mro()
C. __mro__()
D. class.mro()
b
The mro() method and the __mro__ attribute are used to view the method resolution order (MRO) of a class in Python. ClassName.mro() returns a list of classes in MRO order, while ClassName.__mro__ returns a tuple of classes in MRO order. Both provide the same information about the order in which Python searches for methods within a class hierarchy.
13. What will be the output of following Python code if no error found?
class A:
pass
class B(A):
pass
class C(A):
pass
class D(B, C):
pass
print(D.__mro__)A. (D, B, A, C, object)
B. (D, A, B, C, object)
C. (D, C, B, A, object)
D. (D, B, C, A, object)
d
MRO for class D is calculated as D β B β C β A β object.
14. What happens when both parent and child classes have __init__, but the child class does not call super().__init__()?
A. Both parent and child class __init__() methods execute automatically.
B. Python raises an error because super() is missing.
C. The parent class’s __init__() method executes first, then the child’s.
D. Only the child class’s __init__() method executes.
d
If the child class defines __init__() method and does not call super().__init__(), the parent’s __init__() method will not execute. This is because the child’s __init__() method overrides the parent’s class __init__() method entirely like any other method.
15. What will you get the expected result when you execute the below Python code?
class X:
def m1(self):
print("X")
class Y:
def m2(self):
print("Y")
class Z(Y):
pass
z = Z()
z.m2()
z.m1()A. X Y
B. X AttributeError
C. Y X
D. Y AttributeError
d
Class Z(Y) inherits only from class Y. That means it gets all methods and properties of class Y but not of class X. z = Z() creates an object of class Z. Class Z inherits m2() method from Y. So, the result is Y.
Class Z does not inherit from X, and neither Y nor Z defines m1() method. So, Python looks for m1() method in the method resolution order (MRO) and cannot find it. This causes an AttributeError.
Class Z does not inherit from X, and neither Y nor Z defines m1() method. So, Python looks for m1() method in the method resolution order (MRO) and cannot find it. This causes an AttributeError.
16. Identify the type of inheritance:
class A: pass class B(A): pass class C(A): pass
A. Multilevel inheritance
B. Multiple inheritance
C. Hybrid inheritance
D. Hierarchical inheritance
d
Both B and C inherit from the same base class A, which represents hierarchical inheritance.
17. In the below code, which is the immediate parent of class D?
class A: pass class B(A): pass class C(A): pass class D(B, C): pass
A. A
B. B and C
C. B only
D. C only
b
Class D inherits directly from both classes B and C. Therefore, they are its immediate parents.
18. Which type of inheritance is demonstrated by the following code?
class A: pass class B(A): pass class C(A): pass class D(B, C): pass
A. Multilevel inheritance
B. Hierarchical inheritance
C. Multiple inheritance
D. Hybrid inheritance
d
This is a classic case of hybrid inheritance in which multiple, hierarchical, and multilevel structures are combined.
19. Which type of inheritance structure creates a βdiamond problemβ in Python?
A. Single inheritance
B. Multilevel inheritance
C. Hierarchical inheritance
D. Hybrid inheritance with multiple base classes
d
The diamond problem occurs when a class inherits from two or more classes, and those parent classes share a common ancestor. This problem generally occurs in multiple or hybrid inheritance.
20. If no problem occurs in the following Python code, what will be the output?
class A:
def method(self):
print("A")
class B(A):
def method(self):
print("B")
super().method()
class C(A):
def method(self):
print("C")
super().method()
class D(B, C):
def method(self):
print("D")
super().method()
d = D()
d.method()A. D B C A
B. D C B A
C. D A B C
D. A B C D
a
Execution order: D β B β C β A β object
21. What will the print(B.__base__) and print(C.__base__) display in the following code?
class A: pass class B: pass class C(A, B): pass print(B.__base__) print(C.__bases__)
A. object, A, B
B. A, B
C. object, B
D. A, B, object
a
The __bases__ returns a tuple of direct parent classes of a class. For class C, the direct parents are A and B.
22. What is wrong with this inheritance structure code?
class A:
pass
class B(A):
pass
class C(A, B):
passA. Nothing wrong – it’s a valid Python code.
B. Missing super() calls.
C. Class C cannot inherit from both A and B because B is a child of A.
D. Classes should use multiple inheritance with interfaces only.
c
This creates an inconsistent method resolution order (MRO). Python will raise a TypeError because the inheritance creates a cyclic dependency in the class hierarchy (A β B β A). This violates the C3 linearization rules Python uses for multiple inheritance.
23. In a single inheritance scenario, what will Gamma.__mro__[-2] return?
class Alpha:
pass
class Beta(Alpha):
pass
class Gamma(Beta):
pass
print(Gamma.__mro__[-2])
A. <class ‘__main__.Gamma’>
B. <class ‘__main__.Beta’>
C. <class ‘__main__.Alpha’>
D. <class ‘object’>
c
Class hierarchy: Gamma β Beta β Alpha β object. Gamma.__mro__[-2] refers to the second to last class in the method resolution order of the class Gamma. The MRO determines the order in which Python searches for attributes and methods in a class hierarchy. The index -2 accesses the second to last item in the tuple, which is the second parent class in the MRO.
24. Consider the following code. Which type(s) of inheritance does class D represent?
class A: pass class B(A): pass class C: pass class D(B, C): pass
A. Multilevel only
B. Single only
C. Hierarchical and Single
D. Multiple and Multilevel
d
B(A) β multilevel and D(B, C) β inherits from two different classes β multiple. So, together = multiple + multilevel.
25. Which built-in attribute returns the tuple of direct base classes of a class?
A. __bases__
B. __mro__
C. __class__
D. __subclasses__()
a
ClassName.__bases__ returns a tuple of direct parent classes of a class.
26. What will this code print?
class A: pass class B(A): pass class C(B): pass print(A in C.__mro__)
A. False
B. True
C. A
D. <class ‘A’>
b
Since class B inherits from class A and class C inherits from class B, so the full inheritance chain is C β B β A β object. C.__mro__ = (C, B, A, object). The line A in C.__mro__ returns True because class A is a grandparent of class C in the inheritance chain. In other words, the line A in C.__mro__ checks whether class A is present in the method resolution order of class C. Since C inherits from B, and B inherits from A, class A is actually part of C.__mro__.
27. Which of the following best describes multilevel inheritance in Python?
A. A class that inherits from multiple base classes.
B. A class that is inherited by multiple subclasses.
C. A class derived from a class, which is also derived from another class.
D. Both B and C
c
When a class is derived from a class, which is also derived from another class, it is called multilevel inheritance in Python. Example: A β B β C (where C inherits from B, and B inherits from A).
28. What does the following code print?
class A:
pass
class B(A):
pass
b = B()
print(issubclass(B, A))
print(isinstance(b, A))A. True True
B. True False
C. False True
D. False False
a
Since class B is a direct subclass of class A. So, issubclass(B, A) returns True. Since b is an instance of class B, which is a subclass of A. So, b is also an instance of A. Therefore, the isinstance(b, A) returns True.
29. Which of the following is the correct output of the following Python code?
class A:
data = [1, 2]
class B(A):
pass
B.data.append(1)
print(A.data)A. [1, 2]
B. [1, 2, 1]
C. [1]
D. None
b
In Python, if you define a variable at the class level (i.e., outside __init__), it is called a class variable. Class variables are shared among the class and all its subclasses and instances, unless overridden. The class variable data points of the list [1, 2]. This class variable is defined in class A, and class B inherits everything from A without overriding data. Therefore, B.data refers to the same list as A.data. In other words, class B shares the same data as A. Modifying data through class B will affect it in class A too because B.data and A.data point to the same list object in memory.
30. Predict the output:
class A:
def __init__(self):
self.val = 1
class B(A):
def __init__(self):
super().__init__()
self.val += 2
class C(B):
def __init__(self):
super().__init__()
self.val *= 3
print(C().val)A. 1
B. 3
C. 6
D. 9
d
MRO flow: A β B β C. 1 β 1+2 β 3 * 3 = 9.
Quiz Results
0
Total Questions
0
Correct Answers
0
Incorrect Answers
0%
Score






