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 <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; }
Attempts:
2 left
💡 Hint
Remember that cases fall through unless you use break.
✗ Incorrect
Since x is 2, execution starts at case 2 and continues until a break is found. So it prints "Two" and "Three".
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
Check if there is a break after case 3.
✗ Incorrect
Since case 3 has no break, execution falls through to case 4, adding 5 to result (30 + 5 = 35).
❓ Predict Output
advanced2: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; }
Attempts:
2 left
💡 Hint
Cases 4 and 5 share the same code block.
✗ Incorrect
Since day is 5, it matches case 5, which prints "Almost weekend".
❓ Predict Output
advanced2: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; }
Attempts:
2 left
💡 Hint
Check the type allowed in switch expressions in C.
✗ Incorrect
In C, the switch expression must be an integer type. Using float causes a compile error.
🧠 Conceptual
expert3: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; }
Attempts:
2 left
💡 Hint
Check which switch executes default with given values.
✗ Incorrect
The outer switch matches case 1, so it does not print default. The inner switch with inner=3 hits default once, printing "Default" once.