Question 182 of 511
Object-Oriented ProgramminghardMultiple ChoiceObjective-mapped

Quick Answer

The answer is to use a `weakref.WeakSet` for the participants list in ChatRoom. This resolves the memory leak because it breaks the circular reference between ChatRoom and User objects: while each User still holds a strong reference back to its ChatRoom, the ChatRoom now only holds weak references to its Users. When a user disconnects and all external strong references to that User are removed, the User object becomes unreachable and is garbage collected, even though the User still points to the ChatRoom. On the PCAP exam, this scenario tests your understanding of garbage collection and the `weakref` module, specifically how weak references prevent reference cycles from keeping objects alive—a common trap where developers assume Python’s cyclic garbage collector handles all circular references, but it can be delayed or unreliable for real-time applications. For a memory tip: remember that weak references let you “borrow” an object without owning it, so the garbage collector can still reclaim it when no strong references remain.

PCAP Object-Oriented Programming Practice Question

This PCAP practice question tests your understanding of object-oriented programming. Read the scenario carefully and evaluate each option against the stated constraints before committing to an answer. After answering, compare your reasoning against the explanation and wrong-answer breakdown below. Once you have made your selection, read the full explanation to reinforce the concept and understand why each distractor is designed to mislead on exam day.

A development team is building a real-time chat application using Python. The application uses a class 'ChatRoom' that maintains a list of 'User' objects as active participants. Each User object holds a reference back to its ChatRoom to send messages. Over time, the application runs out of memory. Profiling reveals that User objects are not being garbage collected even after users disconnect. The team suspects circular references. Which solution would effectively resolve the memory leak without breaking the functionality?

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 weakref.WeakSet for the participants list in ChatRoom, so that when a User is no longer referenced elsewhere, it is automatically removed

Option A is correct because using a `weakref.WeakSet` for the participants list in `ChatRoom` means the `ChatRoom` holds only weak references to `User` objects. When a user disconnects and all external references to that `User` are removed, the `User` object becomes unreachable and can be garbage collected, even though the `User` still holds a strong reference back to the `ChatRoom`. This breaks the circular reference without requiring manual intervention or altering the `User`-to-`ChatRoom` relationship.

Key principle: Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.

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 weakref.WeakSet for the participants list in ChatRoom, so that when a User is no longer referenced elsewhere, it is automatically removed

    Why this is correct

    WeakSet allows the chat room to hold references without preventing garbage collection; when the only strong references to a User are gone, it is cleaned up.

    Related concept

    Read the scenario before looking for a memorised answer.

  • Increase the Python heap size using PYTHON_MALLOC_DEBUG to avoid memory issues

    Why it's wrong here

    This avoids the symptom but does not fix the root cause; memory will still grow unbounded.

  • Store the ChatRoom reference in User using a weakref.ref, so that the cycle is broken

    Why it's wrong here

    This would break the cycle but User might lose access to its ChatRoom too early if the only strong reference is elsewhere, causing errors.

  • Manually call gc.collect() every time a user disconnects

    Why it's wrong here

    This forces garbage collection but does not prevent cycles from being created; also is not a clean design.

Common exam traps

Common exam trap: answer the scenario, not the keyword

Python Institute often tests the distinction between breaking a cycle from one side versus the other; the trap here is that candidates think weakening the `User`'s reference to `ChatRoom` (Option C) is sufficient, but they overlook that the `ChatRoom`'s strong reference to `User` keeps the `User` alive, so the cycle is still unbreakable from the garbage collector's perspective.

Detailed technical explanation

How to think about this question

Under the hood, Python's garbage collector uses reference counting as the primary mechanism, with a cyclic garbage collector to handle reference cycles. However, if a cycle involves objects that are still strongly referenced from a live root (like the `ChatRoom`'s list), the cycle is not garbage. Using `weakref.WeakSet` ensures that the `ChatRoom` does not increase the reference count of `User` objects, so when the only remaining references to a `User` are weak, the object is immediately deallocated. A real-world scenario is a GUI framework where widgets hold references to parent containers; using weak references for the container back-reference prevents memory leaks when widgets are removed.

KKey Concepts to Remember

  • Read the scenario before looking for a memorised answer.
  • Find the constraint that changes the correct option.
  • Eliminate answers that are true in general but not in this case.

TExam Day Tips

  • Watch for words such as best, first, most likely and least administrative effort.
  • Review why wrong options are wrong, not only why the correct option is correct.

Key takeaway

Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.

Real-world example

How this comes up in practice

A media company stores terabytes of video archives that are accessed once a year for audit purposes. Moving these objects to a cold storage tier (Azure Archive, S3 Glacier, or Google Nearline) costs a fraction of hot storage. Questions like this test whether you understand storage tiers, access frequency tradeoffs, and retrieval latency requirements.

What to study next

Got this wrong? Here's your next step.

Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.

Related practice questions

Related PCAP practice-question pages

Use these pages to review the topic behind this question. This is how one missed question becomes focused revision.

Practice this exam

Start a free PCAP practice session

Short sessions build daily habit. Longer sessions build exam-day stamina. Try a timed session to simulate real conditions.

FAQ

Questions learners often ask

What does this PCAP question test?

Object-Oriented Programming — This question tests Object-Oriented Programming — Read the scenario before looking for a memorised answer..

What is the correct answer to this question?

The correct answer is: Use weakref.WeakSet for the participants list in ChatRoom, so that when a User is no longer referenced elsewhere, it is automatically removed — Option A is correct because using a `weakref.WeakSet` for the participants list in `ChatRoom` means the `ChatRoom` holds only weak references to `User` objects. When a user disconnects and all external references to that `User` are removed, the `User` object becomes unreachable and can be garbage collected, even though the `User` still holds a strong reference back to the `ChatRoom`. This breaks the circular reference without requiring manual intervention or altering the `User`-to-`ChatRoom` relationship.

What should I do if I get this PCAP question wrong?

Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.

What is the key concept behind this question?

Read the scenario before looking for a memorised answer.

About these practice questions

Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Keep practising

More PCAP practice questions

Last reviewed: Jun 30, 2026

Question Discussion

Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.

Loading comments…

Sign in to join the discussion.

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.