ProgrammingModerateGATE CS · Product-company placement

What is printed?

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

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

C3 linearisation puts A after both B and C (A must follow all its subclasses), giving D, B, C, A, object. B has no hi, so C.hi is found before A.hi.

More Python Programming questions

Exams that ask this topic