State Pattern — Concept Check
Questions on State Pattern. Allows an object to alter its behavior when its internal state changes, appearing to change its class.
Questions in this quiz
8 questions on State Pattern — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A vending machine in the 'HasMoney' state has a balance of ₹15. A user selects a product priced at ₹20. What happens next?
- AThe machine dispenses the product and transitions to 'Dispensing' state.
- BThe machine displays 'Insufficient balance. Need ₹20'.
- CThe machine transitions to 'OutOfStock' state.
- DThe machine refunds the balance and transitions to 'Idle' state.
2.In which state does the vending machine allow coins to be inserted but not select a product?
- ADispensing
- BIdle
- CHasMoney
- DOutOfStock
3.A vending machine is in the 'Dispensing' state. A user tries to insert a coin. What happens?
- AThe machine ignores the coin and continues dispensing.
- BThe machine refunds the coin immediately.
- CThe machine accepts the coin and updates the balance.
- DThe machine transitions to 'HasMoney' state.
4.What is the role of the 'OutOfStock' state in a vending machine?
- ATo dispense products without checking stock.
- BTo allow coin insertion but not product selection.
- CTo refund any inserted money automatically.
- DTo prevent any operations and notify the user of stock shortage.
5.A vending machine in 'HasMoney' state has a balance of ₹30. A user selects a product priced at ₹20. What is the next state?
- AOutOfStock
- BHasMoney
- CDispensing
- DIdle
6.In the 'Idle' state, what happens if a user tries to dispense a product without inserting a coin?
- AThe machine transitions to 'HasMoney' state.
- BThe machine refunds any nonexistent balance.
- CThe machine prompts to insert a coin first.
- DThe machine dispenses the product anyway.
7.Why might someone incorrectly believe the 'Dispensing' state allows for refunds?
- AThey think 'Dispensing' manages all money operations.
- BThey confuse 'Dispensing' with 'Idle' state.
- CThey assume refunds are possible at any state.
- DThey believe 'Dispensing' is after 'OutOfStock' state.
8.What operation is allowed in the 'OutOfStock' state of a vending machine?
- AProduct selection is allowed but ignored.
- BDispensing is allowed without stock.
- CCoin insertion is allowed but refunded.
- DNone, all operations are blocked.