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?
- A. The EventBus throws ConcurrentModificationException when the component closes
- B. The components' memory is reclaimed, but events are delivered twice
- C. Nothing; the garbage collector removes observers that are no longer on screen
- 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
- A vending machine object behaves differently when Idle, HasCoin or Dispensing, and each state object decides which state comes next. A…
- Account.withdraw(amount) works for any amount up to the balance. A subclass FixedDeposit overrides it to throw an exception unless the…
- Which call violates the Law of Demeter (talk only to your immediate collaborators) inside an OrderService method that receives an Order…
- How many of these statements about Java methods are correct? 1. A final method can be overloaded in a subclass. 2. A static method can be…
- Classes A, B extends A and C extends B all override m(). Inside C, which way can call A's version of m() on the current object?
- Joshua Bloch recommends implementing a singleton as a single-element enum. Which advantage does this give over a class with a private…