Bird
Raised Fist0

What will be the output of this code?

medium📝 Analysis Q5 of Q15
LLD - Behavioral Design Patterns — Part 1
What will be the output of this code?
class Receiver {
  void action() { print('Action performed'); }
}

class ConcreteCommand implements Command {
  Receiver receiver;
  ConcreteCommand(this.receiver);
  void execute() { receiver.action(); }
}

void main() {
  Receiver r = Receiver();
  Command c = ConcreteCommand(r);
  c.execute();
  c.execute();
}
AAction performed
BAction performed Action performed
CNo output
DRuntime error
Step-by-Step Solution
Solution:
  1. Step 1: Understand execute calls

    Each call to execute invokes receiver.action(), which prints 'Action performed'.
  2. Step 2: Count calls in main

    Main calls c.execute() twice, so output prints twice on separate lines.
  3. Final Answer:

    Output is 'Action performed' printed twice. -> Option B
  4. Quick Check:

    Multiple execute calls print multiple outputs [OK]
Quick Trick: Each execute triggers receiver action print [OK]
Common Mistakes:
MISTAKES
  • Assuming single print for multiple executes
  • Thinking execute does nothing
  • Confusing output format with single line

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes