Challenge - 5 Problems
Polymorphism Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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())
Attempts:
2 left
💡 Hint
Each subclass overrides the speak method. The base class returns '...'.
✗ Incorrect
The Dog and Cat classes override the speak method to return their own sounds. The Animal base class returns '...'. The loop calls speak on each object, printing their respective outputs.
🧠 Conceptual
intermediate1:30remaining
Understanding polymorphism behavior
Which statement best describes polymorphism in the context of inheritance?
Attempts:
2 left
💡 Hint
Think about how subclasses can change behavior of inherited methods.
✗ Incorrect
Polymorphism allows subclasses to override methods from the parent class to provide different behavior while sharing the same method name.
🔧 Debug
advanced2: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())
Attempts:
2 left
💡 Hint
Check how the move method is defined in Car and how it is called.
✗ Incorrect
The Car class overrides move to require a speed argument. Calling c.move() without arguments causes a TypeError.
❓ Predict Output
advanced1: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())
Attempts:
2 left
💡 Hint
super() calls the parent class method.
✗ Incorrect
The Author class calls the parent write method using super(), then adds extra text. The output combines both strings.
🧠 Conceptual
expert2: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())
Attempts:
2 left
💡 Hint
Python uses method resolution order (MRO) from left to right in multiple inheritance.
✗ Incorrect
Class D inherits from B and C. Since B is first, its action method is called. This is Python's MRO behavior.