Bird
0
0

Consider this code snippet:

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

Consider this code snippet:

class OrderStateMachine:
    def __init__(self):
        self.state = 'Pending'
    def ship(self):
        if self.state == 'Processing':
            self.state = 'Shipped'
        else:
            print('Cannot ship from', self.state)

order = OrderStateMachine()
order.ship()

What is the output?

ANo output
BShipped
CCannot ship from Pending
DError: state not defined
Step-by-Step Solution
Solution:
  1. Step 1: Check initial state of order

    Order starts in 'Pending' state.
  2. Step 2: Call ship() method

    ship() only changes state if current state is 'Processing', else prints message.
  3. Final Answer:

    Cannot ship from Pending -> Option C
  4. Quick Check:

    ship() output = Cannot ship from Pending [OK]
Quick Trick: ship() only works from Processing state [OK]
Common Mistakes:
  • Assuming ship() changes state from Pending
  • Expecting no output
  • Confusing initial state

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes