Bird
Raised Fist0
Pythonprogramming~20 mins

Purpose of polymorphism in Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
Polymorphism Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Polymorphism Purpose

What is the main purpose of polymorphism in programming?

ATo allow objects of different classes to be treated as objects of a common superclass
BTo increase the speed of program execution by using multiple processors
CTo store multiple values in a single variable
DTo prevent any changes to the data once it is set
Attempts:
2 left
💡 Hint

Think about how different objects can share the same interface or method names.

Predict Output
intermediate
2:00remaining
Output of Polymorphism Example

What is the output of this Python code demonstrating polymorphism?

Python
class Animal:
    def sound(self):
        return "Some sound"

class Dog(Animal):
    def sound(self):
        return "Bark"

class Cat(Animal):
    def sound(self):
        return "Meow"

animals = [Dog(), Cat(), Animal()]
for animal in animals:
    print(animal.sound())
A
Some sound
Some sound
Some sound
B
Bark
Bark
Bark
C
Bark
Meow
Some sound
D
Meow
Bark
Some sound
Attempts:
2 left
💡 Hint

Each subclass overrides the sound method.

🔧 Debug
advanced
2:00remaining
Identify the Polymorphism Error

What error will this code produce related to polymorphism?

Python
class Bird:
    def fly(self):
        print("Flying")

class Penguin(Bird):
    pass

p = Penguin()
p.fly()
ANo error, prints 'Flying'
BTypeError: Cannot call fly on Penguin
CAttributeError: 'Penguin' object has no attribute 'fly'
DNameError: Bird is not defined
Attempts:
2 left
💡 Hint

Check if Penguin inherits the method from Bird.

📝 Syntax
advanced
2:00remaining
Correct Polymorphic Method Syntax

Which option correctly defines a polymorphic method area in two classes?

Python
class Shape:
    def area(self):
        pass

class Square(Shape):
    def __init__(self, side):
        self.side = side

    # Define area method here

class Circle(Shape):
    def __init__(self, radius):
        self.radius = radius

    # Define area method here
A
Square: def area(self): return self.side + self.side
Circle: def area(self): return 3.14 * self.radius
B
Square: def area(): return side * side
Circle: def area(): return 3.14 * radius ** 2
C
Square: def area(self): return side * side
Circle: def area(self): return 3.14 * radius ** 2
D
Square: def area(self): return self.side * self.side
Circle: def area(self): return 3.14 * self.radius ** 2
Attempts:
2 left
💡 Hint

Remember to use self to access instance variables.

🚀 Application
expert
2:00remaining
Polymorphism in Function Arguments

What will be the output of this code that uses polymorphism in function arguments?

Python
class Printer:
    def print_message(self):
        print("Generic message")

class EmailPrinter(Printer):
    def print_message(self):
        print("Email message")

class SMSPrinter(Printer):
    def print_message(self):
        print("SMS message")

def send_notification(printer: Printer):
    printer.print_message()

send_notification(EmailPrinter())
send_notification(SMSPrinter())
send_notification(Printer())
A
SMS message
Email message
Generic message
B
Email message
SMS message
Generic message
C
Email message
Email message
Email message
D
Generic message
Generic message
Generic message
Attempts:
2 left
💡 Hint

Each subclass overrides print_message. The function calls the method on the passed object.

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

  1. Step 1: Understand the meaning of polymorphism

    Polymorphism means one action can behave differently depending on the object it is acting on.
  2. 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.
  3. Final Answer:

    To allow one function or method to work in different ways depending on the object -> Option A
  4. 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

  1. Step 1: Recall how polymorphism works with methods

    Polymorphism allows methods with the same name to behave differently in different classes.
  2. 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.
  3. Final Answer:

    Define methods with the same name in different classes and call them on their objects -> Option B
  4. 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

  1. Step 1: Understand the classes and their methods

    Dog and Cat classes both have a method named sound that returns different strings.
  2. 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.
  3. Final Answer:

    Bark Meow -> Option D
  4. 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

  1. Step 1: Check method overriding in subclass

    Penguin overrides fly method to print "Cannot fly", which is valid polymorphism.
  2. Step 2: Verify code execution

    Creating Penguin object and calling fly prints "Cannot fly" without error.
  3. Final Answer:

    No error; code correctly uses polymorphism -> Option A
  4. 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

  1. Step 1: Understand the function requirement

    The function calls draw() on any object without knowing its class.
  2. Step 2: Identify the concept allowing this behavior

    Polymorphism allows different objects to respond to the same method call appropriately.
  3. Final Answer:

    Polymorphism -> Option C
  4. 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
  • Mixing with encapsulation which hides data
  • Thinking abstraction means calling any method