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())
- BA
- BCAA
- BCA
- 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
- A House is made of Rooms, and the Rooms cannot exist without the House: when…
- What is the output of the following Java code?
- What is the output of the following C++ program?
- What is the output of the following Java code?
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