Bird
0
0

Analyze the following code snippet implementing the Interpreter pattern:

medium📝 Analysis Q6 of 15
LLD - Behavioral Design Patterns — Part 2
Analyze the following code snippet implementing the Interpreter pattern:
class TerminalExpression:
    def __init__(self, symbol):
        self.symbol = symbol
    def interpret(self, context):
        return self.symbol == context

expr = TerminalExpression('Z')
print(expr.interpret())
What is the error in this code?
Ainterpret() method is called without required argument
BConstructor missing required parameter
CClass name should be ExpressionTerminal
DReturn statement uses wrong comparison operator
Step-by-Step Solution
Solution:
  1. Step 1: Check interpret method signature

    interpret requires one argument: context.
  2. Step 2: Check method call

    expr.interpret() is called without any argument, causing a TypeError.
  3. Final Answer:

    interpret() method is called without required argument -> Option A
  4. Quick Check:

    Method parameters must match call arguments [OK]
Quick Trick: interpret() needs context argument when called [OK]
Common Mistakes:
MISTAKES
  • Calling interpret() without passing context
  • Assuming default argument for interpret
  • Confusing constructor parameters with method parameters

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes