Bird
Raised Fist0

Given this code snippet, what will be the output when the Subject's state changes?

medium📝 Analysis Q4 of Q15
LLD - Design — Chess Game
Given this code snippet, what will be the output when the Subject's state changes?
subject.addObserver(observer1);
subject.addObserver(observer2);
subject.setState(10);

Assuming observers print the new state on update.
Aobserver1: 10 observer2: 10
Bobserver1: 0 observer2: 0
Cobserver1: 10
DNo output because observers are not notified
Step-by-Step Solution
Solution:
  1. Step 1: Understand observer registration

    Both observer1 and observer2 are registered to the subject.
  2. Step 2: Analyze state change notification

    When subject.setState(10) is called, it notifies all observers with the new state.
  3. Final Answer:

    observer1: 10 observer2: 10 -> Option A
  4. Quick Check:

    All observers notified with new state = Output A [OK]
Quick Trick: All registered observers get notified on state change [OK]
Common Mistakes:
MISTAKES
  • Assuming only one observer notified
  • Thinking observers get old state
  • Believing no notification happens

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes