Python Java SQL Course Notes C C++ HTML CSS JS

Python Practice Questions

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

Polymorphism Practice

Method overloading, overriding and duck typing in Python.

1
Method Overriding Basics
Easy
Write a class Animal with sound() printing 'Generic sound', and a class Dog(Animal) overriding it to print 'Bark'.
No input.
Print 'Bark'.
Sample Input 1

                      
Sample Output 1
Bark
Sample Input 2

                      
Sample Output 2
Bark
Sample Input 3

                      
Sample Output 3
Bark
2
Same Method Different Outputs
Easy
Write classes Cat and Dog each with a method speak() printing 'Meow' and 'Woof'. Create both and call speak().
No input.
Print 'Meow' and 'Woof' on separate lines.
Sample Input 1

                      
Sample Output 1
Meow
Woof
Sample Input 2

                      
Sample Output 2
Meow
Woof
Sample Input 3

                      
Sample Output 3
Meow
Woof
3
Polymorphism via Common Interface
Easy
Write classes Circle and Square each with area(). Print the area of a circle (r=5) and a square (side=4).
No input.
Print both areas on separate lines.
Sample Input 1

                      
Sample Output 1
78.5
16
Sample Input 2

                      
Sample Output 2
78.5
16
Sample Input 3

                      
Sample Output 3
78.5
16
4
Duck Typing — Call Method on Any Object
Easy
Write classes Duck with quack() and Person with quack(). Call quack() on both and print the results.
No input.
Print 'Quack quack' and 'Person quacking' on separate lines.
Sample Input 1

                      
Sample Output 1
Quack quack
Person quacking
Sample Input 2

                      
Sample Output 2
Quack quack
Person quacking
Sample Input 3

                      
Sample Output 3
Quack quack
Person quacking
5
Function Taking Any Shape
Easy
Write a function describe(shape) that calls shape.info(). Define Circle and Square with info() methods.
A single line containing 'circle' or 'square'.
Print the shape description.
Sample Input 1
circle
Sample Output 1
I am a circle
Sample Input 2
square
Sample Output 2
I am a square
Sample Input 3
circle
Sample Output 3
I am a circle
6
Operator Polymorphism — String Repeat
Easy
Write a program that prints the string 'Ha' repeated n times using the * operator.
A single line containing integer n.
Print the repeated string.
Sample Input 1
3
Sample Output 1
HaHaHa
Sample Input 2
5
Sample Output 2
HaHaHaHaHa
Sample Input 3
1
Sample Output 3
Ha
7
Polymorphic Method Call in Loop
Easy
Write classes Bird and Fish each with move(). Loop over both objects calling move() and print the results.
No input.
Print both move descriptions.
Sample Input 1

                      
Sample Output 1
Flying in the sky
Swimming in water
Sample Input 2

                      
Sample Output 2
Flying in the sky
Swimming in water
Sample Input 3

                      
Sample Output 3
Flying in the sky
Swimming in water
8
Len Polymorphism
Easy
Write a program that prints len() of a string 'hello' and a list [1,2,3] on separate lines.
No input.
Print 5 and 3 on separate lines.
Sample Input 1

                      
Sample Output 1
5
3
Sample Input 2

                      
Sample Output 2
5
3
Sample Input 3

                      
Sample Output 3
5
3
9
Override with Different Return
Easy
Write a class Vehicle with speed() returning 0, and a class Car(Vehicle) overriding it to return 120.
No input.
Print 120.
Sample Input 1

                      
Sample Output 1
120
Sample Input 2

                      
Sample Output 2
120
Sample Input 3

                      
Sample Output 3
120
10
Simple Polymorphic Function
Easy
Write a function area(shape) that returns shape.area() and call it with a Rectangle(4,5) object.
No input.
Print 20.
Sample Input 1

                      
Sample Output 1
20
Sample Input 2

                      
Sample Output 2
20
Sample Input 3

                      
Sample Output 3
20
1
Polymorphism with Inheritance
Medium
Write a base class Shape with area() returning 0, and classes Rectangle and Triangle overriding it. Compute both areas.
No input.
Print 20 and 15 on separate lines.
Sample Input 1

                      
Sample Output 1
20
15
Sample Input 2

                      
Sample Output 2
20
15
Sample Input 3

                      
Sample Output 3
20
15
2
Method Overloading via Default Arguments
Medium
Write a class Math with a method add(a, b, c=0) that works with 2 or 3 arguments.
First line: two integers. Second line: three integers.
Print the two results on separate lines.
Sample Input 1
2 3
1 2 3
Sample Output 1
5
6
Sample Input 2
10 20
1 2 3
Sample Output 2
30
6
Sample Input 3
0 0
0 0 0
Sample Output 3
0
0
3
Duck Typing with Different Classes
Medium
Write a function make_sound(animal) that calls animal.sound(). Pass a Dog and a Duck object and print both sounds.
No input.
Print 'Bark' and 'Quack' on separate lines.
Sample Input 1

                      
Sample Output 1
Bark
Quack
Sample Input 2

                      
Sample Output 2
Bark
Quack
Sample Input 3

                      
Sample Output 3
Bark
Quack
4
Polymorphic Payment Methods
Medium
Write a base class Payment with pay() raising NotImplementedError, and classes Card and UPI overriding pay().
A single line containing 'card' or 'upi'.
Print the payment confirmation.
Sample Input 1
card
Sample Output 1
Payment via Card
Sample Input 2
upi
Sample Output 2
Payment via UPI
Sample Input 3
card
Sample Output 3
Payment via Card
5
Override __str__ in Subclasses
Medium
Write a base class Item with __str__ returning 'Item', and a class Book(Item) overriding __str__ to return 'Book: [title]'.
A single line containing a book title.
Print the string representation.
Sample Input 1
Python
Sample Output 1
Book: Python
Sample Input 2
Java
Sample Output 2
Book: Java
Sample Input 3
C
Sample Output 3
Book: C
6
Function Polymorphism with len()
Medium
Write a function total_len(a, b) that returns len(a) + len(b). Call it with strings and lists.
First line: two strings. Second line: two lists.
Print both totals on separate lines.
Sample Input 1
hi hello
1 2
Sample Output 1
8
2
Sample Input 2
a b
1 2
Sample Output 2
3
2
Sample Input 3
xyz
9
Sample Output 3
3
1
7
Polymorphic Area with Type
Medium
Write classes Circle and Rectangle with area() and also a method shape_type().
A single line containing 'circle' or 'rectangle'.
Print the shape type and its area.
Sample Input 1
circle
Sample Output 1
circle
78.5
Sample Input 2
rectangle
Sample Output 2
rectangle
20
Sample Input 3
circle
Sample Output 3
circle
78.5
8
Overriding Parent Method with super()
Medium
Write a class Vehicle with describe() printing 'Generic vehicle', and a class Bike(Vehicle) overriding describe() to call super() then print 'It is a bike'.
No input.
Print both lines.
Sample Input 1

                      
Sample Output 1
Generic vehicle
It is a bike
Sample Input 2

                      
Sample Output 2
Generic vehicle
It is a bike
Sample Input 3

                      
Sample Output 3
Generic vehicle
It is a bike
9
Polymorphism in a List
Medium
Write classes Dog, Cat, and Cow each with speak(). Store objects of all three in a list and print all sounds.
No input.
Print 'Bark', 'Meow', and 'Moo' on separate lines.
Sample Input 1

                      
Sample Output 1
Bark
Meow
Moo
Sample Input 2

                      
Sample Output 2
Bark
Meow
Moo
Sample Input 3

                      
Sample Output 3
Bark
Meow
Moo
10
Common Interface — Sortable
Medium
Write classes Student with marks, and a function best(a, b) that returns the student with higher marks using a common score() method.
First line: name and marks of student 1. Second line: name and marks of student 2.
Print the name of the higher-scoring student.
Sample Input 1
Alice 85
Bob 90
Sample Output 1
Bob
Sample Input 2
X 50
Y 60
Sample Output 2
Y
Sample Input 3
P 90
Q 90
Sample Output 3
P
1
Polymorphic Shape Calculator
Hard
Write a base class Shape with area(), and classes Circle, Rectangle, and Triangle overriding it. Read shape type and dimensions, print the area.
First line: shape type. Next lines: dimensions.
Print the area rounded to 2 decimal places.
Sample Input 1
triangle
3 4
Sample Output 1
6.00
Sample Input 2
square
5
Sample Output 2
25.00
Sample Input 3
circle
2
Sample Output 3
12.56
2
Operator Overloading — Add Vectors
Hard
Write a class Vector with x and y, and overload the + operator using __add__ so (x1+x2, y1+y2).
First line: x1 y1. Second line: x2 y2.
Print the resulting vector as (x, y).
Sample Input 1
1 2
3 4
Sample Output 1
(4, 6)
Sample Input 2
0 0
1 1
Sample Output 2
(1, 1)
Sample Input 3
-1 -1
2 2
Sample Output 3
(1, 1)
3
Operator Overloading — Compare Points
Hard
Write a class Point with x and y and overload __lt__ comparing by distance from origin.
First line: point 1. Second line: point 2.
Print 'P1 is closer' or 'P2 is closer'.
Sample Input 1
1 1
3 4
Sample Output 1
P1 is closer
Sample Input 2
0 0
1 1
Sample Output 2
P1 is closer
Sample Input 3
2 2
0 0
Sample Output 3
P2 is closer
4
Polymorphic Notification System
Hard
Write a base class Notification with send() raising NotImplementedError, and classes Email and SMS overriding send().
A single line containing 'email' or 'sms'.
Print the notification message.
Sample Input 1
email
Sample Output 1
Sending email...
Sample Input 2
sms
Sample Output 2
Sending SMS...
Sample Input 3
email
Sample Output 3
Sending email...
5
Dynamic Dispatch in a Loop
Hard
Write classes Circle, Square, and Triangle each with area(). Create a list of objects and print the total area.
No input (use r=1 circle, side=2 square, base=3 height=4 triangle).
Print the total area rounded to 2 decimal places.
Sample Input 1

                      
Sample Output 1
12.14
Sample Input 2

                      
Sample Output 2
12.14
Sample Input 3

                      
Sample Output 3
12.14
6
Overload __eq__ for Student
Hard
Write a class Student with name and marks, and overload __eq__ comparing by name.
First line: student 1 name and marks. Second line: student 2 name and marks.
Print 'Equal' if same name, else 'Different'.
Sample Input 1
Alice 80
Alice 90
Sample Output 1
Equal
Sample Input 2
A 10
B 20
Sample Output 2
Different
Sample Input 3
A 10
A 20
Sample Output 3
Equal
7
Abstract Polymorphism with ABC
Hard
Write an abstract class Shape with abstract method area(), and classes Circle and Rectangle implementing it.
A single line containing 'circle' or 'rectangle'.
Print the area rounded to 2 decimal places.
Sample Input 1
rectangle
Sample Output 1
20.00
Sample Input 2
circle
1
Sample Output 2
3.14
Sample Input 3
rectangle
5 4
Sample Output 3
20.00
8
Polymorphic Game Characters
Hard
Write a base class Character with attack() raising NotImplementedError, and classes Warrior and Mage overriding it.
A single line containing 'warrior' or 'mage'.
Print the attack description.
Sample Input 1
mage
Sample Output 1
Casts a fireball
Sample Input 2
warrior
Sample Output 2
Swing a sword
Sample Input 3
mage
Sample Output 3
Casts a fireball
9
Runtime Polymorphism via Inheritance
Hard
Write a base class Bank with rate() returning 0, and classes SBI and HDFC overriding it with 6.5 and 7.5.
A single line containing 'sbi' or 'hdfc'.
Print the interest rate.
Sample Input 1
hdfc
Sample Output 1
7.5
Sample Input 2
sbi
Sample Output 2
6.5
Sample Input 3
hdfc
Sample Output 3
7.5
10
Template + Polymorphism Combo
Hard
Write a class DataPipeline with run() that calls load() and process(), overridden by CSV and JSON pipelines.
A single line containing 'csv' or 'json'.
Print the two pipeline steps.
Sample Input 1
json
Sample Output 1
Load json
Process json
Sample Input 2
csv
Sample Output 2
Load csv
Process csv
Sample Input 3
json
Sample Output 3
Load json
Process json
Competitive MCQs — Polymorphism
Code snippets, output prediction, concepts & error spotting. Pick an answer to see instant feedback.
Score 0/ 34
Q1
What does polymorphism mean in OOP?
Correct!
Wrong — correct answer is .
Polymorphism lets one interface work with many types.
Q2
What is the output of this code?
python
1
2
3
4
5
6
7
8
class Cat:
    def sound(self):
        return "Meow"
class Dog:
    def sound(self):
        return "Woof"
for a in [Cat(), Dog()]:
    print(a.sound())
Correct!
Wrong — correct answer is .
Each object's own sound() runs — same call, different behaviour.
Q3
What is the output of this code?
class Cat:
def sound(self):
return "Meow"
class Dog:
def sound(self):
return "Woof"
for a in [Cat(), Dog()]:
print(a.sound())
Correct!
Wrong — correct answer is .
Each object's own sound() runs — same call, different behaviour.
Q4
Method overriding is an example of:
Correct!
Wrong — correct answer is .
Overriding resolves the method by the object's runtime type.
Q5
What is the output of this code?
python
1
2
3
4
5
6
7
8
9
class A:
    def f(self):
        return 1
class B(A):
    def f(self):
        return 2
a = A()
b = B()
print(a.f(), b.f())
Correct!
Wrong — correct answer is .
a.f() → 1 (A version), b.f() → 2 (B override).
Q6
What is the output of this code?
class A:
def f(self):
return 1
class B(A):
def f(self):
return 2
a = A()
b = B()
print(a.f(), b.f())
Correct!
Wrong — correct answer is .
a.f() → 1 (A version), b.f() → 2 (B override).
Q7
Method overloading in Python:
Correct!
Wrong — correct answer is .
Python doesn't support true overloading; redefining a method replaces it.
Q8
What is the output of this code?
python
1
print(len("hello"), len([1, 2, 3]))
Correct!
Wrong — correct answer is .
len() behaves polymorphically across strings, lists, etc.
Q9
What is the output of this code?
print(len("hello"), len([1, 2, 3]))
Correct!
Wrong — correct answer is .
len() behaves polymorphically across strings, lists, etc.
Q10
Which operator demonstrates polymorphism by working on numbers AND strings?
Correct!
Wrong — correct answer is .
+ adds numbers and concatenates strings/sequences.
Q11
Duck typing means:
Correct!
Wrong — correct answer is .
Python focuses on an object's behaviour rather than its declared type.
Q12
What is the output of this code?
python
1
2
3
4
5
6
7
8
9
10
11
class Shape:
    def area(self):
        return 0
class Square(Shape):
    def __init__(self, s):
        self.s = s
    def area(self):
        return self.s * self.s
shapes = [Shape(), Square(4)]
for s in shapes:
    print(s.area())
Correct!
Wrong — correct answer is .
The base returns 0; the override returns 4 × 4 = 16.
Q13
What is the output of this code?
class Shape:
def area(self):
return 0
class Square(Shape):
def __init__(self, s):
self.s = s
def area(self):
return self.s * self.s
shapes = [Shape(), Square(4)]
for s in shapes:
print(s.area())
Correct!
Wrong — correct answer is .
The base returns 0; the override returns 4 × 4 = 16.
Q14
Which built-in function shows polymorphism via different implementations of __len__?
Correct!
Wrong — correct answer is .
len() calls the object's __len__, which each class may define differently.
Q15
What is the output of this code?
class A:
def f(self):
return 1
class B(A):
def f(self):
return 2
x = A()
y = B()
print(x.f(), y.f())
Correct!
Wrong — correct answer is .
x uses A.f() and y uses B.f(), showing the same call producing different results.
Q16
Which type of polymorphism does method overriding show?
Correct!
Wrong — correct answer is .
The method chosen depends on the object's runtime type.
Q17
Which is an example of operator polymorphism?
Correct!
Wrong — correct answer is .
The + operator dispatches to the type's __add__, supporting many types.
Q18
What is the output of this code?
class Bird:
def move(self):
return "fly"
class Fish:
def move(self):
return "swim"
for a in [Bird(), Fish()]:
print(a.move())
Correct!
Wrong — correct answer is .
Each animal's own move() runs, demonstrating polymorphism.
Q19
What is duck typing?
Correct!
Wrong — correct answer is .
Python cares whether an object provides the needed methods, not its declared type.
Q20
What is the output of this code?
def call(x):
return x.speak()
class Dog:
def speak(self):
return "Woof"
class Cat:
def speak(self):
return "Meow"
print(call(Dog()), call(Cat()))
Correct!
Wrong — correct answer is .
call() works with any object that has speak(), the essence of duck typing.
Q21
Which built-in shows polymorphism through __str__?
Correct!
Wrong — correct answer is .
str(obj) and print() delegate to __str__ per object.
Q22
What is the output of this code?
class A:
def f(self):
return "A"
class B:
def f(self):
return "B"
print([x.f() for x in [A(), B()]])
Correct!
Wrong — correct answer is .
The list comprehension calls f() on each distinct object.
Q23
How does Python decide which method to run in a polymorphic call?
Correct!
Wrong — correct answer is .
Method lookup follows the object's class at runtime.
Q24
What is the output of this code?
class Shape:
def name(self):
return "Shape"
class Circle(Shape):
def name(self):
return "Circle"
shapes = [Shape(), Circle()]
for s in shapes:
print(s.name(), end=" ")
Correct!
Wrong — correct answer is .
Each object's name() reflects its actual type.
Q25
Which is an advantage of polymorphism?
Correct!
Wrong — correct answer is .
Polymorphic code extends to new classes that satisfy the same interface.
Q26
What is the output of this code?
def area(x):
return x * x
print(area(4), area(2.5))
Correct!
Wrong — correct answer is .
The * operator works for ints and floats, so area returns 16 and 6.25.
Q27
Which concept lets len() work on strings, lists, and dicts?
Correct!
Wrong — correct answer is .
Each type implements __len__ differently behind the same len() call.
Q28
What is the output of this code?
class A:
def f(self):
return 1
class B:
def f(self):
return 2
def g(obj):
return obj.f()
print(g(A()) + g(B()))
Correct!
Wrong — correct answer is .
g returns 1 for A() and 2 for B(), summing to 3.
Q29
Which statement about duck typing is TRUE?
Correct!
Wrong — correct answer is .
Duck typing cares about capabilities over class hierarchy.
Q30
What is the output of this code?
class A:
def f(self):
return "A"
class B(A):
def f(self):
return "B"
obj = B()
print(obj.f())
Correct!
Wrong — correct answer is .
Even through a subclass, the actual object B uses B's override.
Q31
Which operator dispatches to __add__?
Correct!
Wrong — correct answer is .
The + operator calls the left operand's __add__ method.
Q32
What is the output of this code?
class A:
def f(self):
return 1
class B(A):
def f(self):
return 2
def show(x):
print(x.f(), end=" ")
show(A())
show(B())
Correct!
Wrong — correct answer is .
show() dispatches f() polymorphically to each argument.
Q33
What is the output of this code?
print(len("abc"), len([1, 2]), len({1, 2, 3}))
Correct!
Wrong — correct answer is .
len() adapts to each collection's length via __len__.
Q34
Which is the key benefit of polymorphic functions?
Correct!
Wrong — correct answer is .
Polymorphic functions accept any object providing the required interface.