Courseiva
Object-Oriented ProgramminghardMultiple ChoiceObjective-mapped

Class Attribute vs Instance Attribute: Incrementing via Class Name

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)

Refer to the exhibit. What is the output and why?

Quick Answer

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.”

⚠ Common exam trap

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.

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

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.

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

    AttributeError is incorrect because the class attribute `counter` exists and is modified by `A.counter += 1` in `__init__`, so no error occurs.

  • 1

    Why it's wrong here

    1 would be the output if only one instance was created, but two instances are created (`a1` and `a2`), each incrementing `A.counter`, so the value is 2.

  • 2

    Why this is correct

    Correct. The class attribute `counter` starts at 0 and is incremented by 1 each time an instance is created. Two instances are created, so `A.counter` becomes 2.

  • 3

    Why it's wrong here

    3 would require three increments, but only two instances are created, so the output is 2, not 3.

About these practice questions

One of 169 original PCAP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Same concept, more angles

2 more ways this is tested on PCAP

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: 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`. 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.

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.