What is the output of this Python code using multiple inheritance?
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())
Remember Python uses method resolution order (MRO) and looks for methods in the first parent class listed.
Class C inherits from A and B. Since A is listed first, its greet method is used.
What will this code print?
class X: def action(self): return "Action from X" class Y: def action(self): return "Action from Y" class Z(X, Y): def action(self): return super().action() obj = Z() print(obj.action())
super() in class Z calls the next method in the MRO after Z.
super() calls X's action method because X is the first parent in the MRO after Z.
What is the output of this code involving diamond inheritance?
class Base: def greet(self): return "Hello from Base" class Left(Base): def greet(self): return "Left says " + super().greet() class Right(Base): def greet(self): return "Right says " + super().greet() class Child(Left, Right): def greet(self): return "Child says " + super().greet() obj = Child() print(obj.greet())
Check the method resolution order (MRO) for Child and how super() calls chain.
The MRO is Child > Left > Right > Base. Each greet calls super().greet(), so the output chains accordingly.
What error does this code raise?
class A: pass class B: pass class C(A, B): pass
Check the syntax for listing multiple parent classes.
Parent classes must be separated by commas. Missing comma causes SyntaxError.
Given these classes, which option shows the correct method resolution order (MRO) for class D?
class A: pass class B(A): pass class C(A): pass class D(B, C): pass
Python uses C3 linearization for MRO. The order of parents in class D matters.
For class D(B, C), MRO is D, B, C, A, object. B before C because B is listed first.