Bird
Raised Fist0

Identify the error in this code snippet:

medium📝 Debug Q7 of Q15
Python - Inheritance and Code Reuse
Identify the error in this code snippet:
class A:
    def __init__(self):
        print("A init")

class B(A):
    def __init__(self):
        print("B init")
        super().__init__()

b = B()
ANo error, code runs fine
BMissing call to parent class explicitly
Csuper() cannot be used in __init__
Dsuper().__init__() should be called before print statement
Step-by-Step Solution
Solution:
  1. Step 1: Check for syntax errors

    The super().__init__() call is syntactically correct.

  2. Step 2: Verify execution

    The code runs without errors, printing "B init" followed by "A init".

  3. Final Answer:

    No error, code runs fine -> Option A
  4. Quick Check:

    super() called correctly, no crash [OK]
Quick Trick: super().__init__() can be called after other child code [OK]
Common Mistakes:
MISTAKES
  • Believing parent must be called first (not required)
  • Thinking super() invalid in __init__
  • Expecting runtime error from call order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes