ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
class A:
    def who(self): return "A"
class B(A):
    def who(self): return "B"
class C(A):
    def who(self): return "C"
class D(B, C):
    pass
print(D().who())
print([c.__name__ for c in D.__mro__])
  1. A. A ['D', 'A', 'B', 'C', 'object']
  2. B. C ['D', 'B', 'C', 'A', 'object']
  3. C. B ['D', 'B', 'C', 'A', 'object']
  4. D. B ['D', 'C', 'B', 'A', 'object']

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

D inherits who from the nearest class in C3 linearization order, which is B. The MRO visits D, then B, then C, then A, then object, so D().who() is 'B'.

More Python Programming questions

Exams that ask this topic