0
0
C++programming~20 mins

Switch statement in C++ - Practice Problems & Coding Challenges

Choose your learning style9 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.