Challenge - 5 Problems
Inheritance Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this inheritance example?
Look at the code below. What will it print when run?
Python
class Animal: def speak(self): return "I make a sound" class Dog(Animal): def speak(self): return "Woof!" pet = Dog() print(pet.speak())
Attempts:
2 left
💡 Hint
Remember, the Dog class changes the speak method.
✗ Incorrect
The Dog class inherits from Animal but replaces the speak method. So calling speak on a Dog object runs Dog's version, printing "Woof!".
🧠 Conceptual
intermediate1:30remaining
Why use inheritance in programming?
Which of these is the main purpose of inheritance?
Attempts:
2 left
💡 Hint
Think about how inheritance helps avoid repeating code.
✗ Incorrect
Inheritance allows one class to use code from another, making it easier to share and extend behavior without rewriting code.
❓ Predict Output
advanced2:00remaining
What is the output of this multi-level inheritance?
What will this code print?
Python
class A: def greet(self): return "Hello from A" class B(A): def greet(self): return "Hello from B" class C(B): pass obj = C() print(obj.greet())
Attempts:
2 left
💡 Hint
Check which class defines greet and which classes inherit it.
✗ Incorrect
Class C inherits from B, which overrides greet from A. Since C does not override greet, it uses B's version.
🔧 Debug
advanced2:00remaining
Which option causes an error in inheritance?
Which code snippet will cause an error when run?
Attempts:
2 left
💡 Hint
Look at how the Child class initializes value and if it calls the Parent constructor.
✗ Incorrect
Option C's Child __init__ does not call Parent's __init__, so self.value is never set. This causes AttributeError when accessing obj.value.
🧠 Conceptual
expert1:30remaining
What is the main benefit of method overriding in inheritance?
Why do programmers override methods in child classes?
Attempts:
2 left
💡 Hint
Think about how child classes customize behavior.
✗ Incorrect
Overriding lets child classes provide their own version of a method, changing or adding to what the parent class does.