Challenge - 5 Problems
Master of Parent and Child Classes
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of method overriding in child class
What is the output of this code when calling
child.greet()?Python
class Parent: def greet(self): return "Hello from Parent" class Child(Parent): def greet(self): return "Hello from Child" child = Child() print(child.greet())
Attempts:
2 left
💡 Hint
Child class method with the same name replaces the parent's method.
✗ Incorrect
The child class overrides the greet method, so calling greet on a Child instance runs the Child's version.
❓ Predict Output
intermediate2:00remaining
Accessing parent method from child
What is the output of this code?
Python
class Parent: def greet(self): return "Hello from Parent" class Child(Parent): def greet(self): parent_greet = super().greet() return parent_greet + " and Child" child = Child() print(child.greet())
Attempts:
2 left
💡 Hint
super() calls the parent class method.
✗ Incorrect
The child calls the parent's greet method using super(), then adds its own text.
❓ Predict Output
advanced2:00remaining
Instance variable shadowing in child class
What is the output of this code?
Python
class Parent: def __init__(self): self.value = 10 class Child(Parent): def __init__(self): self.value = 20 obj = Child() print(obj.value)
Attempts:
2 left
💡 Hint
Child's __init__ replaces Parent's __init__ unless explicitly called.
✗ Incorrect
Child's __init__ sets value to 20 and does not call Parent's __init__, so value is 20.
❓ Predict Output
advanced2:00remaining
Calling parent constructor in child class
What is the output of this code?
Python
class Parent: def __init__(self): self.value = 10 class Child(Parent): def __init__(self): super().__init__() self.value = 20 obj = Child() print(obj.value)
Attempts:
2 left
💡 Hint
super().__init__() runs the parent's constructor first.
✗ Incorrect
Parent's __init__ sets value to 10, then Child sets it to 20, so final value is 20.
❓ Predict Output
expert2:00remaining
Multiple inheritance method resolution order (MRO)
What is the output of this code?
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 d = D() print(d.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 is checked first in MRO, so B's greet is called.