ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
class A:
    x = 1
class B(A):
    pass
class C(A):
    x = 3
class D(B, C):
    pass
B.x = 2
del B.x
print(D.x, D.__mro__.index(C))
  1. A. 1 2
  2. B. 2 2
  3. C. 3 3
  4. D. 3 2

Correct answer: D. 3 2

D's MRO is D, B, C, A, object (C at index 2). After B.x is added and deleted again, B has no own x, so lookup reaches C.x = 3 before A.x.

More Python Programming questions

Exams that ask this topic