Bird
Raised Fist0
Pythonprogramming~20 mins

Polymorphism through inheritance 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 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.

Practice

(1/5)
1. What does polymorphism through inheritance allow in Python?
easy
A. One method name to have different behaviors in child classes
B. Multiple inheritance from unrelated classes
C. Using the same variable name in different functions
D. Creating objects without defining classes

Solution

  1. Step 1: Understand polymorphism concept

    Polymorphism means one method name can behave differently depending on the class.
  2. Step 2: Relate to inheritance

    Child classes override the method to provide their own behavior.
  3. Final Answer:

    One method name to have different behaviors in child classes -> Option A
  4. Quick Check:

    Polymorphism = One method, many behaviors [OK]
Hint: Polymorphism means same method, different actions [OK]
Common Mistakes:
  • Confusing polymorphism with multiple inheritance
  • Thinking polymorphism means same variable names
  • Believing objects can exist without classes
2. Which of the following is the correct way to override a method in a child class?
easy
A. Call the parent method inside the child method without redefining
B. Use the keyword override before the method
C. Define a method with the same name in the child class
D. Rename the method in the child class

Solution

  1. Step 1: Check method overriding syntax

    In Python, overriding means defining a method with the same name in the child class.
  2. Step 2: Verify other options

    Python does not use override keyword; renaming is not overriding; calling parent method alone is not overriding.
  3. Final Answer:

    Define a method with the same name in the child class -> Option C
  4. Quick Check:

    Override = same method name in child [OK]
Hint: Override by redefining method name in child class [OK]
Common Mistakes:
  • Using a non-existent override keyword
  • Thinking calling parent method equals overriding
  • Renaming method instead of overriding
3. What will be the output of this code?
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 a in animals:
    print(a.sound())
medium
A. Bark\nMeow\nSome sound
B. Some sound\nSome sound\nSome sound
C. Bark\nMeow\nError
D. Error\nError\nError

Solution

  1. Step 1: Identify method overriding

    Dog and Cat classes override sound method to return "Bark" and "Meow" respectively.
  2. Step 2: Trace the loop output

    Loop calls sound() on Dog(), Cat(), and Animal() objects, printing "Bark", "Meow", and "Some sound".
  3. Final Answer:

    Bark Meow Some sound -> Option A
  4. Quick Check:

    Overridden methods print their own sounds [OK]
Hint: Child method overrides parent, prints child's return [OK]
Common Mistakes:
  • Assuming parent method always runs
  • Expecting errors from calling base class method
  • Mixing output order
4. Find the error in this code that tries to demonstrate polymorphism:
class Vehicle:
    def move(self):
        print("Moving")

class Car(Vehicle):
    def move(self):
        print("Driving")

class Bike(Vehicle):
    def move(self):
        print("Riding")

vehicles = [Car(), Bike()]
for v in vehicles:
    v.move
medium
A. List should include Vehicle() instance
B. Incorrect class inheritance syntax
C. Using print instead of return in methods
D. Missing parentheses when calling move method

Solution

  1. Step 1: Check method calls in loop

    The code uses v.move without parentheses, so method is not called.
  2. Step 2: Understand effect of missing parentheses

    Without parentheses, method object is referenced but not executed, so no output occurs.
  3. Final Answer:

    Missing parentheses when calling move method -> Option D
  4. Quick Check:

    Method call needs () to execute [OK]
Hint: Always add () to call methods [OK]
Common Mistakes:
  • Forgetting parentheses on method calls
  • Thinking print vs return causes error here
  • Believing inheritance syntax is wrong
5. You want to add a new class Bird that also uses polymorphism with sound(). Which code correctly extends the existing classes and uses polymorphism?
hard
A. class Bird: def sound(self): return "Chirp" animals.append(Bird()) for a in animals: print(a.sound())
B. class Bird(Animal): def sound(self): return "Chirp" animals.append(Bird()) for a in animals: print(a.sound())
C. class Bird(Animal): def noise(self): return "Chirp" animals.append(Bird()) for a in animals: print(a.sound())
D. class Bird(Animal): def sound(self): print("Chirp") animals.append(Bird()) for a in animals: print(a.sound())

Solution

  1. Step 1: Check inheritance and method name

    Bird must inherit from Animal and override sound() method to maintain polymorphism.
  2. Step 2: Verify method behavior and usage

    Method returns string "Chirp" like others; appending Bird() to animals list and calling sound() works correctly.
  3. Final Answer:

    Correctly inherits Animal and overrides sound() returning "Chirp" -> Option B
  4. Quick Check:

    Polymorphism needs same method name and inheritance [OK]
Hint: Inherit and override same method name with matching signature [OK]
Common Mistakes:
  • Not inheriting from Animal class
  • Using different method name like noise()
  • Printing inside method instead of returning