Challenge - 5 Problems
Inheritance Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of inherited method call
What is the output of this Python code that uses inheritance?
Python
class Animal: def speak(self): return "I make a sound" class Dog(Animal): pass pet = Dog() print(pet.speak())
Attempts:
2 left
💡 Hint
The Dog class does not override the speak method.
✗ Incorrect
Since Dog inherits from Animal and does not override speak, calling speak on Dog instance uses Animal's speak method, returning "I make a sound".
❓ Predict Output
intermediate2:00remaining
Value of inherited attribute after modification
What is the value of pet.color after running this code?
Python
class Animal: def __init__(self): self.color = "brown" class Dog(Animal): def __init__(self): super().__init__() self.color = "black" pet = Dog()
Attempts:
2 left
💡 Hint
The Dog class calls super().__init__() then changes color.
✗ Incorrect
Dog's __init__ calls Animal's __init__ setting color to "brown", then immediately sets color to "black". So pet.color is "black".
🔧 Debug
advanced2:00remaining
Identify the error in method overriding
What error does this code raise when executed?
Python
class Animal: def speak(self): return "sound" class Dog(Animal): def speak(): return "bark" pet = Dog() print(pet.speak())
Attempts:
2 left
💡 Hint
Check the method signature of speak in Dog.
✗ Incorrect
Dog's speak method lacks the self parameter, so calling pet.speak() passes self implicitly but method expects none, causing TypeError.
❓ Predict Output
advanced2:00remaining
Output of method overriding with super()
What is printed when this code runs?
Python
class Animal: def speak(self): return "sound" class Dog(Animal): def speak(self): return super().speak() + " and bark" pet = Dog() print(pet.speak())
Attempts:
2 left
💡 Hint
super() calls the parent class method.
✗ Incorrect
Dog's speak calls Animal's speak via super(), which returns "sound", then adds " and bark". So output is "sound and bark".
🧠 Conceptual
expert2:00remaining
Number of attributes in instance after inheritance and modification
Given the code below, how many attributes does the instance 'pet' have?
Python
class Animal: def __init__(self): self.legs = 4 self.color = "brown" class Dog(Animal): def __init__(self): super().__init__() self.color = "black" self.tail = True pet = Dog()
Attempts:
2 left
💡 Hint
Count all attributes set on pet after Dog.__init__ runs.
✗ Incorrect
pet has legs (from Animal), color (overwritten in Dog), and tail (added in Dog), total 3 attributes.