ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
class A: pass
class B(A): pass
try:
    class C(A, B): pass
    print("ok", end=" ")
except TypeError:
    print("TypeError", end=" ")
class D(B, A): pass
print([k.__name__ for k in D.__mro__])
  1. A. ok ['D', 'B', 'A', 'object']
  2. B. TypeError ['D', 'A', 'B', 'object']
  3. C. TypeError ['D', 'B', 'A', 'object']
  4. D. ok ['D', 'B', 'A', 'A', 'object']

Correct answer: C. TypeError ['D', 'B', 'A', 'object']

C3 linearisation must keep every subclass before its bases and respect the order of the bases list. class C(A, B) asks for A before B, but B is a subclass of A and must come first, so no consistent MRO exists and Python raises TypeError. D(B, A) is consistent: D, B, A, object, each class appearing once.

More Python Programming questions

Exams that ask this topic