0
0
Pythonprogramming~20 mins

Multiple inheritance syntax in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Multiple Inheritance Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of multiple inheritance method call

What is the output of this Python code using multiple inheritance?

Python
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
BHello from B
CHello from C
DTypeError
Attempts:
2 left
💡 Hint

Remember Python uses method resolution order (MRO) and looks for methods in the first parent class listed.

Predict Output
intermediate
2:00remaining
Output when overriding method in multiple inheritance

What will this code print?

Python
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())
AAction from X
BAttributeError
CAction from Z
DAction from Y
Attempts:
2 left
💡 Hint

super() in class Z calls the next method in the MRO after Z.

Predict Output
advanced
2:00remaining
Output of diamond inheritance with super()

What is the output of this code involving diamond inheritance?

Python
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())
AChild says Left says Hello from Base
BChild says Right says Left says Hello from Base
CChild says Left says Right says Hello from Base
DTypeError
Attempts:
2 left
💡 Hint

Check the method resolution order (MRO) for Child and how super() calls chain.

Predict Output
advanced
2:00remaining
Error raised by incorrect multiple inheritance syntax

What error does this code raise?

Python
class A:
    pass

class B:
    pass

class C(A, B):
    pass
ANo error
BTypeError
CNameError
DSyntaxError
Attempts:
2 left
💡 Hint

Check the syntax for listing multiple parent classes.

🧠 Conceptual
expert
2:00remaining
Order of classes in multiple inheritance affects method resolution

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
A[D, A, B, C, object]
B[D, B, C, A, object]
C[D, C, B, A, object]
D[D, B, A, C, object]
Attempts:
2 left
💡 Hint

Python uses C3 linearization for MRO. The order of parents in class D matters.