Bird
0
0

Identify the error in this Payment Strategy Pattern implementation snippet:

medium📝 Analysis Q6 of 15
LLD - Design — Online Shopping Cart
Identify the error in this Payment Strategy Pattern implementation snippet:
class PaymentContext {
  private PaymentStrategy strategy;
  public void pay(double amount) {
    strategy.pay(amount);
  }
}
Astrategy should be static
Bstrategy is not initialized before use
Cpay method should return a value
Dpay method should be static
Step-by-Step Solution
Solution:
  1. Step 1: Check strategy usage

    strategy is used in pay method but never initialized or set, so it will cause a null pointer error.
  2. Step 2: Validate other options

    pay method does not need to return value, strategy does not need to be static, pay method does not need to be static.
  3. Final Answer:

    strategy is not initialized before use -> Option B
  4. Quick Check:

    Uninitialized strategy causes runtime error [OK]
Quick Trick: Always initialize strategy before calling pay [OK]
Common Mistakes:
  • Assuming pay must return value
  • Thinking strategy must be static
  • Confusing method static requirement

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes