C Switch Quiz and Interview- Best C Programming Course

Need Help or More Information?

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

Free C Programming Course • Control Statements

C Switch Quiz and Interview

Test your knowledge of the C switch statement with 10 quiz questions, 4 predict-the-output puzzles, practice challenges and 12 interview questions for embedded C roles.

C Switch Statement Quiz

This final lecture on the switch statement turns knowledge into skill. The C switch statement quiz below covers case labels, default, fall-through, break, continue and menu driven programs from the last four lectures of this free C programming course. Afterwards you will find interview questions that hiring managers commonly ask in embedded C programming interviews. Everything here is part of our free C tutorial, so attempt each question on paper first, then open the answer.

What Is Inside

10 MCQs 4 output puzzles 3 practice labs 12 interview questions free embedded C course

What You Will Learn

Use this page to:

  • Check that you can predict switch behaviour without running the code.
  • Spot fall-through, missing default and loop-related traps.
  • Practise explaining switch design choices in interview language.
  • Find weak areas to revisit in the earlier lectures.

Prerequisites

Finish the four previous lectures of this chapter: switch basics, the break statement, case grouping, and menu driven programs.

Quick Revision Table

ConceptRemember
Switch expressionMust be an integer type; may be a function call
Case labelsUnique integer constants, no variables, no ranges in standard C
defaultOptional, may appear anywhere, runs when nothing matches
No breakExecution falls into the next label
breakLeaves the switch only, never the surrounding loop
continueValid only when an enclosing loop exists
Grouped labelsStack empty labels over one shared block

Multiple Choice Quiz

Choose one option for each question, then open the answer.

  1. Which data type can NOT be used as a switch expression in C?

    • A. char
    • B. enum
    • C. float
    • D. long
    Show answer

    C. Floating point values are not allowed. Switch needs an integer type, and the case labels must be integer constants.

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

    • A. Compile error
    • B. The first case runs
    • C. The whole switch is skipped
    • D. Runtime crash
    Show answer

    C. Control passes to the statement after the switch and nothing inside runs.

  3. Can the default label appear before the case labels?

    • A. No, default must be last
    • B. Yes, labels can be in any order
    • C. Only with a break
    • D. Only in C23
    Show answer

    B. Position does not change which label is chosen. It only affects the downward fall-through direction.

  4. What is the term for running into the next case because break is missing?

    • A. Branching
    • B. Fall through
    • C. Looping
    • D. Short circuit
    Show answer

    B. Fall through.

  5. Which of these is a valid case label?

    • A. case x: (x is an int variable)
    • B. case 2.5:
    • C. case ‘k’:
    • D. case “on”:
    Show answer

    C. A character constant is an integer constant. Variables, floating values and strings are not valid labels.

  6. Inside a for loop, a switch case executes break. What does it exit?

    • A. The for loop
    • B. The function
    • C. Only the switch
    • D. The program
    Show answer

    C. Break applies to the nearest enclosing switch or loop, which here is the switch.

  7. Using continue in a switch that is NOT inside any loop causes:

    • A. A compile error
    • B. It behaves like break
    • C. It restarts the switch
    • D. Undefined output at runtime only
    Show answer

    A. The compiler reports that continue is not within a loop.

  8. Which technique lets ‘x’, ‘X’ and ‘*’ all trigger multiplication?

    • A. Three separate blocks
    • B. Stack the three labels over one block
    • C. Use a range
    • D. Use goto
    Show answer

    B. Empty labels stacked over one shared block.

  9. To stop a menu loop from inside the switch, the best approach is:

    • A. Write break in the exit case only
    • B. Set a flag the loop condition checks
    • C. Remove the default
    • D. Add more cases
    Show answer

    B. Break would only leave the switch. A flag (or return) is needed to end the loop.

  10. Why is break recommended after the last case?

    • A. The compiler requires it
    • B. It speeds up the program
    • C. It prevents spill-over when new cases are added later
    • D. It saves memory
    Show answer

    C. It is a maintenance safeguard; coding standards often require it.

Predict the Output Puzzles

Work out the printed text for each snippet. All four were compiled and run to confirm the answers.

Puzzle 1

int n = 2;
switch (n) {
case 1: printf("A");
case 2: printf("B");
case 3: printf("C"); break;
default: printf("D");
}
Show output
BC

Entry at case 2, falls into case 3, break stops before default.

Puzzle 2

int n = 5;
switch (n) {
default: printf("X");
case 1: printf("Y"); break;
case 2: printf("Z");
}
Show output
XY

No label matches 5, so default runs, then falls into case 1, which breaks.

Puzzle 3

for (int i = 0; i < 4; i++) {
    switch (i) {
    case 1: continue;
    case 2: break;
    default: printf("%d", i);
    }
    printf("-");
}
Show output
0--3-

i=0 prints 0 then a dash; i=1 skips everything with continue; i=2 breaks and prints only a dash; i=3 prints 3 and a dash.

Puzzle 4

int k = 3, r = 0;
switch (k) {
case 1: r += 1;
case 3: r += 3;
case 4: r += 4; break;
case 5: r += 5;
}
printf("%d\n", r);
Show output
7

Case 3 adds 3, falls into case 4 which adds 4, then breaks: 3 + 4 = 7.

Practice Labs

Lab 1: Traffic Light Controller

Read a number 1 to 3 and print RED, YELLOW or GREEN. Use default for invalid values. Then change it so that “3” also prints a reminder to watch for pedestrians using deliberate fall-through, with a comment.

Lab 2: Case-Insensitive Command Parser

Read one character. Treat s/S as start, p/P as pause and q/Q as quit, using tolower on the switch expression. Print “unknown” for anything else.

Lab 3: Sensor Console Menu

Create a menu driven program with options: read temperature (print a fixed fake value), change unit between Celsius and Fahrenheit, show current unit, exit. Keep the unit in a variable declared before the loop and stop the loop with a flag.

Interview Questions on the C Switch Statement

These questions are typical of embedded C and firmware interviews. Practise answering them aloud in your own words.

1. What is the difference between switch and if-else?

Switch compares one integer expression against constant labels and can be compiled into a jump table. If-else handles any condition, including ranges, floats and logical combinations, and is evaluated in order.

2. Which types are allowed in the switch expression?

Integer types including char, short, int, long and enums. Floating point values, strings and structures are not allowed.

3. What is fall through and when is it useful?

Execution continuing into the next label because no break intervenes. It is useful for cumulative behaviour and for grouped labels, provided it is commented.

4. Is default mandatory? Where can it be placed?

It is optional and can be placed anywhere in the block. Safety standards such as MISRA C expect a default branch.

5. Can two cases have the same value?

No. Duplicate case values cause a compile error.

6. Can case labels be variables or ranges?

Labels must be integer constant expressions. Standard C has no ranges; GCC offers a non-portable extension.

7. What does break do inside a switch within a loop?

It exits only the switch. The loop continues with the statement after the switch.

8. How do you leave a loop from inside a switch?

Set a flag tested by the loop condition, use return, or jump to a cleanup label. Break alone is not enough.

9. Is continue valid in a switch?

Only when the switch is inside a loop; then it jumps to the next iteration. Otherwise it is a compile error.

10. How would you implement a firmware state machine?

Keep the state in an enum variable, switch on it inside the main loop, perform the work for the current state, and assign the next state before break. Include a default that moves to a safe or error state.

11. Why might a compiler generate a jump table for a switch?

For dense integer labels, a table indexed by the value gives constant-time dispatch with no comparison chain. Sparse values may instead use binary search or comparisons.

12. How do you stop the implicit fall through warning for intended fall through?

Add a fall through comment before the next label, use the C23 fallthrough attribute, or the GCC attribute, only where the behaviour is intended.

Common Mistakes Checklist

CheckQuestion to ask yourself
BreakDoes every case end with break or a commented fall-through?
DefaultIs there a default that handles bad input?
Loop exitDoes the exit option change something the loop can see?
InputDid I check the return value of scanf?
DuplicatesIs any case value listed twice?

Summary and Key Takeaways

  • You can now read, write and debug switch statements with confidence.
  • Fall-through, break scope and loop exit are the three classic traps.
  • Grouped labels and tolower keep switches short and readable.
  • Menu driven programs and firmware state machines both rely on the same pattern.
  • Always handle default, validate input and compile with warnings enabled.

If any quiz answer surprised you, revisit the matching lecture and re-run its example. This finishes the switch section of the control statements chapter in our free embedded C course.

Frequently Asked Questions

Where can I practise C switch statement questions?

Use the quiz, output puzzles and labs on this page, then write your own small programs and compile them with warnings turned on.

What switch questions are asked in embedded interviews?

Common topics are fall-through, break versus continue in loops, default handling, allowed expression types, state machines and jump table generation.

Is this embedded C course really free?

Yes. EmbeddedPathashala publishes the full course, lectures, quizzes and interview questions at no cost.

Should beginners avoid fall through?

Avoid accidental fall-through completely. Use deliberate fall-through only with a comment so readers and compilers know it is intended.

What should I learn after the switch statement?

Continue with the remaining control statements in this chapter such as goto and loop control, then move on to functions.

Suggested Images

  • Quiz scoreboard graphic: ALT “C switch statement quiz free C programming course”.
  • Fall-through puzzle annotated diagram: ALT “switch fall through output puzzle embedded C programming”.
  • Interview preparation checklist: ALT “embedded C interview questions switch free embedded C course”.

Keep Going for Free

Switch is done. Continue your journey through the EmbeddedPathashala free embedded C course.

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 *