Bird
0
0

Consider this code:

hard📝 Application Q9 of 15
Python - Inheritance and Code Reuse
Consider this code:
class Base:
    def __init__(self, x):
        self.x = x

class Derived(Base):
    def __init__(self, x, y):
        super().__init__(x)
        self.y = y

obj = Derived(5, 10)
print(obj.x, obj.y)

What will be printed?
A5 10
B10 5
CError: missing argument in super()
DError: Derived class missing __init__
Step-by-Step Solution
Solution:
  1. Step 1: Trace constructor calls and arguments

    Derived calls super().__init__(x) passing 5 to Base, setting self.x = 5.

  2. Step 2: Derived sets self.y = 10

    Both attributes exist on obj.

  3. Final Answer:

    5 10 -> Option A
  4. Quick Check:

    super() passes arguments to parent constructor correctly [OK]
Quick Trick: Pass needed args to super().__init__() [OK]
Common Mistakes:
  • Forgetting to pass arguments to super()
  • Expecting error due to missing args
  • Confusing attribute order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes