Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the PaymentStrategy interface.
LLD
interface [1] {
void pay(int amount);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a class name instead of an interface name.
Choosing a name that does not reflect the strategy pattern.
✗ Incorrect
The interface name should be PaymentStrategy to follow the pattern naming.
2fill in blank
mediumComplete the code to implement the pay method in CreditCardPayment class.
LLD
class CreditCardPayment implements PaymentStrategy { public void [1](int amount) { System.out.println("Paid " + amount + " using Credit Card."); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name than the interface.
Forgetting to implement the interface method.
✗ Incorrect
The method must be named pay to implement the interface method.
3fill in blank
hardFix the error in the PaymentContext constructor to accept a PaymentStrategy.
LLD
class PaymentContext { private PaymentStrategy strategy; public PaymentContext([1] strategy) { this.strategy = strategy; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a concrete class type instead of the interface.
Mismatching parameter type and field type.
✗ Incorrect
The constructor parameter type must be PaymentStrategy to accept any payment strategy.
4fill in blank
hardFill both blanks to complete the pay method call in PaymentContext.
LLD
class PaymentContext { private PaymentStrategy strategy; public void pay(int amount) { strategy.[1]([2]); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method name like 'process'.
Passing wrong argument like 'value'.
✗ Incorrect
The method pay is called with the amount parameter.
5fill in blank
hardFill all three blanks to create a PaymentContext with PayPalPayment and pay 100.
LLD
PaymentStrategy strategy = new [1](); PaymentContext context = new PaymentContext([2]); context.[3](100);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong payment class like CreditCardPayment.
Passing wrong variable to context.
Calling wrong method name.
✗ Incorrect
Create a PayPalPayment instance, pass it as strategy to context, then call pay with amount 100.