The correct answer is 2, 2, 2. This output occurs because the code defines a class variable `x = 2` in class `A`, and class `B` inherits from `A` without overriding it. When the `display` method calls `self.x`, Python first looks for an instance attribute named `x` on each object; since no instance attribute is set, it falls back to the inherited class variable, which is `2` for all three instances. On the PCAP exam, this question tests your understanding of the distinction between a class variable and an instance attribute in Python, specifically how attribute lookup order works in inheritance. A common trap is assuming that each instance gets its own copy of the class variable, but in reality, all instances share the same class variable unless an instance attribute is explicitly assigned. Remember the lookup order: instance attribute first, then class variable, then parent class variables—think "ICE" (Instance, Class, parent).
PCAP Object-Oriented Programming Practice Question
This PCAP practice question tests your understanding of object-oriented programming. Read the scenario carefully and evaluate each option against the stated constraints before committing to an answer. After answering, compare your reasoning against the explanation and wrong-answer breakdown below. Once you have made your selection, read the full explanation to reinforce the concept and understand why each distractor is designed to mislead on exam day.
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
2
2
2
Option B is correct because the code defines a class `A` with a class variable `x = 2`, and a class `B` that inherits from `A`. The `display` method prints `self.x`, which first looks up the instance attribute `x`; since no instance attribute is set, it falls back to the class variable `x = 2` from class `A`. The loop creates three instances of `B` and calls `display` on each, so each prints `2` on a separate line, resulting in the output 2, 2, 2.
Key principle: Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
✗
2
0
0
Why it's wrong here
The class method returns class attribute, not instance attribute.
✓
2
2
2
Why this is correct
All calls refer to the same class variable.
Related concept
Read the scenario before looking for a memorised answer.
✗
2
1
1
Why it's wrong here
Instance calls also return class variable, not instance-specific.
✗
0
2
2
Why it's wrong here
count is incremented, so not 0.
Common exam traps
Common exam trap: answer the scenario, not the keyword
Python Institute often tests the distinction between class variables and instance attributes, and the trap here is that candidates mistakenly think each instance gets its own copy of `x` or that the loop modifies `x` per iteration, when in fact all instances share the same class variable unless explicitly overridden.
Detailed technical explanation
How to think about this question
In Python, attribute lookup follows the MRO (Method Resolution Order): instance, then class, then parent classes. Since `B` does not define its own `x`, all instances share the inherited class variable `A.x = 2`. If an instance attribute `x` were assigned (e.g., `self.x = 0`), it would shadow the class variable for that instance only, but no such assignment occurs here. This behavior is fundamental to understanding how Python's descriptor protocol and attribute resolution work in inheritance hierarchies.
KKey Concepts to Remember
Read the scenario before looking for a memorised answer.
Find the constraint that changes the correct option.
Eliminate answers that are true in general but not in this case.
TExam Day Tips
→Watch for words such as best, first, most likely and least administrative effort.
→Review why wrong options are wrong, not only why the correct option is correct.
Key takeaway
Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.
Real-world example
How this comes up in practice
A cloud solutions architect for a retail company is evaluating services for a new workload. The correct answer here reflects best practice for the specific scenario described — not a general cloud recommendation. Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option. Cloud exam questions reward reading the constraint carefully: the same technology can be right or wrong depending on the use case.
What to study next
Got this wrong? Here's your next step.
Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.
Object-Oriented Programming — This question tests Object-Oriented Programming — Read the scenario before looking for a memorised answer..
What is the correct answer to this question?
The correct answer is: 2
2
2 — Option B is correct because the code defines a class `A` with a class variable `x = 2`, and a class `B` that inherits from `A`. The `display` method prints `self.x`, which first looks up the instance attribute `x`; since no instance attribute is set, it falls back to the class variable `x = 2` from class `A`. The loop creates three instances of `B` and calls `display` on each, so each prints `2` on a separate line, resulting in the output 2, 2, 2.
What should I do if I get this PCAP question wrong?
Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.
What is the key concept behind this question?
Read the scenario before looking for a memorised answer.
About these practice questions
Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →
Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.
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.
Question Discussion
Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.
Sign in to join the discussion.