Bird
Raised Fist0

Given the following code snippet, what will be the output?

medium📝 Analysis Q13 of Q15
LLD - Behavioral Design Patterns — Part 2
Given the following code snippet, what will be the output?
class Mediator {
  notify(sender, event) {
    if (event === 'A') return 'Handled A';
    if (event === 'B') return 'Handled B';
    return 'Unknown event';
  }
}

const mediator = new Mediator();
console.log(mediator.notify('Component1', 'B'));
AHandled A
BError: notify method missing
CHandled B
DUnknown event
Step-by-Step Solution
Solution:
  1. Step 1: Analyze notify method logic

    The method returns 'Handled A' if event is 'A', 'Handled B' if event is 'B', else 'Unknown event'.
  2. Step 2: Check the call with event 'B'

    The call is mediator.notify('Component1', 'B'), so it matches the second condition and returns 'Handled B'.
  3. Final Answer:

    Handled B -> Option C
  4. Quick Check:

    Event 'B' returns 'Handled B' [OK]
Quick Trick: Match event string exactly in notify method [OK]
Common Mistakes:
MISTAKES
  • Confusing event 'B' with 'A'
  • Assuming default case triggers for known events
  • Expecting error due to missing parameters

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes