Challenge - 5 Problems
State Machine Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple state machine
What is the output of this simple state machine code snippet?
Embedded C
#include <stdio.h> int main() { enum State {IDLE, RUNNING, STOPPED} state = IDLE; switch(state) { case IDLE: printf("Idle state\n"); break; case RUNNING: printf("Running state\n"); break; case STOPPED: printf("Stopped state\n"); break; } return 0; }
Attempts:
2 left
💡 Hint
Look at the initial value of the state variable.
✗ Incorrect
The variable 'state' is initialized to IDLE, so the switch-case executes the IDLE case and prints "Idle state".
❓ Predict Output
intermediate2:00remaining
State transition output
What will be printed after this code runs?
Embedded C
#include <stdio.h> int main() { enum State {IDLE, RUNNING, STOPPED} state = RUNNING; switch(state) { case IDLE: printf("Idle\n"); break; case RUNNING: printf("Running\n"); state = STOPPED; break; case STOPPED: printf("Stopped\n"); break; } switch(state) { case IDLE: printf("Idle\n"); break; case RUNNING: printf("Running\n"); break; case STOPPED: printf("Stopped\n"); break; } return 0; }
Attempts:
2 left
💡 Hint
Check how the state variable changes after the first switch.
✗ Incorrect
Initially, state is RUNNING, so first switch prints "Running" and sets state to STOPPED. The second switch then prints "Stopped".
🔧 Debug
advanced2:00remaining
Identify the error in this state machine code
What error will this code produce when compiled?
Embedded C
#include <stdio.h> int main() { enum State {IDLE, RUNNING, STOPPED} state = IDLE; switch(state) { case IDLE printf("Idle\n"); break; case RUNNING: printf("Running\n"); break; case STOPPED: printf("Stopped\n"); break; } return 0; }
Attempts:
2 left
💡 Hint
Check the syntax of the case labels in the switch statement.
✗ Incorrect
The case label 'case IDLE' is missing a colon ':', causing a syntax error during compilation.
❓ Predict Output
advanced2:00remaining
Output with fall-through in switch-case
What will this program print?
Embedded C
#include <stdio.h> int main() { enum State {IDLE, RUNNING, STOPPED} state = IDLE; switch(state) { case IDLE: printf("Idle\n"); case RUNNING: printf("Running\n"); break; case STOPPED: printf("Stopped\n"); break; } return 0; }
Attempts:
2 left
💡 Hint
Notice the missing break after case IDLE.
✗ Incorrect
Because there is no break after case IDLE, execution falls through to case RUNNING and prints both lines.
🧠 Conceptual
expert2:00remaining
Number of states handled by this state machine
Given this state machine code, how many distinct states does it handle?
Embedded C
#include <stdio.h> int main() { enum State {IDLE = 0, RUNNING = 1, PAUSED = 2, STOPPED = 3} state = PAUSED; switch(state) { case IDLE: printf("Idle\n"); break; case RUNNING: case PAUSED: printf("Active\n"); break; case STOPPED: printf("Stopped\n"); break; } return 0; }
Attempts:
2 left
💡 Hint
Count how many unique cases produce output.
✗ Incorrect
The switch handles IDLE, RUNNING/PAUSED together, and STOPPED, so 3 distinct states are handled.