Courseiva
Object-Oriented Programming →mediumMultiple Choice

PCAP Object-Oriented Programming Practice Question

Consider the following class hierarchy: class A: def method(self): return 'A'; class B(A): pass; class C(A): def method(self): return 'C'; class D(B, C): pass. What is the output of D().method() according to Python's MRO?

⚠ Common exam trap

Python Institute often tests the C3 linearization rule that the MRO respects the order of base classes in the class definition, so candidates mistakenly think B's inheritance from A takes precedence over C's override, leading them to pick 'A' or 'B' instead of 'C'.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

'C'

Python's MRO (Method Resolution Order) for class D(B, C) is computed using the C3 linearization algorithm, which respects the local precedence order and monotonicity. The MRO for D is D -> B -> C -> A, so D().method() resolves to C.method(), returning 'C'. Option D is correct because C is the first class in the MRO that defines method().

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    'B'

    Why it's wrong here

    Choosing 'B' would be incorrect because B does not define an override of method(), so its class dictionary is not searched as a stopping point. During attribute lookup, Python visits each class in the MRO in order and only uses the entry from the first class that actually has method() in its __dict__. Since B merely inherits the attribute without supplying its own implementation, the lookup moves past B, making 'B' an invalid answer.

  • ✗

    TypeError

    Why it's wrong here

    A TypeError is not produced because the attribute lookup for method() succeeds; the callable is found and bound before any invocation occurs. Errors such as 'object is not callable' or 'missing positional arguments' are only raised after a call is attempted, and here C's method() accepts the call without any mismatch. Therefore the program runs to completion and prints something rather than raising an exception, so TypeError is the wrong choice.

  • ✗

    'A'

    Why it's wrong here

    Although A may be a base class that defines method(), it is not the first class in the MRO to contain that name. Python's C3 linearization places more-derived classes before their ancestors, so C appears before A in the search order and its method() implementation shadows A's version. Thus 'A' would only be printed if no subclass earlier in the MRO had overridden method(), which is not the case here.

  • ✓

    'C'

    Why this is correct

    C is the correct answer because C's method() is the first matching attribute encountered when traversing the MRO of the instance. Attribute lookup checks each class's __dict__ in the order determined by the C3 linearization, and since C is more specialized than its bases, its method() takes precedence. The call therefore dispatches to C's implementation, and the returned string is 'C'.

Visual reference

Client Recursive Resolver Root DNS (13 root servers) TLD DNS (.com, .org, …) Authoritative example.com query IP addr answer

About these practice questions

This PCAP question is part of Courseiva's 421-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This PCAP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCAP exam.