Bird
0
0

Find the bug in this Interpreter pattern implementation:

medium📝 Analysis Q7 of 15
LLD - Behavioral Design Patterns — Part 2
Find the bug in this Interpreter pattern implementation:
class OrExpression:
    def __init__(self, expr1, expr2):
        self.expr1 = expr1
        self.expr2 = expr2
    def interpret(self, context):
        return self.expr1.interpret(context) or self.expr2.interpret

expr = OrExpression(TerminalExpression('A'), TerminalExpression('B'))
print(expr.interpret('A'))
Ainterpret method should return void
BOrExpression should not have __init__ method
CMissing parentheses when calling expr2.interpret
DTerminalExpression class is undefined
Step-by-Step Solution
Solution:
  1. Step 1: Inspect interpret method in OrExpression

    expr2.interpret is referenced without parentheses, so it is a method object, not a call.
  2. Step 2: Understand impact

    Without parentheses, the or operation uses a method object, causing incorrect logic or runtime error.
  3. Final Answer:

    Missing parentheses when calling expr2.interpret -> Option C
  4. Quick Check:

    Method call missing parentheses = Bug [OK]
Quick Trick: Always call interpret() with parentheses [OK]
Common Mistakes:
MISTAKES
  • Forgetting parentheses on method calls
  • Assuming method reference equals method call

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes