PCAP Object-Oriented Programming Practice Question
A class has a class attribute that is a list. A developer modifies this list via one instance, and the change is reflected in all other instances. What is the best practice to avoid this unintended sharing?
⚠ Common exam trap
Python Institute often tests the distinction between class-level and instance-level attributes, and the trap here is that candidates mistakenly think using a tuple (immutable) or a class method solves the sharing problem, when the real issue is the location of the mutable object's definition.
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
✓
Initialize the list in `__init__` rather than as a class attribute.
Class attributes are shared across all instances. By initializing the list inside `__init__`, each instance gets its own independent list object, preventing unintended mutation from affecting other instances. This is the standard Python pattern for instance-specific mutable data.
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 a tuple instead of a list.
Why it's wrong here
A tuple is immutable, so replacing a list with a tuple would prevent any additions, removals, or in-place changes. However, the underlying issue isn't that the data structure is mutable; it's that every instance currently shares one single list object. A tuple would merely turn the symptom into a TypeError whenever code tries to mutate it, and a tuple containing mutable objects still allows those inner objects to be modified, so immutability at the top level does not guarantee isolation.
- ✓
Initialize the list in `__init__` rather than as a class attribute.
Why this is correct
Moving the list into `__init__` as `self.items = ...` causes a brand-new list to be created each time an instance is constructed. Because each object gets its own independent list bound to the instance, changes made through one instance never affect another. This is the standard Python pattern for per-instance mutable state and directly eliminates the accidental sharing caused by a class attribute.
- ✗
Use a class method to modify the list.
Why it's wrong here
A class method is bound to the class, not to an instance; if it modifies the list, it still modifies the very same class-level list that all instances share. Even if you call the class method through an instance, Python passes the class, not the instance, so the method has no access to instance-specific data. This approach does not create a fresh list per instance and therefore preserves the underlying aliasing problem.
- ✗
Use `deepcopy` when accessing the list.
Why it's wrong here
Calling `deepcopy` only at access time copies the list after it has already been stored as a shared class attribute; the original shared object remains, and every access produces a separate, disconnected copy. That makes each read inconsistent with previous reads, defeats the purpose of having persistent state, and adds O(n) copying overhead. It also does nothing to prevent future mutations of the underlying shared list, so it is both inefficient and an incomplete workaround.
Go deeper
Related to this question
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 →
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.