Bird
Raised Fist0
Javaprogramming~20 mins

Switch vs if comparison in Java - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Switch 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 Java code snippet?
Java
int x = 2;
switch (x) {
  case 1:
    System.out.print("One ");
  case 2:
    System.out.print("Two ");
  case 3:
    System.out.print("Three ");
    break;
  default:
    System.out.print("Default ");
}
ATwo Three
BOne Two Three
CTwo
DDefault
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 equivalent to the previous switch?
Java
int x = 2;
if (x == 1) {
  System.out.print("One ");
} else if (x == 2) {
  System.out.print("Two ");
} else if (x == 3) {
  System.out.print("Three ");
} else {
  System.out.print("Default ");
}
AOne Two Three
BDefault
CTwo Three
DTwo
Attempts:
2 left
💡 Hint
If-else does not fall through cases.
🧠 Conceptual
advanced
2:00remaining
Switch expression with yield
What is the value of variable result after this Java switch expression runs?
Java
int x = 3;
String result = switch (x) {
  case 1 -> "One";
  case 2 -> "Two";
  case 3 -> {
    yield "Three";
  }
  default -> "Default";
};
System.out.print(result);
AOne
BDefault
CThree
DTwo
Attempts:
2 left
💡 Hint
Switch expressions use yield to return values from blocks.
❓ Predict Output
advanced
2:00remaining
Switch with multiple labels output
What does this code print?
Java
int day = 5;
switch (day) {
  case 1, 2, 3 -> System.out.print("Start");
  case 4, 5, 6 -> System.out.print("Middle");
  case 7 -> System.out.print("End");
  default -> System.out.print("Unknown");
}
AMiddle
BStart
CEnd
DUnknown
Attempts:
2 left
💡 Hint
Multiple labels can be combined with commas in switch cases.
❓ Predict Output
expert
2:00remaining
Switch vs if with null value
What happens when this code runs?
Java
String s = null;
switch (s) {
  case "a" -> System.out.print("A");
  case "b" -> System.out.print("B");
  default -> System.out.print("Default");
}

if (s == null) {
  System.out.print("Null");
} else if (s.equals("a")) {
  System.out.print("A");
} else {
  System.out.print("Other");
}
ADefaultNull
BThrows NullPointerException
CNullA
DNullDefault
Attempts:
2 left
💡 Hint
Switch on null reference causes an exception.

Practice

(1/5)
1. Which statement best describes when to use switch instead of if in Java?
easy
A. switch is best for checking many fixed values of a single variable.
B. switch can handle complex conditions like ranges easily.
C. if is only for checking equality, not ranges.
D. if cannot be used with multiple conditions.

Solution

  1. Step 1: Understand switch usage

    switch works well when you have one variable and many fixed values to compare against.
  2. Step 2: Compare with if

    if can handle any condition, including ranges and complex expressions, but switch is simpler for fixed values.
  3. Final Answer:

    switch is best for checking many fixed values of a single variable. -> Option A
  4. Quick Check:

    Use switch for fixed values = B [OK]
Hint: Use switch for fixed values, if for ranges or complex checks [OK]
Common Mistakes:
  • Thinking switch can handle ranges
  • Believing if is only for equality
  • Assuming switch works with multiple variables
  • Confusing switch with if-else chains
2. Which of the following is the correct syntax for a switch statement in Java?
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; default: doDefault(); }
D. switch (variable) { case 1 doSomething(); break; default doDefault(); }

Solution

  1. Step 1: Check switch syntax structure

    The correct syntax requires parentheses around the variable and braces enclosing cases.
  2. Step 2: Verify case and break usage

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

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

    Correct switch syntax uses parentheses and colons = A [OK]
Hint: Remember switch needs parentheses and colons for cases [OK]
Common Mistakes:
  • Omitting parentheses around variable
  • Using braces instead of colons after case
  • Missing break statements causing fall-through
  • Incorrect placement of default case
3. What will be the output of the following Java code?
int day = 3;
switch(day) {
  case 1: System.out.println("Monday"); break;
  case 2: System.out.println("Tuesday"); break;
  case 3: System.out.println("Wednesday"); break;
  default: System.out.println("Invalid day");
}
medium
A. Monday
B. Tuesday
C. Invalid day
D. Wednesday

Solution

  1. Step 1: Identify the value of day

    The variable day is set to 3.
  2. Step 2: Match day with switch cases

    The case for 3 prints "Wednesday" and then breaks, so no other cases run.
  3. Final Answer:

    Wednesday -> Option D
  4. Quick Check:

    day=3 matches case 3 = C [OK]
Hint: Match variable value to case label, then print [OK]
Common Mistakes:
  • Forgetting break causes fall-through
  • Choosing default instead of matching case
  • Confusing case numbers
  • Assuming if-else output instead of switch
4. Identify the error in this Java code snippet:
int score = 85;
switch(score) {
  case score >= 90: System.out.println("A grade"); break;
  case score >= 80: System.out.println("B grade"); break;
  default: System.out.println("Fail");
}
medium
A. Missing break statements after cases.
B. Cannot use relational expressions in case labels.
C. Switch variable must be a String, not int.
D. Default case must be last.

Solution

  1. Step 1: Understand switch case label restrictions

    Case labels must be constant values, not expressions like 'score >= 90'.
  2. Step 2: Identify why relational expressions cause error

    Relational expressions are evaluated at runtime and cannot be used as case labels in switch.
  3. Final Answer:

    Cannot use relational expressions in case labels. -> Option B
  4. Quick Check:

    Case labels must be constants = A [OK]
Hint: Case labels must be constants, not conditions [OK]
Common Mistakes:
  • Trying to use ranges in switch cases
  • Forgetting case labels must be constant
  • Assuming switch supports boolean expressions
  • Misplacing default case
5. You want to print a message based on an integer score: "Excellent" if score >= 90, "Good" if score >= 75, "Average" if score >= 50, and "Poor" otherwise. Which approach is best in Java?
hard
A. Use a switch statement with case labels for each range.
B. Use a switch with string values for score.
C. Use nested if-else statements to check ranges.
D. Use a switch with boolean expressions in cases.

Solution

  1. Step 1: Analyze the conditions

    The conditions involve ranges (score >= 90, >= 75, etc.) which cannot be directly used in switch cases.
  2. Step 2: Choose the suitable control structure

    if-else statements can handle ranges and complex conditions easily, unlike switch.
  3. Final Answer:

    Use nested if-else statements to check ranges. -> Option C
  4. Quick Check:

    Ranges need if-else, not switch = D [OK]
Hint: Use if-else for ranges, switch only for fixed values [OK]
Common Mistakes:
  • Trying to use switch for range conditions
  • Using boolean expressions in switch cases
  • Assuming switch works with strings for numbers
  • Ignoring else for default case
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