PCAP Object-Oriented Programming Practice Question
Consider the following code: class A: x = 1; class B(A): pass; class C(A): x = 2; class D(B, C): pass. What is D.x?
⚠ Common exam trap
Python Institute often tests the misconception that Python's multiple inheritance simply merges attributes from all parent classes, leading candidates to expect a tuple or an error, rather than applying the C3 linearization to determine a single overriding value.
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
✓
2
Python's method resolution order (MRO) for class D, which uses multiple inheritance, follows the C3 linearization algorithm. The MRO for D is D, B, C, A, and since C defines x = 2, that attribute overrides the x = 1 from A in the inheritance chain, so D.x resolves to 2.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
AttributeError
Why it's wrong here
AttributeError is raised only when an attribute is not found anywhere in the instance's MRO. Here x exists in both A and C, so the lookup is guaranteed to succeed. The real question is which definition is found first, not whether one exists. Because A defines x and C overrides it, D instances always resolve x without error.
- ✗
1
Why it's wrong here
1 is the value defined in A, but that class is not the first on D's MRO. The linearization for D(B, C) is D -> B -> C -> A -> object, so the interpreter checks B (which inherits but does not define x), then C, where x = 2 shadows A's value. The lookup stops at C and never reaches A. Returning 1 would imply that base-class definitions take precedence over earlier MRO entries, which violates Python's local precedence ordering.
- ✓
2
Why this is correct
C's x = 2 is the correct result because attribute lookup walks D.__mro__ in order: D, B, C, A, object. B adds no x attribute, so the first slot that defines x is C, and its value 2 is returned immediately. This is exactly how the C3 linearization resolves the diamond: the direct base class C is visited before the shared ancestor A. The number 2 therefore comes from the local precedence of C over A in the class definition D(B, C).
- ✗
(1, 2) tuple
Why it's wrong here
The value of D.x is a single integer, never the tuple (1, 2); Python's attribute lookup returns the first matching binding, not a collection of every override in the inheritance chain. Even though A.x is 1 and C.x is 2, only one of those values is selected based on MRO order. A tuple would require an explicit definition like x = (1, 2) or a descriptor that aggregates values, which is not present. Therefore the output is the integer 2, not a tuple.
Visual reference
Go deeper
Related to this question
About these practice questions
Courseiva writes every PCAP question from scratch — 421 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →
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.