Bird
Raised Fist0
Pythonprogramming~20 mins

Method overriding 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
🎖️
Method Overriding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of method overriding in inheritance
What is the output of this Python code that uses method overriding?
Python
class Animal:
    def sound(self):
        return "Some sound"

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

pet = Dog()
print(pet.sound())
A"Bark"
B"Some sound"
CNone
DTypeError
Attempts:
2 left
💡 Hint
Look at which class's method is called when you create a Dog object.
Predict Output
intermediate
2:00remaining
Output when calling overridden method with super()
What will this code print when calling the speak method?
Python
class Parent:
    def speak(self):
        return "Hello from Parent"

class Child(Parent):
    def speak(self):
        return super().speak() + " and Child"

obj = Child()
print(obj.speak())
A"Hello from Parent"
BAttributeError
C"and Child"
D"Hello from Parent and Child"
Attempts:
2 left
💡 Hint
super() calls the method from the parent class.
Predict Output
advanced
2:00remaining
Output of multiple inheritance with method overriding
What is the output of this code using multiple inheritance and method overriding?
Python
class A:
    def greet(self):
        return "Hello from A"

class B(A):
    def greet(self):
        return "Hello from B"

class C(A):
    def greet(self):
        return "Hello from C"

class D(B, C):
    pass

obj = D()
print(obj.greet())
A"Hello from B"
BTypeError
C"Hello from C"
D"Hello from A"
Attempts:
2 left
💡 Hint
Python uses the method resolution order (MRO) to decide which method to call.
Predict Output
advanced
2:00remaining
Output when overriding method with different parameters
What will this code print?
Python
class Base:
    def info(self):
        return "Base info"

class Derived(Base):
    def info(self, detail):
        return f"Derived info: {detail}"

obj = Derived()
print(obj.info())
A"Base info"
B"Derived info: None"
CTypeError
DAttributeError
Attempts:
2 left
💡 Hint
Check if the method call matches the method signature in Derived.
🧠 Conceptual
expert
3:00remaining
Understanding method overriding behavior with super() and multiple inheritance
Given the following classes, what is the output of calling obj.action()?
Python
class X:
    def action(self):
        return "X"

class Y(X):
    def action(self):
        return "Y" + super().action()

class Z(X):
    def action(self):
        return "Z" + super().action()

class W(Y, Z):
    def action(self):
        return "W" + super().action()

obj = W()
print(obj.action())
A"WZYX"
B"WYZX"
C"WXYZ"
D"WXYZX"
Attempts:
2 left
💡 Hint
Check the method resolution order (MRO) for class W and how super() calls chain.

Practice

(1/5)
1.

What is method overriding in Python?

Choose the best description.

easy
A. A child class provides a new version of a method from its parent class.
B. A method that is called automatically when an object is created.
C. A method that cannot be changed once defined in a class.
D. A method that is only accessible inside the class it is defined.

Solution

  1. Step 1: Understand method overriding concept

    Method overriding means a child class changes a method from its parent by defining a method with the same name.
  2. Step 2: Match description to concept

    A child class provides a new version of a method from its parent class. correctly describes this behavior, while others describe different concepts.
  3. Final Answer:

    A child class provides a new version of a method from its parent class. -> Option A
  4. Quick Check:

    Method overriding = child changes parent method [OK]
Hint: Child class method with same name replaces parent method [OK]
Common Mistakes:
  • Confusing overriding with overloading
  • Thinking overriding means creating a new method
  • Believing methods cannot be changed in child classes
2.

Which of the following is the correct way to override a method greet in a child class?

class Parent:
    def greet(self):
        print("Hello from Parent")

class Child(Parent):
    # What goes here?
easy
A. def greet(): print("Hello from Child")
B. def greet(self): print("Hello from Child")
C. def greet(self, name): print(f"Hello {name} from Child")
D. def greet(self): return "Hello from Child"

Solution

  1. Step 1: Check method signature for overriding

    The child method must have the same name and parameters as the parent method to override it properly.
  2. Step 2: Compare options

    def greet(self): print("Hello from Child") matches the parent's method signature exactly. def greet(): print("Hello from Child") misses 'self', C adds a parameter, and D returns a string instead of printing.
  3. Final Answer:

    def greet(self):\n print("Hello from Child") -> Option B
  4. Quick Check:

    Same method name and parameters = correct override [OK]
Hint: Match method name and parameters exactly to override [OK]
Common Mistakes:
  • Omitting 'self' parameter in method
  • Changing method parameters when overriding
  • Returning value instead of matching parent's behavior
3.

What will be the output of the following code?

class Animal:
    def sound(self):
        print("Some sound")

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

pet = Dog()
pet.sound()
medium
A. Some sound
B. None
C. Error: Method not found
D. Bark

Solution

  1. Step 1: Identify method overriding in Dog class

    Dog class overrides the sound() method from Animal to print "Bark" instead of "Some sound".
  2. Step 2: Check which method is called

    When pet.sound() is called, it uses Dog's version, printing "Bark".
  3. Final Answer:

    Bark -> Option D
  4. Quick Check:

    Child method called = overridden output [OK]
Hint: Child method replaces parent method output [OK]
Common Mistakes:
  • Expecting parent method output instead of child's
  • Thinking method call causes error
  • Confusing print output with return value
4.

Find the error in this code that tries to override a method:

class Vehicle:
    def start(self):
        print("Vehicle started")

class Car(Vehicle):
    def start():
        print("Car started")

c = Car()
c.start()
medium
A. Missing 'self' parameter in Car's start method
B. Car class should not inherit Vehicle
C. Parent method start() is private and cannot be overridden
D. Calling start() without parentheses

Solution

  1. Step 1: Check method signature in child class

    Car's start method is missing the 'self' parameter, which is required for instance methods.
  2. Step 2: Understand impact of missing 'self'

    Without 'self', Python treats start as a static method, causing a TypeError when called on an instance.
  3. Final Answer:

    Missing 'self' parameter in Car's start method -> Option A
  4. Quick Check:

    Instance methods need 'self' parameter [OK]
Hint: Always include 'self' as first parameter in instance methods [OK]
Common Mistakes:
  • Forgetting 'self' in child method
  • Thinking inheritance causes error
  • Misunderstanding method call syntax
5.

Given the classes below, what will be the output when c.describe() is called?

class Parent:
    def describe(self):
        print("Parent description")

class Child(Parent):
    def describe(self):
        print("Child description")
        super().describe()

c = Child()
c.describe()
hard
A. Child description
B. Parent description\nChild description
C. Child description\nParent description
D. Error: super() used incorrectly

Solution

  1. Step 1: Understand method overriding with super()

    Child's describe() overrides Parent's but calls super().describe() to run parent's method too.
  2. Step 2: Trace method calls

    Calling c.describe() prints "Child description" first, then calls Parent's describe() printing "Parent description".
  3. Final Answer:

    Child description\nParent description -> Option C
  4. Quick Check:

    super() calls parent method after child override [OK]
Hint: super() calls parent method inside child override [OK]
Common Mistakes:
  • Expecting only child's print output
  • Thinking super() causes error
  • Confusing order of prints