Bird
0
0

Identify the bug in this order state machine method that allows invalid state transitions:

medium📝 Analysis Q14 of 15
LLD - Design — Online Shopping Cart

Identify the bug in this order state machine method that allows invalid state transitions:

def deliver(self):
    if self.state == 'Shipped' or 'Out for Delivery':
        self.state = 'Delivered'
    else:
        raise Exception('Invalid transition');
AThe method should use 'and' instead of 'or'
BThe method does not change the state
CThe exception message is missing
DThe condition always evaluates to True due to incorrect or usage
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the condition logic

    The condition uses 'if self.state == 'Shipped' or 'Out for Delivery'', which always evaluates True because non-empty strings are truthy.
  2. Step 2: Correct the condition

    It should be 'if self.state == 'Shipped' or self.state == 'Out for Delivery'' to check both states properly.
  3. Final Answer:

    The condition always evaluates to True due to incorrect or usage -> Option D
  4. Quick Check:

    Incorrect or condition causes always True [OK]
Quick Trick: Check boolean conditions carefully for correct comparisons [OK]
Common Mistakes:
  • Using 'or' with string literals incorrectly
  • Forgetting to compare both sides explicitly
  • Assuming condition works as intended

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes