Bird
0
0

Analyze the following code snippet:

medium📝 Analysis Q4 of 15
LLD - Behavioral Design Patterns — Part 2
Analyze the following code snippet:
class NotificationSender {
  send(notification) { notification.deliver(); }
}
class NullNotification {
  deliver() { /* intentionally empty */ }
}
const sender = new NotificationSender();
sender.send(new NullNotification());

What will be the result of executing this code?
AAn exception is thrown due to calling deliver on NullNotification.
BNo action occurs, and no errors are thrown.
CThe notification is sent with default content.
DThe system logs a warning about null notification.
Step-by-Step Solution
Solution:
  1. Step 1: Understand the Null Object implementation

    The NullNotification class implements deliver() with an empty method, meaning it does nothing.
  2. Step 2: Analyze the send method

    NotificationSender calls deliver() on the passed notification object. Since NullNotification's deliver() is empty, no action occurs.
  3. Step 3: Check for errors

    Because deliver() exists and is callable, no exceptions are thrown.
  4. Final Answer:

    No action occurs, and no errors are thrown. -> Option B
  5. Quick Check:

    Null Object methods do nothing but prevent errors [OK]
Quick Trick: Null Object methods do nothing but avoid exceptions [OK]
Common Mistakes:
MISTAKES
  • Assuming Null Object throws exceptions
  • Expecting default content to be sent
  • Believing system logs warnings automatically

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes