ProgrammingVery hardInfosys HackWithInfy · TCS CodeVita

What is printed?

What is printed?
class A:
    def f(self):
        return ['A']
class B(A):
    def f(self):
        return ['B'] + super().f()
class C(A):
    def f(self):
        return ['C'] + super().f()
class D(B, C):
    def f(self):
        return ['D'] + super().f()
print(D().f())
  1. A. ['D', 'B', 'A']
  2. B. ['D', 'C', 'B', 'A']
  3. C. ['D', 'B', 'C', 'A']
  4. D. ['D', 'B', 'C', 'A', 'A']

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

Method resolution order for D is D, B, C, A, and each super() call moves to the next class in that order. The result therefore visits B before C.

More Python Programming questions

Exams that ask this topic