PCAP Object-Oriented Programming Practice Question
A developer wants to ensure that a class attribute is shared across all instances. Which approach should be used?
⚠ Common exam trap
Python Institute often tests the distinction between class attributes and instance attributes, and the trap here is that candidates may confuse defining an attribute inside __init__ with creating a shared attribute, not realizing that __init__ always creates instance-specific attributes.
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
✓
Define the attribute in the class body, outside any methods.
Defining an attribute in the class body, outside any methods, creates a class attribute that is shared across all instances. Class attributes are stored in the class's __dict__ and are accessible via the class or any instance, unless shadowed by an instance attribute.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Use the @property decorator.
Why it's wrong here
The @property decorator turns a method into a managed attribute descriptor, but each access invokes the getter and typically returns a value computed from instance state. It does not create a class-level storage location; the backing data is usually an instance attribute or calculated on the fly. As a result, property does not give you a shared attribute—it gives you per-instance behavior with custom logic.
- ✗
Define the attribute inside a method without using self.
Why it's wrong here
A name assigned inside a method body without self. or the class name becomes a local variable scoped to that method. It is destroyed when the method returns and never becomes part of the instance or class namespace. Consequently, no attribute is created at all, let alone a shared class attribute.
- ✓
Define the attribute in the class body, outside any methods.
Why this is correct
Assigning a value in the class body, directly at indentation level of the class, creates a class attribute stored on the class object. All instances of that class share the same object through normal attribute lookup; accessing it via an instance falls back to the class if no instance attribute shadows it. This is the idiomatic way to create shared state used by every instance.
- ✗
Define the attribute inside __init__ using self.
Why it's wrong here
Inside __init__, the line self.attr = value invokes attribute assignment on the instance, which always creates or updates an instance attribute in the instance's own __dict__. Each new object gets its own independent copy, so changes made by one instance do not appear for other instances. This is instance-specific storage, not class-wide shared storage.
Go deeper
Related to this question
About these practice questions
One of 421 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 →
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.