0
0
Pythonprogramming~20 mins

Purpose of polymorphism in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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.