0
0
Pythonprogramming~20 mins

Method overriding in Python - Practice Problems & Coding Challenges

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