Bird
0
0

Given these classes:

hard📝 Application Q8 of 15
Python - Multiple Inheritance and Method Resolution
Given these classes:
class A:
    def action(self):
        return 'A'
class B(A):
    def action(self):
        return 'B'
class C(A):
    def action(self):
        return 'C'
class D(B, C):
    def action(self):
        return super().action() + 'D'
class E(C, B):
    def action(self):
        return super().action() + 'E'

What will D().action() and E().action() return respectively?
A'BD' and 'BE'
B'BD' and 'CE'
C'CD' and 'BE'
DTypeError for E
Step-by-Step Solution
Solution:
  1. Step 1: Determine D's MRO and output

    D's MRO is D, B, C, A. super() in D calls B.action() which returns 'B', so D().action() returns 'B' + 'D' = 'BD'.
  2. Step 2: Determine E's MRO and output

    E's MRO is E, C, B, A. super() in E calls C.action() which returns 'C', so E().action() returns 'C' + 'E' = 'CE'.
  3. Final Answer:

    'BD' and 'CE' -> Option B
  4. Quick Check:

    MRO order affects super() calls and output [OK]
Quick Trick: super() calls next in MRO; order changes output [OK]
Common Mistakes:
  • Mixing B and C outputs
  • Expecting TypeError for E

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes