Bird
Raised Fist0

Consider the following code snippet using the Strategy Pattern. What will be the output when processor.pay(0) is called with UPIStrategy?

medium🧾 Code Trace Q4 of Q15
OOP & Design Patterns - Strategy Pattern - Replace Conditionals with Polymorphism
Consider the following code snippet using the Strategy Pattern. What will be the output when processor.pay(0) is called with UPIStrategy?
AError: Amount must be positive
BProcessing UPI payment of $0
CNo output
DProcessing UPI payment of $0
Step-by-Step Solution
Solution:
  1. Step 1: Trace method call with zero amount

    UPIStrategy.pay is called with amount=0, which is valid and prints the message.
  2. Step 2: Confirm output

    The print statement outputs "Processing UPI payment of $0" exactly.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Zero amount is handled by printing message [OK]
Quick Trick: Zero amount still triggers print in strategy method [OK]
Common Mistakes:
MISTAKES
  • Assuming error on zero amount
  • Confusing output with no output
Trap Explanation:
PITFALL
  • Candidates expect error or no output on zero amount, but code prints message.
Interviewer Note:
CONTEXT
  • Tests boundary input handling in polymorphic methods.
Master "Strategy Pattern - Replace Conditionals with Polymorphism" 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