Object-Oriented Programming · Question 29 · Hard

What is the output of the following Python code?

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):
    pass
print(D().f())
  1. BA
  2. BCAA
  3. BCA
  4. B
Answer and explanation

Answer: C. BCA

Explanation: D's method resolution order is D, B, C, A. B.f calls super().f(), which goes to the next class in D's MRO, which is C, not A. C then calls A. The result is 'B' + 'C' + 'A' = BCA.

Previous: What is the output of the following C++ program? · Next: What is the output of the following Java code?

Practise object-oriented programming

Try a fresh set of object-oriented programming questions with an explanation after every answer, free and without signing up: Object-Oriented Programming practice. Or sit a full placement mock test.

More object-oriented programming questions

More technical & programming practice

DBMS · SQL · Operating Systems · Computer Networks · Software Engineering · Cloud & DevOps · GenAI Basics · Pseudo-code

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge