0
0
Cprogramming~20 mins

Switch statement - 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 <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.