Bird
0
0

Consider this code snippet using Null Object pattern:

medium📝 Analysis Q13 of 15
LLD - Behavioral Design Patterns — Part 2
Consider this code snippet using Null Object pattern:
class Logger {
  log(message) { console.log(message); }
}

class NullLogger {
  log(message) { /* do nothing */ }
}

function process(data, logger) {
  logger.log('Start');
  // process data
  logger.log('End');
}

const logger = new NullLogger();
process('input', logger);

What will be the output when this code runs?
ALogs 'Start' and 'End' messages to console
BNo output will be printed
CThrows an error because NullLogger does not log
DLogs only 'Start' message
Step-by-Step Solution
Solution:
  1. Step 1: Analyze NullLogger behavior

    NullLogger's log method does nothing, so no console output occurs.
  2. Step 2: Trace process function calls

    process calls logger.log twice, but since logger is NullLogger, no output is printed.
  3. Final Answer:

    No output will be printed -> Option B
  4. Quick Check:

    NullLogger logs nothing = no output [OK]
Quick Trick: Null Object methods do nothing, so no output [OK]
Common Mistakes:
MISTAKES
  • Assuming NullLogger logs messages
  • Expecting errors from NullLogger
  • Thinking partial logs appear

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes