ProgrammingHardTCS NQT · Infosys pattern

What is printed?

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

Correct answer: C. 6

C().f() computes 3 + super().f(), where super() in C resolves to B, giving 3 + (2 + super().f()) with B's super() resolving to A, giving 3 + (2 + 1) = 6.

More Python Programming questions

Exams that ask this topic