Bird
Raised Fist0

What will be the output of this code?

medium📝 Predict Output Q5 of Q15
Python - Inheritance and Code Reuse
What will be the output of this code?
class X:
    def __init__(self):
        print("X init")

class Y(X):
    def __init__(self):
        super().__init__()
        print("Y init")

obj = Y()
AY init\nX init
BError: super() not called properly
CX init
DX init\nY init
Step-by-Step Solution
Solution:
  1. Step 1: Follow constructor calls

    Y's constructor calls super().__init__() which runs X's constructor printing "X init".

  2. Step 2: Continue Y's constructor

    After parent's init, Y prints "Y init".

  3. Final Answer:

    X init\nY init -> Option D
  4. Quick Check:

    super().__init__() calls parent constructor first [OK]
Quick Trick: super().__init__() runs parent constructor first [OK]
Common Mistakes:
MISTAKES
  • Expecting child's print before parent's
  • Thinking super() causes error
  • Missing parentheses in super call

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes