Bird
Raised Fist0

Consider the following code snippet:

medium📝 Analysis Q4 of Q15
LLD - Behavioral Design Patterns — Part 1
Consider the following code snippet:
class Door {
  void open() { print('Door opened'); }
  void close() { print('Door closed'); }
}

class DoorOpenCommand implements Command {
  Door door;
  DoorOpenCommand(this.door);
  void execute() { door.open(); }
}

void main() {
  Door door = Door();
  Command command = DoorOpenCommand(door);
  command.execute();
}

What will be the output when this code runs?
ADoor opened
BDoor closed
CNo output
DCompilation error
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the Door class

    Door has methods open() and close() that print respective messages.
  2. Step 2: Check DoorOpenCommand execute()

    execute() calls door.open(), so it will print 'Door opened'.
  3. Step 3: main() calls command.execute()

    Thus, output is 'Door opened'.
  4. Final Answer:

    Door opened -> Option A
  5. Quick Check:

    execute calls door.open() prints 'Door opened' [OK]
Quick Trick: execute calls door.open(), prints 'Door opened' [OK]
Common Mistakes:
MISTAKES
  • Assuming door.close() is called
  • Expecting no output or errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes