Bird
Raised Fist0
Cprogramming~20 mins

Switch vs if comparison - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Switch vs If 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
One
Two
Three
B
Two
Three
C
Two
D
Default
Attempts:
2 left
💡 Hint
Remember that switch cases fall through unless you use break.
❓ Predict Output
intermediate
2:00remaining
If-else equivalent output
What is the output of this if-else code?
C
#include <stdio.h>

int main() {
    int x = 3;
    if (x == 1) {
        printf("One\n");
    } else if (x == 2) {
        printf("Two\n");
    } else if (x == 3) {
        printf("Three\n");
    } else {
        printf("Default\n");
    }
    return 0;
}
A
One
B
Two
C
Three
D
Default
Attempts:
2 left
💡 Hint
Check which condition matches the value of x.
❓ Predict Output
advanced
2:00remaining
Switch with non-constant case labels
What error does this code produce?
C
#include <stdio.h>

int main() {
    int a = 1;
    int b = 2;
    switch (a) {
        case b:
            printf("B\n");
            break;
        default:
            printf("Default\n");
    }
    return 0;
}
AOutput: Default
BRuntime error: segmentation fault
COutput: B
DCompilation error: case label does not reduce to an integer constant
Attempts:
2 left
💡 Hint
Case labels must be constant expressions in C.
🧠 Conceptual
advanced
2:00remaining
Difference in evaluation between switch and if
Which statement about switch and if is true in C?
ASwitch evaluates the expression once and jumps to matching case; if evaluates conditions sequentially.
BSwitch evaluates all cases before executing any code.
CIf evaluates all conditions before executing the matching block.
DSwitch can evaluate ranges like if conditions can.
Attempts:
2 left
💡 Hint
Think about how switch jumps directly to a case.
❓ Predict Output
expert
2:00remaining
Output of nested switch and if
What is the output of this code?
C
#include <stdio.h>

int main() {
    int x = 1, y = 2;
    switch (x) {
        case 1:
            if (y == 2) {
                printf("A\n");
                break;
            }
            printf("B\n");
            break;
        case 2:
            printf("C\n");
            break;
        default:
            printf("D\n");
    }
    return 0;
}
A
A
B
B
C
C
D
D
Attempts:
2 left
💡 Hint
Check the if condition inside case 1 and where the breaks are.

Practice

(1/5)
1. Which statement best describes when to use switch instead of if in C?
easy
A. switch is best for checking one variable against many fixed values.
B. switch can handle complex range conditions better than if.
C. if is only used for simple true/false checks.
D. if cannot be used with multiple conditions.

Solution

  1. Step 1: Understand switch usage

    switch works well when you compare one variable to many fixed values like 1, 2, 3.
  2. Step 2: Compare with if

    if is more flexible and can handle ranges or complex conditions, but switch is clearer for fixed values.
  3. Final Answer:

    switch is best for checking one variable against many fixed values. -> Option A
  4. Quick Check:

    Use switch for fixed values [OK]
Hint: Use switch for many fixed values, if for ranges [OK]
Common Mistakes:
  • Thinking switch handles ranges
  • Believing if can't handle multiple conditions
  • Confusing switch with if for complex logic
2. Which of the following is the correct syntax for a switch statement in C?
easy
A. switch (variable) { case 1: doSomething(); break; default: doDefault(); }
B. switch variable { case 1: doSomething(); break; default: doDefault(); }
C. switch (variable) do { case 1: doSomething(); break; }
D. switch (variable) { if case 1: doSomething(); }

Solution

  1. Step 1: Check switch syntax

    The correct syntax uses parentheses around the variable and curly braces for cases.
  2. Step 2: Identify correct case and break usage

    Each case ends with a colon and usually a break to prevent fall-through.
  3. Final Answer:

    switch (variable) { case 1: doSomething(); break; default: doDefault(); } -> Option A
  4. Quick Check:

    Parentheses + cases + breaks = correct switch [OK]
Hint: Remember switch(variable) with braces and case labels [OK]
Common Mistakes:
  • Omitting parentheses around variable
  • Using if inside switch incorrectly
  • Missing break causing fall-through
3. What will be the output of this C code?
int x = 2;
switch (x) {
  case 1: printf("One\n"); break;
  case 2: printf("Two\n");
  case 3: printf("Three\n"); break;
  default: printf("Default\n");
}
medium
A. Two
B. Two Three
C. Three
D. Default

Solution

  1. Step 1: Identify matching case

    The variable x is 2, so case 2 matches and prints "Two".
  2. Step 2: Check for break after case 2

    There is no break after case 2, so execution falls through to case 3 and prints "Three".
  3. Final Answer:

    Two Three -> Option B
  4. Quick Check:

    Missing break causes fall-through [OK]
Hint: No break after case causes next case to run too [OK]
Common Mistakes:
  • Assuming break after every case automatically
  • Ignoring fall-through behavior
  • Thinking default runs if case matches
4. Find the error in this code snippet:
int num = 3;
switch (num) {
  case 1:
    printf("One\n");
  case 2:
    printf("Two\n");
    break;
  default:
    printf("Default\n");
}
medium
A. Case labels must be strings, not numbers.
B. Switch variable must be a character, not int.
C. Missing break after case 1 causes fall-through.
D. Default case must be last, but it is not.

Solution

  1. Step 1: Check breaks after cases

    Case 1 lacks a break, so if num is 1, it will fall through to case 2 and print both lines.
  2. Step 2: Validate switch rules

    Switch can use int variables, default can be anywhere, and case labels are numbers here, which is correct.
  3. Final Answer:

    Missing break after case 1 causes fall-through. -> Option C
  4. Quick Check:

    Always add break to avoid fall-through [OK]
Hint: Add break after each case to prevent fall-through [OK]
Common Mistakes:
  • Thinking switch only works with chars
  • Believing default must be last
  • Confusing case label types
5. You want to print a message based on a score variable:
- "Fail" if score < 50
- "Pass" if score is between 50 and 75 inclusive
- "Excellent" if score > 75

Which approach is best in C?
hard
A. Use a switch with default only and if inside cases.
B. Use a switch statement with cases for each score range.
C. Use multiple switch statements for each condition.
D. Use if-else statements to check score ranges.

Solution

  1. Step 1: Analyze score conditions

    The conditions involve ranges (less than 50, between 50 and 75, greater than 75).
  2. Step 2: Choose suitable control structure

    Switch works only with fixed values, not ranges, so if-else is better for ranges.
  3. Final Answer:

    Use if-else statements to check score ranges. -> Option D
  4. Quick Check:

    Ranges need if-else, not switch [OK]
Hint: Use if for ranges, switch for fixed values only [OK]
Common Mistakes:
  • Trying to use switch for ranges
  • Using multiple switches unnecessarily
  • Confusing default case with range checks
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard