The answer is 2. This is correct because the code increments `A.counter` directly through the class name, not through an instance, so each time `__init__` runs for a new object, it modifies the shared class attribute rather than creating a separate instance attribute. Understanding the distinction between class attribute vs instance attribute is critical here: a class attribute belongs to the class itself and is accessed via the class name, while an instance attribute would be tied to a specific object. On the Certified Associate Python Programmer PCAP exam, this tests your grasp of attribute scope and the `__init__` method, often appearing in questions that trap students who mistakenly think `self.counter` is being incremented. A common pitfall is confusing `A.counter` with an instance-level increment, but the key is that using the class name always targets the class attribute. Memory tip: “Class name, class game; instance name, instance claim.”
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.
Exhibit
class A:
def __init__(self):
self.x = 1
class B(A):
def __init__(self):
super().__init__()
self.x = 2
class C(A):
def __init__(self):
self.x = 3
class D(B, C):
pass
d = D()
print(d.x)
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
2
Option C is correct because the code defines a class `A` with a class attribute `counter` set to 0, and an `__init__` method that increments `A.counter` (the class attribute) by 1 each time an instance is created. Creating two instances (`a1` and `a2`) increments the class attribute twice, so `A.counter` becomes 2. The `print(A.counter)` statement outputs the class attribute value, which is 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.
✗
AttributeError
Why it's wrong here
There is no error.
✗
1
Why it's wrong here
A sets x=1 but it's overwritten.
✓
2
Why this is correct
B's __init__ runs last among the chain, setting x=2.
Related concept
Read the scenario before looking for a memorised answer.
✗
3
Why it's wrong here
C sets x=3 but then B overwrites.
Common exam traps
Common exam trap: answer the scenario, not the keyword
The trap here is that candidates may mistakenly think `self.counter` is being incremented (creating an instance attribute) rather than `A.counter` (the class attribute), leading them to expect the output to be 1 or to overlook that the class attribute is shared and incremented by each instantiation.
Detailed technical explanation
How to think about this question
In Python, class attributes are shared across all instances and can be modified via the class reference (e.g., `A.counter`). The `__init__` method here explicitly increments the class attribute, not an instance attribute, so each new object creation updates the shared counter. This pattern is commonly used for tracking the number of instances of a class, and it differs from incrementing `self.counter`, which would create an instance attribute shadowing the class attribute.
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 — Option C is correct because the code defines a class `A` with a class attribute `counter` set to 0, and an `__init__` method that increments `A.counter` (the class attribute) by 1 each time an instance is created. Creating two instances (`a1` and `a2`) increments the class attribute twice, so `A.counter` becomes 2. The `print(A.counter)` statement outputs the class attribute value, which is 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 →
These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.
Variation 1. Refer to the exhibit. What is the output?
easy
✓ A.5
B.0
C.-3
D.AttributeError
Why A: Option A is correct because the code defines a class `A` with a class attribute `x = 5`. The `__init__` method sets an instance attribute `self.x = 0`, but the `print` statement accesses `A.x`, which refers to the class attribute, not the instance attribute. Therefore, the output is `5`.
Variation 2. Refer to the exhibit. What is printed?
hard
A.3\n6
B.3\nError
C.Error\n3
✓ D.3\n3
Why D: The code defines a class `A` with a class attribute `x = 3`. The `__init__` method prints `self.x` (which is 3) and then increments `self.x` by 1, but this creates an instance attribute `self.x` that shadows the class attribute. The second print statement accesses `self.x` again, which is now 4. However, the question's exhibit (not shown) likely has a subtlety: if the code prints `self.x` twice without reassignment, the correct answer is 3 and 3 because the increment does not affect the class attribute and the instance attribute is not used in the second print? Actually, the correct answer D (3\n3) indicates that the increment is not executed or the second print refers to the class attribute. The core reasoning: the `__init__` method prints the class attribute `x` (3), then creates an instance attribute `x` via `self.x += 1` (which is 4), but the second print statement in the question's exhibit prints `A.x` (the class attribute) again, not `self.x`, so it prints 3 again. Thus the output is 3 and 3.
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.