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 with super()
What is the output of this Python code?
Python
class Parent: def greet(self): return "Hello from Parent" class Child(Parent): def greet(self): return super().greet() + " and Child" obj = Child() print(obj.greet())
Attempts:
2 left
💡 Hint
Think about how super() calls the parent method and how the returned string is combined.
✗ Incorrect
The Child class overrides greet but calls the Parent's greet using super(). The returned string from Parent is combined with ' and Child', so the final output is 'Hello from Parent and Child'.
❓ Predict Output
intermediate2:00remaining
Output when overriding without calling super()
What will this code print?
Python
class Base: def message(self): return "Base message" class Derived(Base): def message(self): return "Derived message" obj = Derived() print(obj.message())
Attempts:
2 left
💡 Hint
The Derived class replaces the Base method without calling it.
✗ Incorrect
Since Derived overrides message and does not call super(), the method from Derived is used, printing 'Derived message'.
❓ Predict Output
advanced2:30remaining
Output with multiple inheritance and method overriding
What is the output of this code?
Python
class A: def speak(self): return "A speaks" class B(A): def speak(self): return "B speaks" class C(A): def speak(self): return "C speaks" class D(B, C): pass obj = D() print(obj.speak())
Attempts:
2 left
💡 Hint
Check the method resolution order (MRO) for class D.
✗ Incorrect
Class D inherits from B and C. Python uses MRO and finds speak in B first, so 'B speaks' is printed.
❓ Predict Output
advanced2:00remaining
Output when overriding a method and changing return type
What will this code print?
Python
class Parent: def get_value(self): return 10 class Child(Parent): def get_value(self): return "Ten" obj = Child() print(obj.get_value())
Attempts:
2 left
💡 Hint
The Child method returns a string instead of an integer.
✗ Incorrect
The Child class overrides get_value and returns the string 'Ten'. Python allows this change in return type, so 'Ten' is printed.
❓ Predict Output
expert2:30remaining
Output with method overriding and attribute access
What is the output of this code?
Python
class Parent: def __init__(self): self.value = 5 def get_value(self): return self.value class Child(Parent): def __init__(self): self.value = 10 def get_value(self): return super().get_value() + 5 obj = Child() print(obj.get_value())
Attempts:
2 left
💡 Hint
Consider which __init__ method runs and what self.value is when super().get_value() is called.
✗ Incorrect
Child's __init__ does not call Parent's __init__, so Parent's self.value is never set. Child sets self.value to 10. But super().get_value() uses Parent's method which accesses self.value on the Child instance, which is 10. So super().get_value() returns 10, then +5 makes 15. However, since Parent's __init__ is not called, self.value is set by Child to 10. So output is 15.