Bird
Raised Fist0

Consider the following code snippet implementing the Command Pattern with undo/redo stacks. After executing the commands: Add 5, Add 3, Undo, Redo, what is the final state of the receiver?

easy🧾 Code Trace Q12 of Q15
OOP & Design Patterns - Command Pattern - Undo/Redo, Request Queuing & Logging
Consider the following code snippet implementing the Command Pattern with undo/redo stacks. After executing the commands: Add 5, Add 3, Undo, Redo, what is the final state of the receiver?
A5
B8
C3
D0
Step-by-Step Solution
Solution:
  1. Step 1: Trace command executions

    Execute Add 5 -> state = 5; Execute Add 3 -> state = 8.
  2. Step 2: Trace undo and redo

    Undo removes Add 3 -> state reverts to 5; Redo reapplies Add 3 -> state returns to 8.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    State after redo is 8 [OK]
Quick Trick: Undo then redo restores the last undone command [OK]
Common Mistakes:
MISTAKES
  • Forgetting redo reapplies the command
  • Off-by-one in stack operations
Trap Explanation:
PITFALL
  • Some think undo permanently removes commands or redo does nothing after new commands.
Interviewer Note:
CONTEXT
  • Checks candidate's ability to mentally execute command pattern with undo/redo stacks.
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