0
0
C++programming~20 mins

Switch vs if comparison in C++ - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Switch vs If Mastery
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;
}
ATwo Three
BOne Two Three
CThree
DDefault
Attempts:
2 left
💡 Hint
Remember that switch cases fall through unless you use break.
Predict Output
intermediate
2:00remaining
If-else chain output
What is the output of this C++ code using if-else?
C++
#include <iostream>
int main() {
    int x = 2;
    if (x == 1) {
        std::cout << "One ";
    } else if (x == 2) {
        std::cout << "Two ";
    } else if (x == 3) {
        std::cout << "Three ";
    } else {
        std::cout << "Default ";
    }
    return 0;
}
ATwo
BOne Two
CTwo Three
DDefault
Attempts:
2 left
💡 Hint
If-else stops checking after the first true condition.
🧠 Conceptual
advanced
2:00remaining
Switch statement limitations
Which of the following is NOT a limitation of the C++ switch statement compared to if-else?
ASwitch only works with integral or enum types, not with strings or floats.
BSwitch cannot evaluate complex conditions like ranges or multiple variables.
CSwitch cases must be constant expressions known at compile time.
DSwitch statements always execute all cases regardless of matching.
Attempts:
2 left
💡 Hint
Think about how switch chooses which case to run.
Predict Output
advanced
2:00remaining
Output difference between switch and if
What is the output of this C++ code snippet?
C++
#include <iostream>
int main() {
    int x = 5;
    switch (x) {
        case 1:
            std::cout << "One ";
            break;
        case 2:
            std::cout << "Two ";
            break;
        default:
            std::cout << "Default switch ";
    }
    if (x == 1) {
        std::cout << "One if ";
    } else if (x == 2) {
        std::cout << "Two if ";
    } else {
        std::cout << "Default if ";
    }
    return 0;
}
ADefault if
BDefault switch
CDefault switch Default if
DOne if
Attempts:
2 left
💡 Hint
Both switch and if-else handle default cases here.
🧠 Conceptual
expert
2:00remaining
When to prefer switch over if-else
Which scenario best justifies using a switch statement instead of if-else in C++?
AWhen you need to check multiple unrelated conditions involving different variables and complex expressions.
BWhen you have a single variable with many possible constant integral values to handle distinctly.
CWhen you want to evaluate ranges or conditions involving inequalities.
DWhen you want to execute code based on string comparisons.
Attempts:
2 left
💡 Hint
Switch works best with one variable and fixed values.