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'.
Input Format
No input.
Output Format
Print 'Bark'.
Sample Test Cases
|
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().
Input Format
No input.
Output Format
Print 'Meow' and 'Woof' on separate lines.
Sample Test Cases
|
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).
Input Format
No input.
Output Format
Print both areas on separate lines.
Sample Test Cases
|
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.
Input Format
No input.
Output Format
Print 'Quack quack' and 'Person quacking' on separate lines.
Sample Test Cases
|
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.
Input Format
A single line containing 'circle' or 'square'.
Output Format
Print the shape description.
Sample Test Cases
|
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.
Input Format
A single line containing integer n.
Output Format
Print the repeated string.
Sample Test Cases
|
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.
Input Format
No input.
Output Format
Print both move descriptions.
Sample Test Cases
|
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.
Input Format
No input.
Output Format
Print 5 and 3 on separate lines.
Sample Test Cases
|
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.
Input Format
No input.
Output Format
Print 120.
Sample Test Cases
|
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.
Input Format
No input.
Output Format
Print 20.
Sample Test Cases
|
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.
Input Format
No input.
Output Format
Print 20 and 15 on separate lines.
Sample Test Cases
|
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.
Input Format
First line: two integers. Second line: three integers.
Output Format
Print the two results on separate lines.
Sample Test Cases
|
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.
Input Format
No input.
Output Format
Print 'Bark' and 'Quack' on separate lines.
Sample Test Cases
|
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().
Input Format
A single line containing 'card' or 'upi'.
Output Format
Print the payment confirmation.
Sample Test Cases
|
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]'.
Input Format
A single line containing a book title.
Output Format
Print the string representation.
Sample Test Cases
|
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.
Input Format
First line: two strings. Second line: two lists.
Output Format
Print both totals on separate lines.
Sample Test Cases
|
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().
Input Format
A single line containing 'circle' or 'rectangle'.
Output Format
Print the shape type and its area.
Sample Test Cases
|
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'.
Input Format
No input.
Output Format
Print both lines.
Sample Test Cases
|
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.
Input Format
No input.
Output Format
Print 'Bark', 'Meow', and 'Moo' on separate lines.
Sample Test Cases
|
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.
Input Format
First line: name and marks of student 1. Second line: name and marks of student 2.
Output Format
Print the name of the higher-scoring student.
Sample Test Cases
|
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.
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
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).
Input Format
First line: x1 y1. Second line: x2 y2.
Output Format
Print the resulting vector as (x, y).
Sample Test Cases
|
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.
Input Format
First line: point 1. Second line: point 2.
Output Format
Print 'P1 is closer' or 'P2 is closer'.
Sample Test Cases
|
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().
Input Format
A single line containing 'email' or 'sms'.
Output Format
Print the notification message.
Sample Test Cases
|
Sample Input 1
|
Sample Output 1
Sending email... |
|
Sample Input 2
sms |
Sample Output 2
Sending SMS... |
|
Sample Input 3
|
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.
Input Format
No input (use r=1 circle, side=2 square, base=3 height=4 triangle).
Output Format
Print the total area rounded to 2 decimal places.
Sample Test Cases
|
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.
Input Format
First line: student 1 name and marks. Second line: student 2 name and marks.
Output Format
Print 'Equal' if same name, else 'Different'.
Sample Test Cases
|
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.
Input Format
A single line containing 'circle' or 'rectangle'.
Output Format
Print the area rounded to 2 decimal places.
Sample Test Cases
|
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.
Input Format
A single line containing 'warrior' or 'mage'.
Output Format
Print the attack description.
Sample Test Cases
|
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.
Input Format
A single line containing 'sbi' or 'hdfc'.
Output Format
Print the interest rate.
Sample Test Cases
|
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.
Input Format
A single line containing 'csv' or 'json'.
Output Format
Print the two pipeline steps.
Sample Test Cases
|
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 |