Multiple Choice Questions (MCQs) on Inheritance in Python – OOP Practice
Boost your Python skills with these MCQs on Object-Oriented Programming and Inheritance. Ideal for beginners, students, and job seekers to test and strengthen their understanding of Python OOP concepts.
📝 Multiple Choice Questions
🟢 Beginner
Q1. What is the main purpose of inheritance in Python?
-
To create multiple objects of a class
-
To reuse code and extend functionality
-
To define private methods in a class
-
To create a new programming paradigm
Answer
To reuse code and extend functionality
Inheritance allows code reuse and method overriding in child classes.
Q2. Which of the following is the correct syntax for inheritance in Python?
-
class Child inherits Parent:
-
class Child(Parent):
-
class Parent -> Child:
-
class Parent(Child):
Answer
class Child(Parent):
In Python, a child class is defined using the syntax `class Child(Parent):`, where `Child` inherits from `Parent`.
Q3. What is the output of the following code?
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
pass
d = Dog()
d.speak()
-
Animal speaks
-
Dog speaks
-
Error: **speak()** not defined
-
Nothing
Answer
Animal speaks
Dog inherits the `speak()` method from Animal.
Q4. What is the output of the following code?
class Parent:
def __init__(self):
print("Parent constructor")
class Child(Parent):
def __init__(self):
print("Child constructor")
c = Child()
-
Parent constructor
-
Child constructor
-
Parent constructor Child constructor
-
Error
Answer
Child constructor
The `Child` constructor overrides the `Parent` constructor.
Q5. What is the primary purpose of inheritance in Python?
-
To improve memory efficiency
-
To hide implementation details
-
To enable code reusability
-
To reduce program complexity
Answer
To enable code reusability
Inheritance helps avoid code duplication by allowing child classes to reuse methods and properties of parent classes.
Q6. Which syntax correctly defines a class `Child` inheriting from `Parent`?
-
{"class Child extends Parent"=>nil}
-
{"class Child(Parent)"=>nil}
-
{"class Child inherits Parent"=>nil}
-
{"class Child"=>"Parent"}
Answer
class Child(Parent):
In Python, inheritance is denoted by placing the parent class name in parentheses after the child class name.
🟡 Intermediate
Q1. What is the output of the following Python code?
class Parent:
def greet(self):
print("Hello from Parent")
class Child(Parent):
def greet(self):
print("Hello from Child")
Parent.greet(self)
super().greet()
obj = Child()
obj.greet()
-
Hello from Child
Hello from Parent
Hello from Parent
-
Hello from Parent
Hello from Child
Hello from Parent
-
Hello from Child
Hello from Child
Hello from Parent
-
Hello from Parent
Hello from Parent
Hello from Child
Answer
Hello from Child
Hello from Parent
Hello from Parent
The `Child.greet()` method prints "Hello from Child", then explicitly calls `Parent.greet(self)` and `super().greet()`, both of which call the `Parent` class's `greet()` method, resulting in "Hello from Parent" printed twice.
Q2. What is the output of the following code?
class Parent:
def show(self):
print("Parent")
class Child(Parent):
def show(self):
print("Child")
obj = Child()
obj.show()
-
Parent
-
Child
-
Parent Child
-
Error
Answer
Child
Q3. What is the output of the following code?
class Parent:
def greet(self):
print("Hello from Parent!")
class Child(Parent):
def greet(self):
print("Hello from Child!")
obj = Child()
obj.greet()
-
Hello from Parent!
-
Hello from Child!
-
Error
-
No output
Answer
Hello from Child!
The `greet` method is overridden in the `Child` class, so that version is called.
Q4. What does the `super()` function do?
-
Returns the base class instance
-
Calls a method from a sibling class
-
Calls a method from the parent class
-
Creates a new subclass
Answer
Calls a method from the parent class
`super()` is used to call methods from the parent class, especially in method overrides
Q5. In multiple inheritance, the method resolution order (MRO) follows:
-
Depth-first search
-
Breadth-first search
-
C3 linearization
-
Random order
Answer
C3 linearization
Python uses C3 linearization to determine the order in which classes are searched when executing a method.
Q6. What does `isinstance(child_obj, Parent)` return?
-
true
-
false
-
TypeError
-
None
Answer
true
If `child_obj` is an instance of a class derived from `Parent`, `isinstance` will return `True`.
🔴 Advanced
Q1. Which type of inheritance is NOT supported in Python?
-
Single
-
Multiple
-
Cascading
-
Multilevel
Answer
Cascading
Python supports single, multiple, and multilevel inheritance, but "cascading" is not a recognized inheritance type.
Q2. How do you explicitly call a parent class method from a child class?
-
Parent.method(self)
-
self.parent_method()
-
super().method()
-
Both A and C
Answer
Both A and C
You can call a parent method using either `Parent.method(self)` or `super().method()`.
Q3. Which statement about private members (e.g., `__var`) in inheritance is true?
-
They are directly accessible in subclasses
-
They are inaccessible due to name mangling
-
They can be accessed using `super()`
-
They are inherited without name mangling
Answer
They are inaccessible due to name mangling
Private members with double underscores are name-mangled and not directly accessible from subclasses.
Q4. Hybrid inheritance refers to:
-
Combining multiple types of inheritance
-
Inheriting from two parent classes
-
Overriding all parent methods
-
Using interfaces only
Answer
Combining multiple types of inheritance
Hybrid inheritance is a combination of more than one type of inheritance, like multiple and multilevel.
🧠 Practice & Progress
Explore More Topics
Python Fundamentals
Fundamentals more ...
🧠 Python Advanced
Object-Oriented Programming in Python (OOP)
More...
🧠 Modules