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 Cache:
def __init__(self, func):
self.func = func
self.cache = {}
def __call__(self, *args):
if args in self.cache:
return self.cache[args]
result = self.func(*args)
self.cache[args] = result
return result
@Cache
def add(a, b):
return a + b
print(add(1, 2))
print(add(1, 2))
class Cache:
def __init__(self, func):
self.func = func
self.cache = {}
def __call__(self, *args):
if args in self.cache:
return self.cache[args]
result = self.func(*args)
self.cache[args] = result
return result
@Cache
def add(a, b):
return a + b
print(add(1, 2))
print(add(1, 2))
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
3\n3
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.
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.
✗
3\n6
Why it's wrong here
No modification.
✗
3\nError
Why it's wrong here
No error.
✗
Error\n3
Why it's wrong here
First call works fine.
✓
3\n3
Why this is correct
First computes, second retrieves from cache.
Related concept
Read the scenario before looking for a memorised answer.
Common exam traps
Common exam trap: answer the scenario, not the keyword
Python Institute often tests the subtle difference between class attributes and instance attributes, specifically that `self.x += 1` creates a new instance attribute rather than modifying the class attribute, leading candidates to mistakenly think the class attribute itself is incremented.
Detailed technical explanation
How to think about this question
In Python, class attributes are shared across all instances unless shadowed by an instance attribute. The expression `self.x += 1` is equivalent to `self.x = self.x + 1`, which reads the class attribute (3) and then assigns a new instance attribute (4). Subsequent access to `self.x` would return the instance attribute, but if the code prints `A.x` (the class attribute), it remains 3. This distinction is critical when using mutable vs. immutable objects; for immutable types like integers, the assignment creates a new instance attribute, while for mutable types, in-place modification affects the class attribute directly.
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 company's IT admin needs to give a contractor read-only access to production logs without sharing account credentials. Using role-based access control (RBAC) and temporary scoped permissions — not a permanent shared password — is the correct pattern. Questions like this test whether you can apply least-privilege access across cloud identity services.
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: 3\n3 — 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.
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.