Bird
0
0

Identify the problem in this code:

medium📝 Debug Q7 of 15
Python - Inheritance and Code Reuse
Identify the problem in this code:
class Parent:
    def __init__(self):
        self.value = 5

class Child(Parent):
    def __init__(self):
        self.value = 10

obj = Child()
print(obj.value)
AChild's __init__ does not call Parent's __init__, so parent's init is skipped.
BChild class cannot have its own __init__ method.
CParent class __init__ is private and inaccessible.
DValue attribute is not defined in Child class.
Step-by-Step Solution
Solution:
  1. Step 1: Understand constructor overriding

    Child defines its own __init__, which replaces Parent's __init__ unless called explicitly.
  2. Step 2: Check attribute value

    Child sets value to 10; Parent's value=5 is skipped because Parent's __init__ not called.
  3. Final Answer:

    Child's __init__ does not call Parent's __init__, so parent's init is skipped. -> Option A
  4. Quick Check:

    Overriding __init__ hides parent's unless super() called [OK]
Quick Trick: Call super().__init__() to run parent constructor [OK]
Common Mistakes:
  • Assuming parent's __init__ runs automatically
  • Thinking Child can't have __init__

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes