Courseiva
Object-Oriented ProgramminghardMultiple ChoiceObjective-mapped

PCAP Class attribute Practice Question

Exhibit

class Counter:
    count = 0
    def __init__(self):
        Counter.count += 1
        self.id = Counter.count

c1 = Counter()
c2 = Counter()
print(c1.id, c2.id)

Refer to the exhibit. What is the output?

⚠ Common exam trap

The trap is that candidates may think `self.x = 2` modifies the class attribute for all instances, leading them to choose '2 2', or they may forget the instance attribute shadows the class attribute, choosing '1 1'.

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

1 2

'1 2'. When `print(A.x, a.x)` is executed, `A.x` accesses the class attribute `x=1`, and `a.x` accesses the instance attribute `x=2` (set in `__init__`), which shadows the class attribute for that instance. Therefore, the output is '1 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.

  • 1 1

    Why it's wrong here

    Output 1 1 would occur only if the instance attribute had never been assigned, so e.attr still resolved to the class attribute value of 1. The line that sets the instance attribute to 2 is present, however, and that assignment does not modify Example.attr; it merely creates a separate entry in the instance's own namespace. Thus choosing 1 1 ignores the shadowing effect of the instance attribute.

  • 0 1

    Why it's wrong here

    The value 0 is simply not present anywhere in this code, so this output cannot be produced by any valid execution path. To get 0 1, the class attribute would have to be initialized to 0 (for example, attr = 0) or some method would have to return 0 before the first print. Since Example.attr is hard-coded as 1, the first printed value is inevitably 1, making 0 1 an impossible result.

  • 1 2

    Why this is correct

    This is correct because the two values are read from different namespaces. Example.attr directly accesses the class attribute, which remains 1 after assignment. In contrast, e.attr = 2 creates an instance attribute named 'attr' that shadows the class attribute during attribute lookup on that instance, so the instance dump returns 2. Therefore the output is exactly 1 2.

  • 2 2

    Why it's wrong here

    2 2 would require the assignment e.attr = 2 to overwrite the class attribute itself, but Python semantics do not do that. Attribute assignment on an instance always writes to the instance's own __dict__, leaving the unchanged class attribute at 1 for future class-level access. Only a direct assignment like Example.attr = 2 would change the class attribute, and that is not what the code does.

About these practice questions

This PCAP question is part of Courseiva's 169-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

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.