ProgrammingHardProduct-company placement · GATE CS

A UI component registers itself as an observer of a long-lived global EventBus when it is created but never unregisters. Screens using such components are opened and closed many times. What goes wrong?

  1. A. The EventBus throws ConcurrentModificationException when the component closes
  2. B. The components' memory is reclaimed, but events are delivered twice
  3. C. Nothing; the garbage collector removes observers that are no longer on screen
  4. D. The bus keeps strong references to every closed component, so they are never garbage-collected and keep receiving events: a memory leak

Correct answer: D. The bus keeps strong references to every closed component, so they are never garbage-collected and keep receiving events: a memory leak

This is the lapsed-listener problem: the subject's observer list keeps every observer reachable. Fixes are explicit unsubscription tied to the component's lifecycle, or weak references in the subject.

More OOP with C++ and Java questions

Exams that ask this topic