Bird
0
0

You have two parent classes:

hard📝 Application Q8 of 15
Python - Inheritance and Code Reuse
You have two parent classes:
class A:
    def greet(self):
        return "Hello from A"

class B:
    def greet(self):
        return "Hello from B"

class C(A, B):
    def greet(self):
        return super().greet() + " and C"

print(C().greet())

What is the output?
AHello from B and C
BHello from A and C
CHello from C
DError due to multiple inheritance
Step-by-Step Solution
Solution:
  1. Step 1: Understand method resolution order (MRO)

    Class C inherits from A then B, so super() calls A's greet first.

  2. Step 2: Combine strings from super() and C

    super().greet() returns "Hello from A", then " and C" is added.

  3. Final Answer:

    Hello from A and C -> Option B
  4. Quick Check:

    MRO picks first parent method = super() calls A [OK]
Quick Trick: super() follows MRO, calls first parent method [OK]
Common Mistakes:
  • Assuming super() calls B's method
  • Expecting error from multiple inheritance
  • Ignoring MRO order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes