Quiz- Switch Case Interview Questions for Embedded C: Quiz and Code Challenges-Free Embedded C Course in Hyderabad

Need Help or More Information?

Questions about this free C programming course, embedded C training or career guidance? Get in touch.

MODULE 6 · LESSON 6.2 · PRACTICE

Switch Case Interview Questions for Embedded C: Quiz and Code Challenges

57 questions: multiple choice, write-the-label blanks, a construct-matching table, true/false, 6 code challenges with real output and 12 detailed interview answers. Treat it as a model for switch case interview questions.

57
Questions
Beginner → Advanced
Level
Live score
Auto-graded
Live Score Tracker
0
Correct
0
Wrong
0
Attempted
Answer the auto-graded questions (MCQ, fill-in, true/false) to see your score.

Section 1: Multiple Choice Questions — switch, case, break and default

These switch case interview questions in multiple-choice form are typical of written screening tests. Click an answer; explanations always appear.

1. Which types may be used as the controlling expression of a switch?

2. What must a case label be?

3. What happens when a case does not end with break?

4. What does break do inside a switch that is inside a for loop?

5. What does continue do inside a switch that is inside a for loop?

6. Where may the default label appear?

7. What happens if no case matches and there is no default?

8. Which flag makes GCC warn about unannotated fall-through?

9. Why should a case body that declares a variable be in braces?

10. Which case label is valid?

11. A dense switch with many cases is most likely compiled to:

12. Which switch is best expressed with a lookup table instead?

13. Which MISRA C rule demands a default label?

14. What is a case 1 ... 5: range?

15. Which statement about switch on an enum is true?

Section 2: Write or Evaluate (Fill in the Blanks)

Type the exact C label or keyword for the first three items and a short value for the rest.

1. Write the label that matches the character A:

2. Write the statement that leaves a switch:

3. Write the label that handles every unmatched value:

4. switch (3) { case 3: x = 1; case 4: x += 2; break; case 5: x += 4; } gives x = (x starts at 0)

5. switch (9) { case 1: x = 5; break; } with x = 7 beforehand leaves x =

6. How many break statements are needed to stack labels case 1: case 2: case 3: over one body?

7. The number of default labels allowed in one switch is at most

8. The standard keyword to leave only the switch is

Section 3: Which Construct Fits?

Choose the best tool, as asked in switch case interview questions about design.

TaskYour answerResult
Decode a one-byte UART command into twelve different actionsswitchifswitch with grouped labelsconst lookup tableenum + switchdeliberate fall-throughif-elsebraces around the case
Check whether a temperature is above 30 °Cswitchifswitch with grouped labelsconst lookup tableenum + switchdeliberate fall-throughif-elsebraces around the case
Map month numbers to day countsswitchifswitch with grouped labelsconst lookup tableenum + switchdeliberate fall-throughif-elsebraces around the case
Map an index 0–4 to a port name stringswitchifswitch with grouped labelsconst lookup tableenum + switchdeliberate fall-throughif-elsebraces around the case
Protocol parser that remembers where it is between bytesswitchifswitch with grouped labelsconst lookup tableenum + switchdeliberate fall-throughif-elsebraces around the case
Cases that build on each other (init levels)switchifswitch with grouped labelsconst lookup tableenum + switchdeliberate fall-throughif-elsebraces around the case
Choose the action by comparing two variablesswitchifswitch with grouped labelsconst lookup tableenum + switchdeliberate fall-throughif-elsebraces around the case
A case that needs a local variableswitchifswitch with grouped labelsconst lookup tableenum + switchdeliberate fall-throughif-elsebraces around the case

Section 4: True or False

Statements typical of switch case interview questions.

1. Every case in a switch must end with a break statement for the code to compile.

2. A switch can have the same case value twice if the bodies differ.

3. The expression in a switch is evaluated once.

4. switch (str) with case "ON": compares the string contents.

5. A return inside a case does the job of a break.

6. The order of case labels affects which one matches.

7. A switch can be used to implement a finite state machine.

8. const int k = 3; switch (x) { case k: ... } is valid C.

Section 5: Code Snippet Challenges

Trace each program on paper first; the real output follows. These challenges are the code-reading half of switch case interview questions.

Challenge 1 — Output?: Missing breaks
#include <stdio.h> int main(void) { int x = 2; switch (x) { case 1: printf(“one “); case 2: printf(“two “); case 3: printf(“three “); default: printf(“other “); } printf(“\n”); return 0; }

What is printed for x = 2?

Output:
two three other 
Execution enters at case 2 and falls through case 3 and default, printing all three words.
Challenge 2 — Output?: default in the middle
#include <stdio.h> int main(void) { int x = 7; switch (x) { default: printf(“D “); case 1: printf(“one “); break; case 2: printf(“two “); } printf(“\n”); return 0; }

What is printed for x = 7?

Output:
D one 
No case matches, so control goes to default, which prints D and then falls into case 1 until the break. The position of default does not change that.
Challenge 3 — Output?: break and continue in a loop
#include <stdio.h> int main(void) { int n = 0; for (int i = 0; i < 4; i++) { switch (i) { case 1: continue; case 2: break; default: n += 10; } n++; } printf(“%d\n”, n); return 0; }

What is n at the end?

Output:
23
i = 0: default adds 10, then n++ gives 11. i = 1: continue skips n++. i = 2: break leaves the switch, n++ gives 12. i = 3: default adds 10 giving 22, n++ gives 23.
Challenge 4 — Find the bug: Two labels with the same value
#include <stdio.h> int main(void) { char c = 5; switch (c) { case 5: printf(“five\n”); break; case 4 + 1: printf(“also five\n”); break; } return 0; }

Does this compile?

Compiler result:dup.c: In function ‘main’: dup.c:10:9: error: duplicate case value 10 | case 4 + 1: | ^~~~ dup.c:7:9: note: previously used here 7 | case 5: | ^~~~The constant expression 4 + 1 equals 5, so the label duplicates the first one: a compile error.
Challenge 5 — Find the bug: Declaration right after a label
#include <stdio.h> int main(void) { int a = 1; switch (a) { case 1: int t = 5; printf(“%d\n”, t); break; case 2: printf(“two\n”); break; } return 0; }

What does GCC say about this switch?

Compiler result:decl.c: In function ‘main’: decl.c:8:13: warning: a label can only be part of a statement and a declaration is not a statement [-Wpedantic] 8 | int t = 5; | ^~~Before C23 a label must be followed by a statement. GCC accepts it as an extension in its default mode and warns under -pedantic (C11 mode shown); the portable and safe form is to wrap the case body in braces.
Challenge 6 — Output?: A shift turns a range into a value
#include <stdio.h> int main(void) { unsigned v = 7; switch (v >> 2) { case 0: printf(“0..3\n”); break; case 1: printf(“4..7\n”); break; default: printf(“8+\n”); break; } return 0; }

What is printed for v = 7?

Output:
4..7
7 >> 2 is 1, so the second case runs. The shift maps 0–3 to 0, 4–7 to 1 and everything larger to other values.

Section 6: Switch Case Interview Questions for Embedded C (with Detailed Answers)

Switch case interview questions test your knowledge of fall-through, constants, compilation and state-machine design. Click a question for a model answer.

Q1. What is the difference between switch and if-else?Basic+

A switch compares one integer expression with integer constants and jumps to the matching label; if-else can test any condition, including ranges, floating-point values and several variables. A switch with dense labels can compile to a jump table. The price is the fall-through hazard and the restriction to constants.

Q2. What does default do and is it mandatory?Basic+

Default runs when no case label matches. C does not require it, but MISRA C rule 16.4 does, and good practice uses it to catch unexpected values, for example to log an error or to move a state machine into a safe state.

Q3. What is fall-through? When is it acceptable?Intermediate+

Execution continues from one case into the next when no break stops it. Stacking labels with no code between them is normal. Fall-through with code in between is acceptable only when deliberate, annotated (/* fall through */ or __attribute__((fallthrough))), and reviewed, because it is the most common switch bug.

fallthrough_iq.c
#include <stdio.h> int main(void) { int n = 3, total = 0; switch (n) { case 3: total += 4; /* fall through */ case 2: total += 2; /* fall through */ case 1: total += 1; break; } printf(“%d\n”, total); return 0; }
Output (verified by compiling and running)
7
Q4. How is a switch implemented by the compiler?Intermediate+

Depending on the case values the compiler produces a comparison chain (few cases), a jump table with a bounds check (dense values; on Cortex-M the TBB or TBH instruction), a binary search over comparisons (sparse values) or a lookup table when the cases only produce values. At -O0 the result can differ from -O2, so look at the disassembly when timing matters.

Q5. Can you use a string in a switch? What do you do instead?Intermediate+

No, a switch needs integer expressions. Options: an else-if ladder with strcmp; a table of (string, enum) pairs searched linearly or by binary search and followed by a switch on the enum; a hash of the string computed at run time with compile-time constants only if you have a constant hash. Embedded parsers often switch on the first character and then verify the rest.

Q6. Why put braces around a case that defines variables?Intermediate+

A case label does not create a scope; the variable would be visible in all following cases, and a jump to a later label skips its initialiser, leaving the value indeterminate. Braces limit the scope to that case. Also, before C23 a declaration cannot directly follow a label.

Q7. Write a state machine for a debounced button using a switch.Intermediate+
debounce_fsm_iq.c
#include <stdio.h> typedef enum { UP, MAYBE_DOWN, DOWN, MAYBE_UP } bstate_t; static bstate_t st = UP; static int step(int raw) /* returns the debounced level */ { switch (st) { case UP: if (raw) st = MAYBE_DOWN; break; case MAYBE_DOWN: st = raw ? DOWN : UP; break; case DOWN: if (!raw) st = MAYBE_UP; break; case MAYBE_UP: st = raw ? DOWN : UP; break; default: st = UP; break; } return st == DOWN || st == MAYBE_UP; } int main(void) { int raw[] = { 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0 }; for (int i = 0; i < 11; i++) printf(“raw=%d -> %d\n”, raw[i], step(raw[i])); return 0; }
Output (verified by compiling and running)
raw=0 -> 0 raw=1 -> 0 raw=0 -> 0 raw=1 -> 0 raw=1 -> 1 raw=1 -> 1 raw=0 -> 1 raw=1 -> 1 raw=0 -> 1 raw=0 -> 0 raw=0 -> 0

A change of the raw input must persist for two consecutive samples before the output follows. A single-sample glitch is ignored because the machine returns to its previous stable state.

Q8. When would you replace a switch with a function-pointer table?Advanced+

When there are many cases (dozens of commands), each case calls a function, and the index is dense. A table of function pointers such as handler[cmd]() adds new commands by adding a row, can be stored in flash, and allows unit tests of each handler. Always bounds-check the index and handle NULL entries. Lesson 8.4 covers function pointers.

Q9. A switch on a state variable occasionally does nothing in the field. What could be happening?Advanced+

The state variable probably contains a value that matches no case and the switch has no default: corrupted by a stack overflow, a buffer overrun, an uninitialised variable or concurrent modification from an ISR without volatile or locking. A default that logs and resets the machine turns a silent failure into a diagnosable one. Also check for enum values cast from raw integers.

Q10. Why can switch (c) misbehave for bytes above 127?Advanced+

char may be signed (x86 GCC) or unsigned (ARM GCC). A byte 0xE9 then becomes -23 or 233 and case labels written as character constants may not match as expected. Use uint8_t for data bytes and compare against unsigned constants, and keep char for text.

Q11. What are the MISRA C rules about switch statements?Advanced+

Rule 16.1: switch statements shall be well formed. 16.2: a switch label shall only be used when the most closely enclosing compound statement is the body of a switch (no Duff’s device). 16.3: an unconditional break shall terminate every switch clause. 16.4: every switch shall have a default. 16.5: default shall appear first or last. 16.6: every switch shall have at least two clauses. 16.7: a switch expression shall not have essentially Boolean type. Rules differ slightly between the 2004 and 2012 editions, so check the one your project follows.

Q12. What is Duff’s device and why is it banned in MISRA code?Advanced+

It is an unrolled copy loop that interleaves a switch with a do-while so that the case labels jump into the middle of the loop. It is legal C and saves loop overhead, but it violates the structured-switch rule and is hard to read; modern compilers unroll loops themselves. Know it for interviews; do not use it in production firmware.

Study Plan: Using Your Score on These Switch Case Interview Questions

ScoreMeaningNext step
90–100%ExcellentContinue to Module 7: loops, starting with the for loop.
70–89%GoodRedo Examples 3, 5 and 8 of this lesson.
Below 70%ReviseRe-read theory sections 3, 4 and 6, then retake the quiz.
Habit to build: Compile with -Wall -Wextra and treat every fall-through warning as a bug until you have annotated it.

Need Help or More Information?

Questions about this free C programming course, embedded C training or career guidance? Get in touch.

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *