Bird
0
0

Given the following Python-like pseudocode for an Interpreter pattern, what will be the output?

medium📝 Analysis Q13 of 15
LLD - Behavioral Design Patterns — Part 2
Given the following Python-like pseudocode for an Interpreter pattern, what will be the output?
class TerminalExpression:
    def __init__(self, data):
        self.data = data
    def interpret(self, context):
        return self.data in context

class AndExpression:
    def __init__(self, expr1, expr2):
        self.expr1 = expr1
        self.expr2 = expr2
    def interpret(self, context):
        return self.expr1.interpret(context) and self.expr2.interpret(context)

expr1 = TerminalExpression('apple')
expr2 = TerminalExpression('banana')
and_expr = AndExpression(expr1, expr2)
print(and_expr.interpret(['apple', 'banana', 'cherry']))
ATrue
BFalse
CError due to missing method
DNone
Step-by-Step Solution
Solution:
  1. Step 1: Evaluate TerminalExpression interpret calls

    expr1.interpret checks if 'apple' is in the list ['apple', 'banana', 'cherry'] -> True. expr2.interpret checks if 'banana' is in the list -> True.
  2. Step 2: Evaluate AndExpression interpret

    AndExpression returns True if both expr1 and expr2 interpret return True. Both are True, so result is True.
  3. Final Answer:

    True -> Option A
  4. Quick Check:

    Both terms in list -> True [OK]
Quick Trick: AND expression true only if both sub-expressions true [OK]
Common Mistakes:
MISTAKES
  • Assuming 'in' checks keys instead of values
  • Confusing AND with OR logic
  • Forgetting to return boolean result

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes