Bird
Raised Fist0

Find the error in this code:

medium📝 Debug Q14 of Q15
Python - Inheritance and Code Reuse
Find the error in this code:
class Parent:
    def __init__(self, name):
        self.name = name

class Child(Parent):
    def __init__(self, name, age):
        self.age = age

c = Child('Anna', 10)
print(c.name, c.age)
AChild class __init__ does not call Parent __init__, so name is missing
BSyntax error in class definition
CCannot create Child instance with two arguments
Dprint statement syntax is wrong
Step-by-Step Solution
Solution:
  1. Step 1: Check constructor chaining in child class

    The child class __init__ method sets age but does not call super().__init__(name) to set name from the parent.
  2. Step 2: Understand consequence of missing super call

    Because name is not set in Child, accessing c.name will cause an error or missing attribute.
  3. Final Answer:

    Child class __init__ does not call Parent __init__, so name is missing -> Option A
  4. Quick Check:

    Missing super() call = missing parent attributes [OK]
Quick Trick: Always call super().__init__ in child __init__ to set parent attributes [OK]
Common Mistakes:
MISTAKES
  • Forgetting to call super().__init__ in child constructor
  • Assuming parent attributes set automatically
  • Confusing syntax errors with logic errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes