Inheritance Practice
Build class hierarchies with single, multiple and multilevel inheritance.
1
Basic Single Inheritance
Easy
Write a base class Animal with method speak() printing 'Animal speaks', and a class Dog that inherits it. Call speak() on a Dog object.
Input Format
No input.
Output Format
Print 'Animal speaks'.
Sample Test Cases
|
Sample Input 1
|
Sample Output 1
Animal speaks |
|
Sample Input 2
|
Sample Output 2
Animal speaks |
|
Sample Input 3
|
Sample Output 3
Animal speaks |
2
Inherit Attributes from Parent
Easy
Write a class Person with name attribute, and a class Student(Person) that inherits it. Read a name and print it.
Input Format
A single line containing a name.
Output Format
Print the name.
Sample Test Cases
|
Sample Input 1
Alice |
Sample Output 1
Alice |
|
Sample Input 2
Bob |
Sample Output 2
Bob |
|
Sample Input 3
Sam |
Sample Output 3
Sam |
3
Child Class Adds a Method
Easy
Write a class Vehicle with brand attribute, and a class Car(Vehicle) with an extra method honk() printing 'Beep!'.
Input Format
First line: brand. Then call honk().
Output Format
Print the brand and 'Beep!' on separate lines.
Sample Test Cases
|
Sample Input 1
Toyota |
Sample Output 1
Toyota Beep! |
|
Sample Input 2
Honda |
Sample Output 2
Honda Beep! |
|
Sample Input 3
Ford |
Sample Output 3
Ford Beep! |
4
Inherit Parent Method
Easy
Write a class Shape with method area() printing 'Area not defined', and a class Circle(Shape) that uses it.
Input Format
No input.
Output Format
Print 'Area not defined'.
Sample Test Cases
|
Sample Input 1
|
Sample Output 1
Area not defined |
|
Sample Input 2
|
Sample Output 2
Area not defined |
|
Sample Input 3
|
Sample Output 3
Area not defined |
5
Child Object Calls Parent Method
Easy
Write a class Bird with method fly() printing 'Flying', and class Eagle(Bird). Create an Eagle object and call fly().
Input Format
No input.
Output Format
Print 'Flying'.
Sample Test Cases
|
Sample Input 1
|
Sample Output 1
Flying |
|
Sample Input 2
|
Sample Output 2
Flying |
|
Sample Input 3
|
Sample Output 3
Flying |
6
Multilevel Inheritance
Easy
Write classes A, B(A), C(B). Give A a method hello() and call it on a C object.
Input Format
No input.
Output Format
Print 'Hello from A'.
Sample Test Cases
|
Sample Input 1
|
Sample Output 1
Hello from A |
|
Sample Input 2
|
Sample Output 2
Hello from A |
|
Sample Input 3
|
Sample Output 3
Hello from A |
7
Inherit and Print Parent Data
Easy
Write a class Employee with name, and a class Manager(Employee). Read a name and print it via the child class.
Input Format
A single line containing a name.
Output Format
Print the name.
Sample Test Cases
|
Sample Input 1
Ravi |
Sample Output 1
Ravi |
|
Sample Input 2
Sam |
Sample Output 2
Sam |
|
Sample Input 3
Kim |
Sample Output 3
Kim |
8
Child Class with Own Attribute
Easy
Write a class Laptop with brand, and a class GamingLaptop(Laptop) with extra attribute rgb = True. Print both.
Input Format
First line: brand.
Output Format
Print the brand and True on separate lines.
Sample Test Cases
|
Sample Input 1
ASUS |
Sample Output 1
ASUS True |
|
Sample Input 2
Dell |
Sample Output 2
Dell True |
|
Sample Input 3
HP |
Sample Output 3
HP True |
9
Use isinstance with Inheritance
Easy
Write classes Animal and Dog(Animal). Create a Dog object and print isinstance(dog, Animal).
Input Format
No input.
Output Format
Print True.
Sample Test Cases
|
Sample Input 1
|
Sample Output 1
True |
|
Sample Input 2
|
Sample Output 2
True |
|
Sample Input 3
|
Sample Output 3
True |
10
Inheritance with Method Call Chain
Easy
Write a class GrandParent with method greet() printing 'Hello', class Parent(GrandParent), and class Child(Parent). Call greet() on Child.
Input Format
No input.
Output Format
Print 'Hello'.
Sample Test Cases
|
Sample Input 1
|
Sample Output 1
Hello |
|
Sample Input 2
|
Sample Output 2
Hello |
|
Sample Input 3
|
Sample Output 3
Hello |
1
Override Parent Method
Medium
Write a class Animal with sound() printing 'Generic sound', and a class Cat(Animal) that overrides it to print 'Meow'.
Input Format
No input.
Output Format
Print 'Meow'.
Sample Test Cases
|
Sample Input 1
|
Sample Output 1
Meow |
|
Sample Input 2
|
Sample Output 2
Meow |
|
Sample Input 3
|
Sample Output 3
Meow |
2
super() to Call Parent Constructor
Medium
Write a class Person with __init__(name), and a class Student(Person) that calls super().__init__(name) and adds grade.
Input Format
First line: name. Second line: grade.
Output Format
Print 'Name: [name]' and 'Grade: [grade]'.
Sample Test Cases
|
Sample Input 1
Alice A |
Sample Output 1
Name: Alice Grade: A |
|
Sample Input 2
Bob B |
Sample Output 2
Name: Bob Grade: B |
|
Sample Input 3
Sam C |
Sample Output 3
Name: Sam Grade: C |
3
Single Inheritance with Extra Method
Medium
Write a class BankAccount with balance and deposit(), and a class SavingsAccount(BankAccount) with apply_interest(rate).
Input Format
First line: initial balance. Second line: deposit. Third line: interest rate.
Output Format
Print the balance after deposit and interest.
Sample Test Cases
|
Sample Input 1
1000 500 10 |
Sample Output 1
1650.0 |
|
Sample Input 2
500 0 0 |
Sample Output 2
500.0 |
|
Sample Input 3
2000 1000 5 |
Sample Output 3
3150.0 |
4
Multiple Inheritance Basics
Medium
Write classes A with method a() printing 'A', B with method b() printing 'B', and C(A, B). Call both methods on a C object.
Input Format
No input.
Output Format
Print 'A' and 'B' on separate lines.
Sample Test Cases
|
Sample Input 1
|
Sample Output 1
A B |
|
Sample Input 2
|
Sample Output 2
A B |
|
Sample Input 3
|
Sample Output 3
A B |
5
Check Method Resolution
Medium
Write classes A and B, and class C(A, B). Print the MRO (method resolution order) class names joined by ' -> '.
Input Format
No input.
Output Format
Print the MRO names.
Sample Test Cases
|
Sample Input 1
|
Sample Output 1
C -> A -> B -> object |
|
Sample Input 2
|
Sample Output 2
C -> A -> B -> object |
|
Sample Input 3
|
Sample Output 3
C -> A -> B -> object |
6
Inherited Attribute Access
Medium
Write a class Phone with brand attribute, and a class Smartphone(Phone) that adds os. Read both and print them.
Input Format
First line: brand. Second line: OS.
Output Format
Print both on separate lines.
Sample Test Cases
|
Sample Input 1
Samsung Android |
Sample Output 1
Samsung Android |
|
Sample Input 2
Apple iOS |
Sample Output 2
Apple iOS |
|
Sample Input 3
Xiaomi Android |
Sample Output 3
Xiaomi Android |
7
Parent Method with Super in Override
Medium
Write a class Animal with speak() printing 'Animal speaks', and a class Dog(Animal) that overrides speak() to first call super().speak() then print 'Dog barks'.
Input Format
No input.
Output Format
Print 'Animal speaks' and 'Dog barks' on separate lines.
Sample Test Cases
|
Sample Input 1
|
Sample Output 1
Animal speaks Dog barks |
|
Sample Input 2
|
Sample Output 2
Animal speaks Dog barks |
|
Sample Input 3
|
Sample Output 3
Animal speaks Dog barks |
8
Inheritance with Class Variables
Medium
Write a class Employee with class variable company = 'TB24', and a class Developer(Employee). Print the company via a Developer object.
Input Format
No input.
Output Format
Print 'TB24'.
Sample Test Cases
|
Sample Input 1
|
Sample Output 1
TB24 |
|
Sample Input 2
|
Sample Output 2
TB24 |
|
Sample Input 3
|
Sample Output 3
TB24 |
9
Child Class with Additional Method
Medium
Write a class Shape with area() returning 0, and a class Square(Shape) with side and area() returning side*side.
Input Format
A single line containing the side.
Output Format
Print the area.
Sample Test Cases
|
Sample Input 1
5 |
Sample Output 1
25 |
|
Sample Input 2
3 |
Sample Output 2
9 |
|
Sample Input 3
10 |
Sample Output 3
100 |
10
Parent Reference to Child
Medium
Write classes Animal and Dog(Animal). Create a Dog object, store it in a variable of type Animal, and print the type name of the object.
Input Format
No input.
Output Format
Print 'Dog'.
Sample Test Cases
|
Sample Input 1
|
Sample Output 1
Dog |
|
Sample Input 2
|
Sample Output 2
Dog |
|
Sample Input 3
|
Sample Output 3
Dog |
1
Vehicle Hierarchy with Fuel
Hard
Write a class Vehicle with make and fuel methods, and classes Car and Bike that override a details() method.
Input Format
First line: type ('car' or 'bike'). Second line: make.
Output Format
Print the make and vehicle type.
Sample Test Cases
|
Sample Input 1
car Toyota |
Sample Output 1
Make: Toyota Type: Car |
|
Sample Input 2
bike Hero |
Sample Output 2
Make: Hero Type: Bike |
|
Sample Input 3
car Hyundai |
Sample Output 3
Make: Hyundai Type: Car |
2
Multiple Inheritance with super()
Hard
Write classes A with __init__ setting a, B with __init__ setting b, and C(A, B) that calls both via super() and prints both values.
Input Format
First line: value a. Second line: value b.
Output Format
Print both values on separate lines.
Sample Test Cases
|
Sample Input 1
10 20 |
Sample Output 1
10 20 |
|
Sample Input 2
5 6 |
Sample Output 2
5 6 |
|
Sample Input 3
0 0 |
Sample Output 3
0 0 |
3
Abstract-Like Base with Override
Hard
Write a class Shape with a method area() that raises NotImplementedError, and classes Circle and Rectangle that override it.
Input Format
First line: shape type. Next lines: dimensions.
Output Format
Print the area rounded to 2 decimal places.
Sample Test Cases
|
Sample Input 1
circle 5 |
Sample Output 1
78.50 |
|
Sample Input 2
rectangle 3 4 |
Sample Output 2
12.00 |
|
Sample Input 3
circle 2 |
Sample Output 3
12.56 |
4
Library System with Inheritance
Hard
Write a class Item with title, and classes Book and DVD that extend it with extra attributes.
Input Format
First line: type ('book' or 'dvd'). Second line: title. Third line: extra value (author or duration).
Output Format
Print the item details.
Sample Test Cases
|
Sample Input 1
book Python Guido |
Sample Output 1
Title: Python Author: Guido |
|
Sample Input 2
dvd Inception 148 |
Sample Output 2
Title: Inception Duration: 148 |
|
Sample Input 3
book Python Guido |
Sample Output 3
Title: Python Author: Guido |
5
Diamond Problem MRO
Hard
Write classes A, B(A), C(A), and D(B, C). Print the MRO to show the resolution order.
Input Format
No input.
Output Format
Print the MRO class names joined by ' -> '.
Sample Test Cases
|
Sample Input 1
|
Sample Output 1
D -> B -> C -> A -> object |
|
Sample Input 2
|
Sample Output 2
D -> B -> C -> A -> object |
|
Sample Input 3
|
Sample Output 3
D -> B -> C -> A -> object |
6
Employee Hierarchy with Pay
Hard
Write a class Employee with name and base pay, and classes Manager (adds bonus) and Intern (halved pay) overriding monthly_pay().
Input Format
First line: type ('manager' or 'intern'). Second line: name. Third line: base pay.
Output Format
Print the monthly pay rounded to 2 decimal places.
Sample Test Cases
|
Sample Input 1
manager Alice 60000 |
Sample Output 1
66000.00 |
|
Sample Input 2
intern Bob 10000 |
Sample Output 2
5000.00 |
|
Sample Input 3
manager Sam 50000 |
Sample Output 3
55000.00 |
7
Multi-Level: Person -> Student -> Scholar
Hard
Write classes Person(name), Student(Person) adding grade, and Scholar(Student) adding scholarship amount.
Input Format
First line: name. Second line: grade. Third line: scholarship.
Output Format
Print name, grade, and scholarship on separate lines.
Sample Test Cases
|
Sample Input 1
Alice A 5000 |
Sample Output 1
Alice A 5000 |
|
Sample Input 2
Bob B 2000 |
Sample Output 2
Bob B 2000 |
|
Sample Input 3
Sam A 0 |
Sample Output 3
Sam A 0 |
8
Template Method Pattern
Hard
Write a base class DataProcessor with a template method process() that calls steps step1() and step2(), overridden in subclasses CSVProcessor and JSONProcessor.
Input Format
A single line containing 'csv' or 'json'.
Output Format
Print the two steps performed.
Sample Test Cases
|
Sample Input 1
csv |
Sample Output 1
Step1: parse csv Step2: clean csv |
|
Sample Input 2
json |
Sample Output 2
Step1: parse json Step2: clean json |
|
Sample Input 3
csv |
Sample Output 3
Step1: parse csv Step2: clean csv |
9
Zoo with Mixed Inheritance
Hard
Write a base class Animal with name, and classes Mammal(Animal) with warm_blooded=True, Bird(Animal) with wings=True, and Bat(Mammal) that can fly.
Input Format
A single line containing the bat's name.
Output Format
Print the name, warm-blooded status, and that it can fly.
Sample Test Cases
|
Sample Input 1
Bruce |
Sample Output 1
Bruce True Can fly |
|
Sample Input 2
Bruce |
Sample Output 2
Bruce True Can fly |
|
Sample Input 3
Lucy |
Sample Output 3
Lucy True Can fly |
10
Composition over Inheritance
Hard
Write a class Engine with start() printing 'Engine started', and a class Car that composes an Engine instead of inheriting, calling engine.start() from its own start().
Input Format
No input.
Output Format
Print 'Engine started'.
Sample Test Cases
|
Sample Input 1
|
Sample Output 1
Engine started |
|
Sample Input 2
|
Sample Output 2
Engine started |
|
Sample Input 3
|
Sample Output 3
Engine started |
Competitive MCQs — Inheritance
Code snippets, output prediction, concepts & error spotting. Pick an answer to see instant feedback.
Score
0/ 34
Q1
What is the output of this code?
python
1
2
3
4
5
6
7
2
3
4
5
6
7
class A:
def hi(self):
return "A"
class B(A):
pass
b = B()
print(b.hi())Correct!
Wrong — correct answer is .
B inherits hi() from A, so b.hi() returns "A".
Q2
What is the output of this code?
class A:
def hi(self):
return "A"
class B(A):
pass
b = B()
print(b.hi())
class A:
def hi(self):
return "A"
class B(A):
pass
b = B()
print(b.hi())
Correct!
Wrong — correct answer is .
B inherits hi() from A, so b.hi() returns "A".
Q3
Which keyword is used to define inheritance?
Correct!
Wrong — correct answer is .
class Child(Parent): creates a subclass.
Q4
What does issubclass(B, A) return if B inherits from A?
Correct!
Wrong — correct answer is .
issubclass checks the inheritance relationship.
Q5
What is the output of this code?
python
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
class A:
def greet(self):
return "Parent"
class B(A):
def greet(self):
return "Child"
b = B()
print(b.greet())Correct!
Wrong — correct answer is .
B overrides greet(), so the child version runs and returns "Child".
Q6
What is the output of this code?
class A:
def greet(self):
return "Parent"
class B(A):
def greet(self):
return "Child"
b = B()
print(b.greet())
class A:
def greet(self):
return "Parent"
class B(A):
def greet(self):
return "Child"
b = B()
print(b.greet())
Correct!
Wrong — correct answer is .
B overrides greet(), so the child version runs and returns "Child".
Q7
Which method calls the parent class implementation?
Correct!
Wrong — correct answer is .
super() gives access to inherited methods from the parent.
Q8
What is multiple inheritance?
Correct!
Wrong — correct answer is .
class C(A, B): inherits from both A and B.
Q9
What is the output of this code?
python
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
class A:
def __init__(self):
print("A init")
class B(A):
def __init__(self):
super().__init__()
print("B init")
b = B()Correct!
Wrong — correct answer is .
super().__init__() runs A's constructor first, then B continues.
Q10
What is the output of this code?
class A:
def __init__(self):
print("A init")
class B(A):
def __init__(self):
super().__init__()
print("B init")
b = B()
class A:
def __init__(self):
print("A init")
class B(A):
def __init__(self):
super().__init__()
print("B init")
b = B()
Correct!
Wrong — correct answer is .
super().__init__() runs A's constructor first, then B continues.
Q11
Which built-in checks whether an object is an instance of a class?
Correct!
Wrong — correct answer is .
isinstance(obj, Class) returns True when obj is an instance.
Q12
What is the output of this code?
python
1
2
3
4
5
6
7
2
3
4
5
6
7
class Animal:
def speak(self):
return "..."
class Dog(Animal):
def speak(self):
return "Woof"
print(Dog().speak())Correct!
Wrong — correct answer is .
Dog overrides speak(), so Dog().speak() → "Woof".
Q13
What is the output of this code?
class Animal:
def speak(self):
return "..."
class Dog(Animal):
def speak(self):
return "Woof"
print(Dog().speak())
class Animal:
def speak(self):
return "..."
class Dog(Animal):
def speak(self):
return "Woof"
print(Dog().speak())
Correct!
Wrong — correct answer is .
Dog overrides speak(), so Dog().speak() → "Woof".
Q14
What is the MRO?
Correct!
Wrong — correct answer is .
MRO defines the order in which Python searches base classes for attributes.
Q15
What is the output of this code?
class A:
def f(self):
return "A"
class B(A):
pass
print(B().f())
class A:
def f(self):
return "A"
class B(A):
pass
print(B().f())
Correct!
Wrong — correct answer is .
B has no f of its own, so the inherited A.f runs and returns "A".
Q16
Which keyword refers to the parent class?
Correct!
Wrong — correct answer is .
super() returns a proxy that delegates method calls up the MRO.
Q17
What is the output of this code?
class A:
def hi(self):
return "Parent hi"
class B(A):
def hi(self):
return super().hi() + " + Child"
print(B().hi())
class A:
def hi(self):
return "Parent hi"
class B(A):
def hi(self):
return super().hi() + " + Child"
print(B().hi())
Correct!
Wrong — correct answer is .
super().hi() invokes the parent version, then the child appends text.
Q18
What does class C(A, B) demonstrate?
Correct!
Wrong — correct answer is .
C inherits from both A and B in multiple inheritance.
Q19
What is the output of this code?
class A:
pass
class B(A):
pass
class C(B):
pass
print(issubclass(C, A))
class A:
pass
class B(A):
pass
class C(B):
pass
print(issubclass(C, A))
Correct!
Wrong — correct answer is .
issubclass() is transitive: C is a subclass of A through B.
Q20
What is the output of this code?
class A:
def __init__(self):
print("A", end="")
class B(A):
def __init__(self):
super().__init__()
print("B", end="")
B()
class A:
def __init__(self):
print("A", end="")
class B(A):
def __init__(self):
super().__init__()
print("B", end="")
B()
Correct!
Wrong — correct answer is .
super().__init__() runs A's constructor before B finishes.
Q21
Which built-in tells whether an object is an instance of a class or its subclass?
Correct!
Wrong — correct answer is .
isinstance() returns True for subclasses too; type() does exact matches only.
Q22
What is the output of this code?
class A:
x = 10
class B(A):
pass
b = B()
print(b.x)
class A:
x = 10
class B(A):
pass
b = B()
print(b.x)
Correct!
Wrong — correct answer is .
Instances of B inherit class attribute x from A.
Q23
What is an override?
Correct!
Wrong — correct answer is .
Overriding replaces the inherited behavior with a subclass version.
Q24
Which is TRUE about overriding?
Correct!
Wrong — correct answer is .
Override means the child provides its own implementation of an inherited method.
Q25
What is the output of this code?
class A:
def speak(self):
return "A"
class B(A):
def speak(self):
return "B"
class C(B):
pass
print(C().speak())
class A:
def speak(self):
return "A"
class B(A):
def speak(self):
return "B"
class C(B):
pass
print(C().speak())
Correct!
Wrong — correct answer is .
C inherits B's override, so speak() returns "B".
Q26
Which error does a diamond inheritance scenario risk?
Correct!
Wrong — correct answer is .
Python linearizes the diamond with C3 MRO to keep resolution consistent.
Q27
What is the output of this code?
class A:
def greet(self):
return "Hello"
class B(A):
def greet(self):
return super().greet() + " World"
print(B().greet())
class A:
def greet(self):
return "Hello"
class B(A):
def greet(self):
return super().greet() + " World"
print(B().greet())
Correct!
Wrong — correct answer is .
The parent returns "Hello" and the child appends " World".
Q28
What does a subclass inherit by default?
Correct!
Wrong — correct answer is .
Subclasses inherit accessible members; double-underscore names are mangled per class.
Q29
Which of these creates a subclass of Animal?
Correct!
Wrong — correct answer is .
Python uses parentheses: class Dog(Animal):.
Q30
What is the output of this code?
class A:
def f(self):
return 1
class B(A):
def f(self):
return super().f() + 1
class C(B):
def f(self):
return super().f() + 1
print(C().f())
class A:
def f(self):
return 1
class B(A):
def f(self):
return super().f() + 1
class C(B):
def f(self):
return super().f() + 1
print(C().f())
Correct!
Wrong — correct answer is .
The chain adds 1 at B and again at C, so 1 + 1 + 1 = 3.
Q31
Which is an example of an "is-a" relationship?
Correct!
Wrong — correct answer is .
Inheritance models is-a; composition models has-a.
Q32
What is the output of this code?
class A:
def __init__(self):
self.x = 1
class B(A):
def __init__(self):
super().__init__()
self.x += 1
print(B().x)
class A:
def __init__(self):
self.x = 1
class B(A):
def __init__(self):
super().__init__()
self.x += 1
print(B().x)
Correct!
Wrong — correct answer is .
The parent sets x=1, then the child increments it to 2.
Q33
How many parents can a class inherit from in Python?
Correct!
Wrong — correct answer is .
class C(A, B, ...): lists multiple parents; Python resolves via MRO.
Q34
What does the MRO of class C(B, A) look like?
Correct!
Wrong — correct answer is .
Method lookup checks C, its listed parents in order, then object.