Bird
0
0

Consider the following code snippet:

medium📝 Analysis Q4 of 15
LLD - Design — Online Shopping Cart
Consider the following code snippet:
PaymentContext context = new PaymentContext();
context.setPaymentStrategy(new PayPalPayment());
context.pay(75);

Assuming PayPalPayment's pay method prints "Processed payment of 75 via PayPal", what will be the output?
AProcessed payment of 75 via PayPal
BPaid 75 using Credit Card
CPayment of 75 failed
DNo payment strategy set
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the code

    The PaymentContext is instantiated without a strategy initially.
  2. Step 2: Set the payment strategy

    The strategy is set to PayPalPayment using setPaymentStrategy.
  3. Step 3: Call the pay method

    Calling pay(75) invokes the pay method of PayPalPayment, which prints "Processed payment of 75 via PayPal".
  4. Final Answer:

    Processed payment of 75 via PayPal -> Option A
  5. Quick Check:

    Strategy set correctly and method called [OK]
Quick Trick: Set strategy before pay call to get correct output [OK]
Common Mistakes:
  • Assuming default strategy is CreditCardPayment
  • Forgetting to set the payment strategy before calling pay
  • Confusing output messages of different payment methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes