Courseiva
Object-Oriented ProgrammingmediumMultiple ChoiceObjective-mapped

PCAP Object-Oriented Programming Practice Question

A developer creates classes `A`, `B(A)`, `C(A)`, and `D(B, C)`. When calling a method from `D` that is defined in `A`, which class's version is used according to Python's MRO?

⚠ Common exam trap

Python Institute often tests the misconception that Python's MRO simply searches the first parent and its ancestors before moving to the next parent (depth-first left-to-right), but the actual C3 linearization can produce a different order, especially in diamond inheritance, and candidates may incorrectly assume the method from `A` is found directly without considering the intermediate classes in the MRO.

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

The method from A, found through B then C.

Python's Method Resolution Order (MRO) for class `D(B, C)` follows the C3 linearization algorithm, which ensures a depth-first left-to-right search while preserving monotonicity. For `D(B, C)`, the MRO is `D -> B -> C -> A`, so a method defined in `A` that is not overridden in `B` or `C` will be found via `B` first, then `C`, and finally `A`. Option D correctly states that the method from `A` is used, found through `B` then `C`, which matches the actual resolution path.

Answer analysis

Option-by-option breakdown

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

  • The method from B, because it is the first parent.

    Why it's wrong here

    B is the first base class in the inheritance list and thus appears first in the MRO after D, but method resolution does not stop at a class merely because it is first; since B does not define the method, the lookup continues along the MRO to C and eventually A, which is the first class that actually provides it.

  • The method from C, because it appears after B.

    Why it's wrong here

    While C is the second class in the MRO after B, the method is still not defined on C, so the interpreter's lookup skips it and proceeds to A; a class must actually contain the attribute for the traversal to stop, and mere position in the MRO does not imply the method is defined there.

  • The method from A, but only if B and C do not override it.

    Why it's wrong here

    This statement is true but incomplete: it correctly identifies that A's method is used when neither B nor C overrides it, but the important detail is the MRO traversal order, which is D -> B -> C -> A for this hierarchy, so the lookup reaches A only after passing through B and C. The condition is satisfied here, and the method is not found directly from A but as the terminal point of the linearized search.

  • The method from A, found through B then C.

    Why this is correct

    The C3 linearization algorithm for class D(B, C), where B and C both inherit from A, produces the MRO D -> B -> C -> A. The attribute lookup follows this exact sequence: it checks D, then B, then C, and finally A; since neither B nor C defines the method, the first defining class encountered is A. Thus the method is found on A after the traversal through B and C.

About these practice questions

This PCAP question is part of Courseiva's 169-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.