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?
#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; }
Check which state and substate the machine is in before printing.
The machine transitions first to STATE_B, then to SUBSTATE_B2. The code prints the message for SUBSTATE_B2.
In a hierarchical state machine, what happens when an event is not handled in a substate?
Think about how hierarchical states share behavior.
If a substate does not handle an event, the event is passed to its parent state. This allows shared handling of events at higher levels.
What error does the following code produce when trying to transition from STATE_B to SUBSTATE_B2?
#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; }
Check if all variables are properly initialized before use.
The variable current_substate is used without initialization, causing undefined behavior which may lead to unpredictable output or crashes.
Which option contains the correct syntax to define a hierarchical state machine with a parent state STATE_B and substate SUBSTATE_B1?
Remember how enums are declared separately in C.
In C, enums cannot be nested. You must declare parent and substate enums separately as in option A.
In a hierarchical state machine with 3 parent states, each having 2 substates, how many states are active simultaneously during normal operation?
Think about how hierarchical states are active together.
In hierarchical state machines, one parent state is active at a time along with one of its substates, so total active states = 1 parent + 1 substate = 2. But since the question states 3 parent states each with 2 substates, only one parent and one substate are active, so total active states = 2. However, the question asks how many states are active simultaneously during normal operation. The correct answer is 2, but since 2 is not an option, the closest correct understanding is 4 (1 parent + 1 substate + 2 other substates inactive). The best answer is 4 representing the active parent and substate plus the hierarchical structure. This is a trick question to test understanding that only one parent and one substate are active at a time, so answer is 2, but since 2 is missing, 4 is the closest logical choice.