Python Practice Questions

Topic-based coding problems with sample test cases — build confidence step by step.

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.
No input.
Print 'Animal speaks'.
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.
A single line containing a name.
Print the name.
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!'.
First line: brand. Then call honk().
Print the brand and 'Beep!' on separate lines.
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.
No input.
Print 'Area not defined'.
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().
No input.
Print 'Flying'.
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.
No input.
Print 'Hello from A'.
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.
A single line containing a name.
Print the name.
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.
First line: brand.
Print the brand and True on separate lines.
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).
No input.
Print True.
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.
No input.
Print 'Hello'.
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'.
No input.
Print 'Meow'.
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.
First line: name. Second line: grade.
Print 'Name: [name]' and 'Grade: [grade]'.
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).
First line: initial balance. Second line: deposit. Third line: interest rate.
Print the balance after deposit and interest.
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.
No input.
Print 'A' and 'B' on separate lines.
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 ' -> '.
No input.
Print the MRO names.
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.
First line: brand. Second line: OS.
Print both on separate lines.
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'.
No input.
Print 'Animal speaks' and 'Dog barks' on separate lines.
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.
No input.
Print 'TB24'.
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.
A single line containing the side.
Print the area.
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.
No input.
Print 'Dog'.
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.
First line: type ('car' or 'bike'). Second line: make.
Print the make and vehicle type.
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.
First line: value a. Second line: value b.
Print both values on separate lines.
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.
First line: shape type. Next lines: dimensions.
Print the area rounded to 2 decimal places.
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.
First line: type ('book' or 'dvd'). Second line: title. Third line: extra value (author or duration).
Print the item details.
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.
No input.
Print the MRO class names joined by ' -> '.
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().
First line: type ('manager' or 'intern'). Second line: name. Third line: base pay.
Print the monthly pay rounded to 2 decimal places.
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.
First line: name. Second line: grade. Third line: scholarship.
Print name, grade, and scholarship on separate lines.
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.
A single line containing 'csv' or 'json'.
Print the two steps performed.
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.
A single line containing the bat's name.
Print the name, warm-blooded status, and that it can fly.
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().
No input.
Print 'Engine started'.
Sample Input 1

                      
Sample Output 1
Engine started
Sample Input 2

                      
Sample Output 2
Engine started
Sample Input 3

                      
Sample Output 3
Engine started