PCPP2 Design Patterns Practice Question
In the Observer pattern, what is the best way to prevent memory leaks when the Subject maintains a list of Observers?
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
✓
Use the weakref module to store references to the Observers
If the Subject maintains strong references to Observers, they cannot be garbage collected. Using weak references allows them to be collected when no longer used elsewhere.
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 global list of observers to manage them centrally
Why it's wrong here
A global list increases coupling and doesn't solve the memory leak issue.
- ✗
Set the Observer list to None after every notification cycle
Why it's wrong here
This would remove all observers, preventing future notifications.
- ✓
Use the weakref module to store references to the Observers
Why this is correct
weakref.ref allows the Subject to notify observers without preventing their garbage collection.
- ✗
Explicitly call the detach method for every Observer
Why it's wrong here
This requires manual management and is prone to errors if observers are short-lived.
About these practice questions
This PCPP2 question is part of Courseiva's 194-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 →
JA
Written and reviewed by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
Last reviewed August 2026 · checked against the official Python Institute exam blueprint
This PCPP2 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 PCPP2 exam.