Challenge - 5 Problems
Master of Extending Parent Behavior
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of extended method with super() call
What is the output of this Python code when calling
Child().greet()?Python
class Parent: def greet(self): return "Hello from Parent" class Child(Parent): def greet(self): parent_msg = super().greet() return parent_msg + " and Child" print(Child().greet())
Attempts:
2 left
💡 Hint
Think about how
super() calls the parent method and how the strings are combined.✗ Incorrect
The
Child class calls super().greet() which returns "Hello from Parent". Then it adds " and Child" to that string, so the final output is "Hello from Parent and Child".❓ Predict Output
intermediate2:00remaining
Output when parent method is not called
What will be printed when running this code?
Python
class Parent: def action(self): print("Parent action") class Child(Parent): def action(self): print("Child action") obj = Child() obj.action()
Attempts:
2 left
💡 Hint
Check which method is called when the child overrides the parent method without calling super().
✗ Incorrect
The
Child class overrides the action method and does not call super(). So only "Child action" is printed.🔧 Debug
advanced2:00remaining
Why does this code raise an error?
This code tries to extend the parent method but raises an error. What is the cause?
Python
class Parent: def calculate(self, x): return x * 2 class Child(Parent): def calculate(self, x): return super().calculate(x) + 3 print(Child().calculate(4))
Attempts:
2 left
💡 Hint
Check how
super() is called in Python 3.✗ Incorrect
In Python 3,
super() must be called with parentheses to get the super object. Using super.calculate(x) causes an AttributeError because super is a built-in function, not an object.❓ Predict Output
advanced2:00remaining
Output of multiple inheritance with super()
What is the output of this code?
Python
class A: def process(self): return "A" class B(A): def process(self): return super().process() + "B" class C(A): def process(self): return super().process() + "C" class D(B, C): def process(self): return super().process() + "D" print(D().process())
Attempts:
2 left
💡 Hint
Remember Python uses Method Resolution Order (MRO) in multiple inheritance.
✗ Incorrect
The MRO for class D is D -> B -> C -> A. Each
super().process() calls the next in line: D calls B, B calls C, C calls A, resulting in "A" (A) + "C" (C) + "B" (B) + "D" (D) = "ACBD".🧠 Conceptual
expert2:00remaining
Why use super() in method overriding?
Which of the following best explains why we use
super() when overriding a method in a child class?Attempts:
2 left
💡 Hint
Think about how inheritance helps reuse code.
✗ Incorrect
Using
super() allows the child class to run the parent class method, then add or change behavior, so we don't have to rewrite the parent's code.