Challenge - 5 Problems
Method Overriding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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())
Attempts:
2 left
💡 Hint
Look at which class's method is called when you create a Dog object.
✗ Incorrect
The Dog class overrides the sound method of Animal. So calling sound on a Dog instance returns "Bark".
❓ Predict Output
intermediate2: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())
Attempts:
2 left
💡 Hint
super() calls the method from the parent class.
✗ Incorrect
The Child's speak method calls the Parent's speak using super(), then adds " and Child".
❓ Predict Output
advanced2: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())
Attempts:
2 left
💡 Hint
Python uses the method resolution order (MRO) to decide which method to call.
✗ Incorrect
Class D inherits from B and C. B's greet overrides A's. D uses B's greet because B is before C in inheritance.
❓ Predict Output
advanced2: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())
Attempts:
2 left
💡 Hint
Check if the method call matches the method signature in Derived.
✗ Incorrect
Derived's info requires one argument, but obj.info() is called without arguments, causing TypeError.
🧠 Conceptual
expert3: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())
Attempts:
2 left
💡 Hint
Check the method resolution order (MRO) for class W and how super() calls chain.
✗ Incorrect
The MRO for W is W -> Y -> Z -> X. Each action calls super().action(), so the output concatenates in order: W + Y + Z + X = "WYZX".