0
0
Pythonprogramming~20 mins

Polymorphism through inheritance in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Polymorphism Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of polymorphic method calls
What is the output of this Python code that uses polymorphism through inheritance?
Python
class Animal:
    def speak(self):
        return "..."

class Dog(Animal):
    def speak(self):
        return "Woof"

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

animals = [Dog(), Cat(), Animal()]
for a in animals:
    print(a.speak())
AWoof\nMeow\nNone
BNone\nNone\nNone
CWoof\nMeow\n...
DWoof\nMeow
Attempts:
2 left
💡 Hint
Each subclass overrides the speak method. The base class returns '...'.
🧠 Conceptual
intermediate
1:30remaining
Understanding polymorphism behavior
Which statement best describes polymorphism in the context of inheritance?
AA subclass cannot change any methods from the parent class.
BA subclass can have methods with the same name as the parent class, but different behavior.
CPolymorphism means creating multiple classes with no relation.
DInheritance prevents method overriding.
Attempts:
2 left
💡 Hint
Think about how subclasses can change behavior of inherited methods.
🔧 Debug
advanced
2:00remaining
Identify the error in polymorphic method call
What error will this code raise when run?
Python
class Vehicle:
    def move(self):
        return "Moving"

class Car(Vehicle):
    def move(self, speed):
        return f"Moving at {speed} km/h"

v = Vehicle()
c = Car()
print(v.move())
print(c.move())
ATypeError: move() missing 1 required positional argument: 'speed'
BAttributeError: 'Car' object has no attribute 'move'
CNo error, prints 'Moving' and 'Moving at km/h'
DSyntaxError: invalid syntax
Attempts:
2 left
💡 Hint
Check how the move method is defined in Car and how it is called.
Predict Output
advanced
1:30remaining
Output of polymorphic method with super()
What is the output of this code using super() in polymorphism?
Python
class Writer:
    def write(self):
        return "Writing text"

class Author(Writer):
    def write(self):
        base = super().write()
        return base + " and publishing books"

a = Author()
print(a.write())
AWriting text and publishing books
BWriting text
Cand publishing books
DAttributeError: 'super' object has no attribute 'write'
Attempts:
2 left
💡 Hint
super() calls the parent class method.
🧠 Conceptual
expert
2:30remaining
Polymorphism with multiple inheritance method resolution
Given these classes, what will be the output of calling c.action()?
Python
class A:
    def action(self):
        return "Action from A"

class B(A):
    def action(self):
        return "Action from B"

class C(A):
    def action(self):
        return "Action from C"

class D(B, C):
    pass

d = D()
print(d.action())
AAction from A
BAction from C
CTypeError: ambiguous method resolution
DAction from B
Attempts:
2 left
💡 Hint
Python uses method resolution order (MRO) from left to right in multiple inheritance.