Bird
Raised Fist0
C++programming~20 mins

Switch statement in C++ - 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 <iostream>
int main() {
    int x = 2;
    switch (x) {
        case 1:
            std::cout << "One ";
        case 2:
            std::cout << "Two ";
        case 3:
            std::cout << "Three ";
            break;
        default:
            std::cout << "Default ";
    }
    return 0;
}
AOne Two Three
BTwo Three
CTwo
DDefault
Attempts:
2 left
💡 Hint
Remember that without break, execution continues to next cases.
❓ Predict Output
intermediate
2:00remaining
Switch with char variable output
What will this program print?
C++
#include <iostream>
int main() {
    char grade = 'B';
    switch (grade) {
        case 'A':
            std::cout << "Excellent";
            break;
        case 'B':
            std::cout << "Good";
            break;
        case 'C':
            std::cout << "Fair";
            break;
        default:
            std::cout << "Poor";
    }
    return 0;
}
APoor
BExcellent
CFair
DGood
Attempts:
2 left
💡 Hint
Check which case matches the char 'B'.
🔧 Debug
advanced
2:00remaining
Identify the error in switch statement
What error will this code produce when compiled?
C++
#include <iostream>
int main() {
    int num = 1;
    switch (num) {
        case 1:
            std::cout << "One";
            break
        case 2:
            std::cout << "Two";
            break;
        default:
            std::cout << "Other";
    }
    return 0;
}
ASyntax error: missing semicolon after break
BRuntime error: break statement invalid
CLogical error: prints wrong output
DNo error, runs fine
Attempts:
2 left
💡 Hint
Check punctuation after break statements.
❓ Predict Output
advanced
2:00remaining
Switch with enum class output
What will this program print?
C++
#include <iostream>
enum class Color { Red, Green, Blue };
int main() {
    Color c = Color::Green;
    switch (c) {
        case Color::Red:
            std::cout << "Red";
            break;
        case Color::Green:
            std::cout << "Green";
            break;
        case Color::Blue:
            std::cout << "Blue";
            break;
    }
    return 0;
}
ACompilation error
BRed
CBlue
DGreen
Attempts:
2 left
💡 Hint
Check if enum class values can be used directly in switch cases.
🧠 Conceptual
expert
3:00remaining
Number of cases executed in switch with fall-through
Given the code below, how many case labels will execute when x = 3?
C++
#include <iostream>
int main() {
    int x = 3;
    switch (x) {
        case 1:
            std::cout << "One ";
        case 2:
            std::cout << "Two ";
        case 3:
            std::cout << "Three ";
        case 4:
            std::cout << "Four ";
            break;
        default:
            std::cout << "Default ";
    }
    return 0;
}
A3
B2
C4
D1
Attempts:
2 left
💡 Hint
Remember that without break, execution continues through all following cases.

Practice

(1/5)
1. What is the main purpose of a switch statement in C++?
easy
A. To declare a variable
B. To repeat a block of code multiple times
C. To define a function
D. To choose which block of code to run based on a single value

Solution

  1. Step 1: Understand the role of switch

    A switch statement checks one value and runs code matching that value.
  2. Step 2: Compare with other options

    Repeating code is done by loops, functions define reusable code, and variables store data.
  3. Final Answer:

    To choose which block of code to run based on a single value -> Option D
  4. Quick Check:

    Switch selects code by value = A [OK]
Hint: Switch picks code based on one value quickly [OK]
Common Mistakes:
  • Confusing switch with loops
  • Thinking switch defines functions
  • Mixing switch with variable declaration
2. Which of the following is the correct syntax to start a switch statement for variable choice?
easy
A. switch(choice) {
B. switch choice {
C. switch choice() {
D. switch(choice);

Solution

  1. Step 1: Recall switch syntax

    The switch statement requires parentheses around the variable: switch(variable).
  2. Step 2: Check each option

    switch(choice) { uses correct parentheses and opening brace. Others miss parentheses or use wrong punctuation.
  3. Final Answer:

    switch(choice) { -> Option A
  4. Quick Check:

    Correct switch syntax uses parentheses [OK]
Hint: Switch needs parentheses around variable [OK]
Common Mistakes:
  • Omitting parentheses
  • Using semicolon instead of brace
  • Adding parentheses after variable name
3. What will be the output of this code?
int x = 2;
switch(x) {
  case 1:
    std::cout << "One";
    break;
  case 2:
    std::cout << "Two";
    break;
  default:
    std::cout << "Other";
}
medium
A. One
B. Two
C. Other
D. No output

Solution

  1. Step 1: Check the value of x

    The variable x is 2.
  2. Step 2: Match case in switch

    The switch matches case 2, so it prints "Two" and then breaks.
  3. Final Answer:

    Two -> Option B
  4. Quick Check:

    Value 2 matches case 2 = Two [OK]
Hint: Match variable value to case label [OK]
Common Mistakes:
  • Forgetting break causes fall-through
  • Choosing default when case matches
  • Confusing output with case number
4. Identify the error in this switch statement:
int n = 3;
switch(n) {
  case 1:
    std::cout << "One";
  case 2:
    std::cout << "Two";
    break;
  default:
    std::cout << "Default";
}
medium
A. default case must be last
B. Variable n should be a string
C. Missing break after case 1 causes fall-through
D. case labels must be strings

Solution

  1. Step 1: Check case 1 block

    There is no break; after case 1, so execution falls through to case 2.
  2. Step 2: Understand consequences

    Without break, both "One" and "Two" print if n=1. This is usually unintended.
  3. Final Answer:

    Missing break after case 1 causes fall-through -> Option C
  4. Quick Check:

    Break prevents fall-through = A [OK]
Hint: Always add break after each case to stop fall-through [OK]
Common Mistakes:
  • Forgetting break causes multiple outputs
  • Thinking default must be last (it can be anywhere)
  • Believing case labels must be strings
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 snippet correctly does this?
hard
A. switch(day) { case 1: case 2: case 3: case 4: case 5: std::cout << "Weekday"; break; case 6: case 7: std::cout << "Weekend"; break; default: std::cout << "Invalid"; }
B. switch(day) { case 1: case 2: case 3: case 4: case 5: std::cout << "Weekend"; break; case 6: case 7: std::cout << "Weekday"; break; default: std::cout << "Invalid"; }
C. switch(day) { case 1: case 2: case 3: case 4: case 5: std::cout << "Weekday"; case 6: case 7: std::cout << "Weekend"; break; default: std::cout << "Invalid"; }
D. switch(day) { case 1: case 2: case 3: case 4: case 5: std::cout << "Weekend"; case 6: case 7: std::cout << "Weekday"; break; default: std::cout << "Invalid"; }

Solution

  1. Step 1: Group cases for weekdays and weekends

    Cases 1 to 5 print "Weekday"; cases 6 and 7 print "Weekend". Grouping cases with no code between them is correct.
  2. Step 2: Check breaks and outputs

    switch(day) { case 1: case 2: case 3: case 4: case 5: std::cout << "Weekday"; break; case 6: case 7: std::cout << "Weekend"; break; default: std::cout << "Invalid"; } groups cases correctly and uses breaks to prevent fall-through. Others mix outputs or miss breaks.
  3. Final Answer:

    Option A code correctly prints "Weekday" for 1-5 and "Weekend" for 6-7 -> Option A
  4. Quick Check:

    Group cases + breaks = correct switch [OK]
Hint: Group cases without code, add break after each group [OK]
Common Mistakes:
  • Missing breaks causing fall-through
  • Swapping outputs for cases
  • Not grouping cases properly
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