Bird
0
0

Identify the issue in this Python Strategy pattern implementation:

medium📝 Analysis Q7 of 15
LLD - Behavioral Design Patterns — Part 1
Identify the issue in this Python Strategy pattern implementation:
class Strategy:
def execute(self):
pass
class ConcreteStrategyB(Strategy):
def execute(self):
print('B')
class Context:
def __init__(self, strategy):
self.strategy = strategy
def execute(self):
self.strategy.execute
AStrategy class should not have an execute method
BMissing parentheses when calling execute method
CContext class should not store strategy as attribute
DConcreteStrategyB should not inherit from Strategy
Step-by-Step Solution
Solution:
  1. Step 1: Review Context's execute method

    It calls self.strategy.execute without parentheses.
  2. Step 2: Understand method invocation in Python

    Methods must be called with parentheses to execute.
  3. Step 3: Identify the bug

    Missing parentheses means the method is referenced but not called, so no output occurs.
  4. Final Answer:

    Missing parentheses when calling execute method -> Option B
  5. Quick Check:

    Method call requires parentheses [OK]
Quick Trick: Method calls need parentheses in Python [OK]
Common Mistakes:
MISTAKES
  • Forgetting parentheses on method calls
  • Thinking base class shouldn't have method
  • Misunderstanding inheritance necessity

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes