Bird
Raised Fist0

Consider this code:

hard🚀 Application Q9 of Q15
Python - Polymorphism and Dynamic Behavior
Consider this code:
class A:
    def method(self):
        return 'A'

class B(A):
    def method(self):
        return super().method() + 'B'

class C(B):
    def method(self):
        return super().method() + 'C'

obj = C()
print(obj.method())

What is the output?
ACBA
BABC
CA
DError due to super()
Step-by-Step Solution
Solution:
  1. Step 1: Trace method calls from class C

    Calling obj.method() runs C's method, which calls super().method() (B's method).
  2. Step 2: Trace B's method call

    B's method calls super().method() (A's method), which returns 'A'. Then B adds 'B' to get 'AB'.
  3. Step 3: Combine results in C's method

    C adds 'C' to 'AB', resulting in 'ABC'.
  4. Final Answer:

    ABC -> Option B
  5. Quick Check:

    super() chains calls up the inheritance [OK]
Quick Trick: super() chains calls up inheritance tree [OK]
Common Mistakes:
MISTAKES
  • Thinking output is reversed
  • Expecting only last class output
  • Assuming super() causes error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes