Bird
Raised Fist0

Given this pseudocode for a game state manager, what will be the output after calling changeState('PAUSED') twice?

medium📝 Analysis Q13 of Q15
LLD - Design — Chess Game
Given this pseudocode for a game state manager, what will be the output after calling changeState('PAUSED') twice?
class GameStateManager:
  def __init__(self):
    self.state = 'MENU'
  def changeState(self, new_state):
    if self.state != new_state:
      self.state = new_state
      print(f'State changed to {self.state}')
    else:
      print(f'State already {self.state}')

manager = GameStateManager()
manager.changeState('PAUSED')
manager.changeState('PAUSED')
AState already PAUSED State already PAUSED
BState changed to PAUSED State changed to PAUSED
CState changed to PAUSED State already PAUSED
DState changed to MENU State changed to PAUSED
Step-by-Step Solution
Solution:
  1. Step 1: Analyze first changeState call

    Initial state is 'MENU'. Changing to 'PAUSED' triggers state change and prints 'State changed to PAUSED'.
  2. Step 2: Analyze second changeState call

    State is already 'PAUSED', so it prints 'State already PAUSED' without changing.
  3. Final Answer:

    State changed to PAUSED State already PAUSED -> Option C
  4. Quick Check:

    Second call same state = no change message [OK]
Quick Trick: Second same state call prints 'already' message [OK]
Common Mistakes:
MISTAKES
  • Assuming state changes again on same value
  • Ignoring else branch output
  • Confusing initial state with changed state

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes