Bird
Raised Fist0
Cprogramming~20 mins

Switch statement - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Switch Statement Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of switch with fall-through
What is the output of this C code snippet?
C
#include <stdio.h>

int main() {
    int x = 2;
    switch (x) {
        case 1:
            printf("One\n");
        case 2:
            printf("Two\n");
        case 3:
            printf("Three\n");
            break;
        default:
            printf("Default\n");
    }
    return 0;
}
A
Two
B
One
Two
Three
C
Two
Three
D
Default
Attempts:
2 left
💡 Hint
Remember that cases fall through unless you use break.
❓ Predict Output
intermediate
2:00remaining
Value of variable after switch
What is the value of variable result after running this code?
C
#include <stdio.h>

int main() {
    int code = 3;
    int result = 0;
    switch (code) {
        case 1:
            result = 10;
            break;
        case 2:
            result = 20;
            break;
        case 3:
            result = 30;
        case 4:
            result += 5;
            break;
        default:
            result = -1;
    }
    printf("%d\n", result);
    return 0;
}
A35
B30
C-1
D20
Attempts:
2 left
💡 Hint
Check if there is a break after case 3.
❓ Predict Output
advanced
2:00remaining
Output with multiple cases combined
What is the output of this program?
C
#include <stdio.h>

int main() {
    int day = 5;
    switch (day) {
        case 1:
        case 2:
        case 3:
            printf("Weekday\n");
            break;
        case 4:
        case 5:
            printf("Almost weekend\n");
            break;
        case 6:
        case 7:
            printf("Weekend\n");
            break;
        default:
            printf("Invalid day\n");
    }
    return 0;
}
A
Weekend
B
Almost weekend
C
Weekday
D
Invalid day
Attempts:
2 left
💡 Hint
Cases 4 and 5 share the same code block.
❓ Predict Output
advanced
2:00remaining
Error caused by invalid switch expression type
What error will this code produce when compiled?
C
#include <stdio.h>

int main() {
    float value = 2.5;
    switch (value) {
        case 1.0:
            printf("One\n");
            break;
        case 2.5:
            printf("Two point five\n");
            break;
        default:
            printf("Default\n");
    }
    return 0;
}
ACompile error: switch expression must be an integer type
BRuntime error: invalid case label
COutput: Two point five
DOutput: Default
Attempts:
2 left
💡 Hint
Check the type allowed in switch expressions in C.
🧠 Conceptual
expert
3:00remaining
Number of times default executes in nested switches
Consider this nested switch code. How many times will the default case print "Default" when outer = 1 and inner = 3?
C
#include <stdio.h>

int main() {
    int outer = 1;
    int inner = 3;
    switch (outer) {
        case 1:
            switch (inner) {
                case 1:
                    printf("Inner One\n");
                    break;
                case 2:
                    printf("Inner Two\n");
                    break;
                default:
                    printf("Default\n");
            }
            break;
        default:
            printf("Default\n");
    }
    return 0;
}
A3
B2
C0
D1
Attempts:
2 left
💡 Hint
Check which switch executes default with given values.

Practice

(1/5)
1. What is the main purpose of a switch statement in C?
easy
A. To define a function
B. To repeat a block of code multiple times
C. To select one action from multiple choices based on a variable's value
D. To declare variables

Solution

  1. Step 1: Understand the role of switch

    A switch statement chooses one path among many based on a variable's value.
  2. Step 2: Compare with other options

    Loops repeat code, functions define reusable blocks, and declarations create variables, which are different from switch's purpose.
  3. Final Answer:

    To select one action from multiple choices based on a variable's value -> Option C
  4. Quick Check:

    Switch = Choice selection [OK]
Hint: Switch picks one case based on value, like a menu choice [OK]
Common Mistakes:
  • Confusing switch with loops
  • Thinking switch defines functions
  • Believing switch declares variables
2. Which of the following is the correct syntax to start a switch statement in C?
easy
A. switch (variable) { case 1: ... }
B. switch variable { case 1: ... }
C. switch [variable] { case 1: ... }
D. switch variable: case 1: ...

Solution

  1. Step 1: Recall switch syntax

    The correct syntax uses parentheses around the variable: switch(variable) { ... }
  2. Step 2: Check options

    Only switch (variable) { case 1: ... } uses parentheses correctly; others miss parentheses or use wrong brackets.
  3. Final Answer:

    switch (variable) { case 1: ... } -> Option A
  4. Quick Check:

    Switch needs parentheses around variable [OK]
Hint: Remember: switch(variable) uses parentheses, not brackets or none [OK]
Common Mistakes:
  • Omitting parentheses around variable
  • Using square brackets instead of parentheses
  • Using colon instead of braces
3. What will be the output of this C code?
int x = 2;
switch (x) {
  case 1:
    printf("One\n");
    break;
  case 2:
    printf("Two\n");
    break;
  default:
    printf("Other\n");
}
medium
A. One
B. Other
C. No output
D. Two

Solution

  1. Step 1: Identify the switch variable value

    The variable x is 2, so the switch checks case 2.
  2. Step 2: Match case and output

    Case 2 matches, so it prints "Two" and then breaks to stop further checks.
  3. Final Answer:

    Two -> Option D
  4. Quick Check:

    Case 2 matches x=2, prints "Two" [OK]
Hint: Match variable value to case, print and break [OK]
Common Mistakes:
  • Forgetting break causes fall-through
  • Choosing default when case matches
  • Confusing case numbers
4. Find the error in this switch statement:
int n = 3;
switch (n) {
  case 1:
    printf("One\n");
  case 2:
    printf("Two\n");
    break;
  default:
    printf("Default\n");
}
medium
A. Missing break after case 1
B. Missing default case
C. Wrong switch syntax
D. Variable n not declared

Solution

  1. Step 1: Check each case for break

    Case 1 lacks a break, so if matched, it would fall through to case 2.
  2. Step 2: Understand impact

    Missing break can cause unintended multiple prints; adding break after case 1 fixes this.
  3. Final Answer:

    Missing break after case 1 -> Option A
  4. Quick Check:

    Each case needs break to stop fall-through [OK]
Hint: Always add break after each case unless fall-through wanted [OK]
Common Mistakes:
  • Ignoring missing break causes fall-through
  • Thinking default is mandatory
  • Assuming variable declaration error
5. You want to write a switch statement that prints "Weekend" for cases 6 and 7, and "Weekday" for cases 1 to 5. Which code correctly does this?
hard
A. switch(day) { case 1: printf("Weekday\n"); break; case 2: printf("Weekday\n"); break; case 3: printf("Weekend\n"); break; case 4: printf("Weekday\n"); break; case 5: printf("Weekday\n"); break; case 6: printf("Weekend\n"); break; case 7: printf("Weekend\n"); break; }
B. switch(day) { case 1: case 2: case 3: case 4: case 5: printf("Weekday\n"); break; case 6: case 7: printf("Weekend\n"); break; default: printf("Invalid\n"); }
C. switch(day) { case 1: case 2: case 3: case 4: case 5: printf("Weekend\n"); break; case 6: case 7: printf("Weekday\n"); break; }
D. switch(day) { case 1: case 2: case 3: case 4: case 5: printf("Weekday\n"); case 6: case 7: printf("Weekend\n"); break; }

Solution

  1. Step 1: Group cases for same output

    Cases 1 to 5 print "Weekday", so group them before one printf and break.
  2. Step 2: Group weekend cases

    Cases 6 and 7 print "Weekend" similarly grouped with break.
  3. Step 3: Check default case

    switch(day) { case 1: case 2: case 3: case 4: case 5: printf("Weekday\n"); break; case 6: case 7: printf("Weekend\n"); break; default: printf("Invalid\n"); } includes default for invalid days, which is good practice.
  4. Final Answer:

    Groups weekday cases 1-5 and weekend cases 6-7 with breaks and default -> Option B
  5. Quick Check:

    Group cases with same output, add break after each group [OK]
Hint: Group cases with same output, add break after group [OK]
Common Mistakes:
  • Printing wrong labels for grouped cases
  • Missing breaks causing fall-through
  • Not handling default case
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard