PCAP Object-Oriented Programming Practice Question
Exhibit
class Counter:
count = 0
def __init__(self):
Counter.count += 1
@classmethod
def get_count(cls):
return cls.count
c1 = Counter()
c2 = Counter()
print(Counter.get_count())
print(c1.get_count())
print(c2.get_count())Refer to the exhibit. What is the output?
⚠ Common exam trap
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.
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 2 2
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.
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
Choosing 2, 0, 0 implies you think `Sample.count` is correctly incremented to 2, but each instance somehow has its own `count` attribute initialized to 0. In the exhibit no `self.count = ...` assignment exists inside `__init__`, so instance attribute lookup finds no key in the instance `__dict__` and falls back to the class attribute. Therefore `a.count` and `b.count` also evaluate to 2, not 0.
- ✓
2 2 2
Why this is correct
The class attribute `count` begins at 0, and each call to `__init__` executes `Sample.count += 1`; with two instantiations before any `print`, the shared class variable is exactly 2. Attribute access on an instance first checks that instance's namespace; since neither `a` nor `b` ever assigns `self.count`, both lookups resolve to the same class-level integer. Thus the three printed lines are identical: 2, 2, 2.
- ✗
2 1 1
Why it's wrong here
`2, 1, 1` would require each instance to store its own private counter value of 1, masking the class attribute during instance access. However, the code never creates an instance attribute named `count`, so `a.count` and `b.count` do not perform an instance-level lookup that finds a different value; they traverse up to `Sample.count`. The class attribute is shared and unchanged by instance access, so the second and third lines mirror the class value of 2, not a per-instance 1.
- ✗
0 2 2
Why it's wrong here
This output mistakenly places the first `print(Sample.count)` before the two constructor calls, or assumes incrementing happens after printing. In the actual sequence, both `Sample()` calls run first and each increments the class variable, so the printed class attribute is already 2. The subsequent instance accesses also resolve to that same class attribute, so the first line is 2 and the following lines are also 2, not 0 and then 2.
Go deeper
Related to this question
About these practice questions
Courseiva writes every PCAP question from scratch — 169 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.