0
0
Embedded Cprogramming~20 mins

Hierarchical state machine concept in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Hierarchical State Machine Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of nested state transitions

Consider a hierarchical state machine with states STATE_A and STATE_B, where STATE_B has substates SUBSTATE_B1 and SUBSTATE_B2. The machine starts in STATE_A. What is the output after transitioning to SUBSTATE_B2?

Embedded C
#include <stdio.h>

enum State {STATE_A, STATE_B};
enum SubState {SUBSTATE_B1, SUBSTATE_B2};

int main() {
    enum State current_state = STATE_A;
    enum SubState current_substate = SUBSTATE_B1;

    // Transition to STATE_B
    current_state = STATE_B;
    // Transition to SUBSTATE_B2
    current_substate = SUBSTATE_B2;

    if (current_state == STATE_B) {
        if (current_substate == SUBSTATE_B1) {
            printf("In SUBSTATE_B1\n");
        } else if (current_substate == SUBSTATE_B2) {
            printf("In SUBSTATE_B2\n");
        }
    } else {
        printf("In STATE_A\n");
    }
    return 0;
}
AIn SUBSTATE_B1
BIn STATE_A
CIn SUBSTATE_B2
DNo output
Attempts:
2 left
💡 Hint

Check which state and substate the machine is in before printing.

🧠 Conceptual
intermediate
1:30remaining
Understanding parent state behavior

In a hierarchical state machine, what happens when an event is not handled in a substate?

AThe event is ignored and no action is taken.
BThe event is passed up to the parent state to handle.
CThe machine resets to the initial state.
DThe machine crashes due to unhandled event.
Attempts:
2 left
💡 Hint

Think about how hierarchical states share behavior.

🔧 Debug
advanced
2:00remaining
Identify the bug in hierarchical state transition

What error does the following code produce when trying to transition from STATE_B to SUBSTATE_B2?

Embedded C
#include <stdio.h>

enum State {STATE_A, STATE_B};
enum SubState {SUBSTATE_B1, SUBSTATE_B2};

int main() {
    enum State current_state = STATE_A;
    enum SubState current_substate;

    current_state = STATE_B;
    // Forgot to initialize current_substate

    if (current_state == STATE_B) {
        if (current_substate == SUBSTATE_B2) {
            printf("In SUBSTATE_B2\n");
        } else {
            printf("In other substate\n");
        }
    }
    return 0;
}
AUndefined behavior due to uninitialized variable
BRuntime error: Segmentation fault
CNo output printed
DCompilation error: 'current_substate' used uninitialized
Attempts:
2 left
💡 Hint

Check if all variables are properly initialized before use.

📝 Syntax
advanced
1:30remaining
Syntax error in hierarchical state machine code

Which option contains the correct syntax to define a hierarchical state machine with a parent state STATE_B and substate SUBSTATE_B1?

Aenum State {STATE_A, STATE_B}; enum SubState {SUBSTATE_B1, SUBSTATE_B2};
Benum State {STATE_A, STATE_B {SUBSTATE_B1, SUBSTATE_B2}};
Cenum State {STATE_A, STATE_B}; enum State SUBSTATE_B1, SUBSTATE_B2;
Denum State {STATE_A, STATE_B}; enum SubState SUBSTATE_B1, SUBSTATE_B2;
Attempts:
2 left
💡 Hint

Remember how enums are declared separately in C.

🚀 Application
expert
2:30remaining
Number of active states in hierarchical state machine

In a hierarchical state machine with 3 parent states, each having 2 substates, how many states are active simultaneously during normal operation?

A3
B6
C1
D4
Attempts:
2 left
💡 Hint

Think about how hierarchical states are active together.