Courseiva
Question 52 of 169
Object-Oriented ProgramminghardMultiple ChoiceObjective-mapped

PCAP Class attribute Practice Question

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))

Refer to the exhibit. What is printed?

⚠ Common exam trap

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.

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

3\n3

The code defines a class `A` with a class attribute `x = 3`. Inside `__init__`, the first `print(self.x)` accesses the class attribute (since no instance attribute exists yet), printing `3`. The statement `self.x += 1` is equivalent to `self.x = self.x + 1`; it reads the class attribute for the right-hand side, evaluates to `4`, and then creates a new instance attribute `x` with value `4`, shadowing the class attribute. The second `print(A.x)` explicitly accesses the class attribute, which remains `3`. Hence the output is `3` and `3` on separate lines.

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

    This output would require the function to produce different values on identical successive calls, such as a counter that increments each invocation. However, the exhibit uses caching: the second call with the same argument bypasses the function body and returns the previously stored result, so no modification occurs and the value remains 3.

  • 3\nError

    Why it's wrong here

    This output would mean the first call succeeded but the second threw an exception, perhaps due to an exhausted iterator or a mutated argument. Caching, however, means the second call never re-executes the function code; it retrieves the cached return value. Since the first call completes without error, the second call cannot introduce a new error from the function body.

  • Error\n3

    Why it's wrong here

    This output would indicate the first call raised an exception and the second somehow succeeded, which contradicts how caching behaves in the exhibit. In a cached function, the first call executes and must finish normally to store a result; the output shows a successful computation of 3. Therefore, there is no error on the first call, making this sequence impossible.

  • 3\n3

    Why this is correct

    On the first invocation, the function computes and returns 3, and the cache stores that result keyed by the argument. The second invocation sees the argument already in the cache and immediately returns the stored value 3 without re-entering the function. This behavior is exactly what caching decorators like `functools.lru_cache` provide, making the output consistent.

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 →

How Courseiva writes practice questions · Editorial policy

Last reviewed: Jun 30, 2026

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.

Loading comments…

Sign in to join the discussion.

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.