Bird
Raised Fist0

What will be the output of this code?

medium📝 Predict Output Q13 of Q15
Python - Multiple Inheritance and Method Resolution
What will be the output of this code?
class A:
    def greet(self):
        return 'Hello from A'

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

class C(A, B):
    pass

obj = C()
print(obj.greet())
AHello from A
BError: Method greet not found
CHello from C
DHello from B
Step-by-Step Solution
Solution:
  1. Step 1: Understand method resolution order (MRO)

    Python looks for methods in the order of parent classes listed. Here, C inherits from A first, then B.
  2. Step 2: Determine which greet method is called

    Since A is first, C uses A's greet method, returning 'Hello from A'.
  3. Final Answer:

    Hello from A -> Option A
  4. Quick Check:

    MRO uses first parent method [OK]
Quick Trick: First parent class method is used in conflicts [OK]
Common Mistakes:
MISTAKES
  • Choosing method from second parent
  • Expecting child's own method when none defined
  • Thinking it causes error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes