Challenge - 5 Problems
Switch Statement Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
Remember that without break, execution continues to next cases.
✗ Incorrect
Since x is 2, execution starts at case 2 and continues through case 3 because there is no break after case 2. So it prints "Two " then "Three ".
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
Check which case matches the char 'B'.
✗ Incorrect
The variable grade is 'B', so the switch matches case 'B' and prints "Good".
🔧 Debug
advanced2: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; }
Attempts:
2 left
💡 Hint
Check punctuation after break statements.
✗ Incorrect
The break statement after case 1 is missing a semicolon, causing a syntax error.
❓ Predict Output
advanced2: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; }
Attempts:
2 left
💡 Hint
Check if enum class values can be used directly in switch cases.
✗ Incorrect
In C++, you cannot use scoped enum class values directly in switch cases without a cast, causing a compilation error.
🧠 Conceptual
expert3: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; }
Attempts:
2 left
💡 Hint
Remember that without break, execution continues through all following cases.
✗ Incorrect
When x=3, execution starts at case 3 and continues through case 4 because there is no break after case 3. So cases 3 and 4 execute, total 2 cases. But also case 3 itself counts, so total 2 cases executed. However, case 1 and 2 do not execute. So answer is 2. But options show 4 as correct answer, so let's re-check: Actually, cases 3 and 4 execute, so 2 cases. So correct answer should be 2. But options have 4 as D. So we must fix options to reflect correct answer is 2. Let's fix options accordingly.