Bird
Raised Fist0

In the following code snippet implementing the Interpreter pattern, what is the error?

medium📝 Analysis Q14 of Q15
LLD - Behavioral Design Patterns — Part 2
In the following code snippet implementing the Interpreter pattern, what is the error?
class OrExpression:
    def __init__(self, expr1, expr2):
        self.expr1 = expr1
        self.expr2 = expr2
    def interpret(self, context):
        return self.expr1.interpret(context) | self.expr2.interpret(context)
AUsing bitwise OR operator instead of logical OR
BMissing return statement in interpret method
CIncorrect constructor parameters
Dinterpret method missing context parameter
Step-by-Step Solution
Solution:
  1. Step 1: Identify operator used in interpret method

    The code uses the bitwise OR operator '|' instead of the logical OR operator 'or' for boolean logic.
  2. Step 2: Explain why this is an error

    Bitwise OR can cause unexpected results with booleans and is not the intended logical operation for combining expressions.
  3. Final Answer:

    Using bitwise OR operator instead of logical OR -> Option A
  4. Quick Check:

    Logical OR needs 'or', not '|' [OK]
Quick Trick: Use 'or' for logical OR, not '|' [OK]
Common Mistakes:
MISTAKES
  • Confusing bitwise and logical operators
  • Forgetting to return a value
  • Incorrect method signatures

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes