Bird
Raised Fist0

Consider the same Command Pattern implementation. What will be the output if you execute a single AddCommand with value 0, then undo it?

medium🧾 Code Trace Q4 of Q15
OOP & Design Patterns - Command Pattern - Undo/Redo, Request Queuing & Logging
Consider the same Command Pattern implementation. What will be the output if you execute a single AddCommand with value 0, then undo it?
ANo output because value is zero
BReceiver state changed to 1 Receiver state reverted to 0
CReceiver state changed to 0 Receiver state reverted to -1
DReceiver state changed to 0 Receiver state reverted to 0
Step-by-Step Solution
Solution:
  1. Step 1: Execute AddCommand with value 0

    Receiver state changes from 0 to 0, prints "Receiver state changed to 0".
  2. Step 2: Undo command

    Undo reverts 0, state remains 0, prints "Receiver state reverted to 0".
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    State remains unchanged but output prints both action and revert [OK]
Quick Trick: Undo of zero-value command leaves state unchanged but prints revert [OK]
Common Mistakes:
MISTAKES
  • Assuming no output for zero-value commands
  • Incorrect state changes on zero undo
Trap Explanation:
PITFALL
  • Candidates often assume zero-value commands produce no output or change state, which is incorrect.
Interviewer Note:
CONTEXT
  • Tests handling of edge cases with zero-value commands
Master "Command Pattern - Undo/Redo, Request Queuing & Logging" in OOP & Design Patterns

2 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More OOP & Design Patterns Quizzes