Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q5 of 15
Python - Inheritance and Code Reuse
What will be the output of this code?
class A:
    def show(self):
        print('A show')

class B(A):
    def show(self):
        print('B show')
        super().show()

b = B()
b.show()
AA show B show
BB show A show
CB show
DError: super() call invalid
Step-by-Step Solution
Solution:
  1. Step 1: Trace the method calls in B.show()

    First it prints 'B show', then calls super().show() which prints 'A show'.

  2. Step 2: Determine output order

    Output is 'B show' first, then 'A show' on next line.

  3. Final Answer:

    B show A show -> Option B
  4. Quick Check:

    Child prints then super() parent prints = D [OK]
Quick Trick: super() calls parent after child's code if placed after print [OK]
Common Mistakes:
  • Assuming parent prints first
  • Expecting error from super()
  • Confusing print order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes