Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is polymorphism in programming?
Polymorphism means one thing can take many forms. It lets different objects be treated the same way even if they behave differently.
Click to reveal answer
beginner
Why do we use polymorphism?
We use polymorphism to write flexible and reusable code. It helps us use the same code for different types of objects.
Click to reveal answer
intermediate
How does polymorphism improve code maintenance?
Polymorphism allows changing or adding new object types without changing the code that uses them, making maintenance easier.
Click to reveal answer
beginner
Give a real-life example of polymorphism.
Think of a remote control that works with many devices like TV, DVD player, or stereo. The remote is the same but controls different devices differently.
Click to reveal answer
intermediate
What is method overriding in polymorphism?
Method overriding is when a child class changes a method from its parent class to do something different but keeps the same method name.
Click to reveal answer
What does polymorphism allow in programming?
AUsing the same interface for different data types
BWriting code only for one data type
CMaking code run faster
DPreventing code reuse
✗ Incorrect
Polymorphism lets you use the same interface or method name for different types of objects.
Which of these is an example of polymorphism?
AUsing different variable names for each animal
BDifferent animals making different sounds using the same method name 'make_sound()'
CWriting separate code for each animal's sound
DUsing only one animal in the program
✗ Incorrect
Different animals can have a 'make_sound()' method that works differently, showing polymorphism.
How does polymorphism help with code maintenance?
ABy allowing new object types without changing existing code
BBy making code longer
CBy removing all comments
DBy duplicating code
✗ Incorrect
Polymorphism lets you add new types without changing the code that uses them, making maintenance easier.
What is method overriding?
ACalling a method from outside the class
BDeleting a method from a class
CCreating a new method with a different name
DChanging a method in a child class while keeping the same name
✗ Incorrect
Method overriding means a child class changes a parent's method but keeps the same method name.
Which statement about polymorphism is true?
AIt is unrelated to object-oriented programming
BIt restricts objects to one form only
CIt allows one interface to control access to different underlying forms
DIt makes code harder to read
✗ Incorrect
Polymorphism allows one interface to work with different underlying forms or data types.
Explain in your own words why polymorphism is useful in programming.
Think about how one tool can work with many things.
You got /4 concepts.
Describe a simple real-life example that shows the idea of polymorphism.
Think about something you use that works with many devices or things.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of polymorphism in Python programming?
easy
A. To allow one function or method to work in different ways depending on the object
B. To make the program run faster by using multiple processors
C. To store multiple values in a single variable
D. To create a new data type from existing types
Solution
Step 1: Understand the meaning of polymorphism
Polymorphism means one action can behave differently depending on the object it is acting on.
Step 2: Match the purpose with the options
To allow one function or method to work in different ways depending on the object correctly describes this behavior, while others describe unrelated concepts.
Final Answer:
To allow one function or method to work in different ways depending on the object -> Option A
Quick Check:
Polymorphism = One action, many behaviors [OK]
Hint: Polymorphism means same name, different actions [OK]
Common Mistakes:
Confusing polymorphism with speed optimization
Thinking polymorphism is about storing multiple values
Mixing polymorphism with data type creation
2. Which of the following is the correct way to demonstrate polymorphism with methods in Python?
easy
A. Define multiple methods with different names in the same class
B. Define methods with the same name in different classes and call them on their objects
C. Use only one method in one class without overriding
D. Use global variables to change method behavior
Solution
Step 1: Recall how polymorphism works with methods
Polymorphism allows methods with the same name to behave differently in different classes.
Step 2: Check which option matches this behavior
Define methods with the same name in different classes and call them on their objects correctly describes defining same-named methods in different classes and calling them on their objects.
Final Answer:
Define methods with the same name in different classes and call them on their objects -> Option B
Quick Check:
Same method name, different classes = polymorphism [OK]
Hint: Same method name in different classes shows polymorphism [OK]
Common Mistakes:
Thinking polymorphism means different method names
Ignoring method overriding in subclasses
Using global variables to control method behavior
3. What will be the output of the following code?
class Dog:
def sound(self):
return "Bark"
class Cat:
def sound(self):
return "Meow"
animals = [Dog(), Cat()]
for animal in animals:
print(animal.sound())
medium
A. Meow
Bark
B. Bark
Bark
C. Error: sound method not found
D. Bark
Meow
Solution
Step 1: Understand the classes and their methods
Dog and Cat classes both have a method named sound that returns different strings.
Step 2: Trace the loop calling sound on each object
The loop calls sound() on Dog instance (returns "Bark") and Cat instance (returns "Meow"), printing each.
Final Answer:
Bark
Meow -> Option D
Quick Check:
Different classes, same method name, different outputs [OK]
Hint: Same method name, different classes, different outputs [OK]
Common Mistakes:
Assuming both calls return the same string
Expecting a runtime error due to method name
Mixing the order of outputs
4. Find the error in this code that tries to use polymorphism:
class Bird:
def fly(self):
print("Flying")
class Penguin(Bird):
def fly(self):
print("Cannot fly")
p = Penguin()
p.fly()
medium
A. No error; code correctly uses polymorphism
B. Penguin class must call super().fly() inside fly
C. Method fly must return a value
D. Penguin class should not override fly method
Solution
Step 1: Check method overriding in subclass
Penguin overrides fly method to print "Cannot fly", which is valid polymorphism.
Step 2: Verify code execution
Creating Penguin object and calling fly prints "Cannot fly" without error.
Final Answer:
No error; code correctly uses polymorphism -> Option A
Quick Check:
Overriding method in subclass is correct polymorphism [OK]
Hint: Overriding method in subclass is allowed [OK]
Common Mistakes:
Thinking overriding is an error
Expecting method must return a value
Believing super() call is mandatory
5. You want to write a function that accepts any object and calls its draw() method, regardless of the object's class. Which concept does this best illustrate?
hard
A. Inheritance
B. Encapsulation
C. Polymorphism
D. Abstraction
Solution
Step 1: Understand the function requirement
The function calls draw() on any object without knowing its class.
Step 2: Identify the concept allowing this behavior
Polymorphism allows different objects to respond to the same method call appropriately.
Final Answer:
Polymorphism -> Option C
Quick Check:
Same method call, different objects = polymorphism [OK]
Hint: Calling same method on any object shows polymorphism [OK]
Common Mistakes:
Confusing with inheritance which is about class hierarchy