Bird
Raised Fist0
Javaprogramming~10 mins

Switch statement in Java - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start a switch statement on the variable day.

Java
switch([1]) {
    case 1:
        System.out.println("Monday");
        break;
    default:
        System.out.println("Other day");
}
Drag options to blanks, or click blank then click option'
Aday
Bswitch
C1
Dcase
Attempts:
3 left
💡 Hint
Common Mistakes
Putting a number instead of the variable inside switch parentheses.
Writing 'switch' or 'case' inside the parentheses.
2fill in blank
medium

Complete the code to print "Weekend" when day is 6 or 7.

Java
switch(day) {
    case 6:
    case [1]:
        System.out.println("Weekend");
        break;
    default:
        System.out.println("Weekday");
}
Drag options to blanks, or click blank then click option'
A7
B5
C8
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 5 or 8 instead of 7 for Sunday.
Adding break between case 6 and case 7, which stops fall-through.
3fill in blank
hard

Fix the error by completing the code to handle the default case correctly.

Java
int day = 3;
switch(day) {
    case 1:
        System.out.println("Monday");
        break;
    case 2:
        System.out.println("Tuesday");
        break;
    default:
        System.out.println([1]);
}
Drag options to blanks, or click blank then click option'
A"Unknown day
BUnknown day
CUnknown day"
D"Unknown day"
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the quotes around the string.
Using mismatched or incomplete quotes.
4fill in blank
hard

Fill both blanks to complete the switch statement that prints the name of the month for month 1 or 2.

Java
switch(month) {
    case [1]:
        System.out.println("January");
        break;
    case [2]:
        System.out.println("February");
        break;
    default:
        System.out.println("Other month");
}
Drag options to blanks, or click blank then click option'
A1
B3
C2
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or 3 instead of 1 or 2.
Mixing up the order of months.
5fill in blank
hard

Fill all three blanks to create a switch that prints the day name for day 1, 2, or 3.

Java
switch([1]) {
    case [2]:
        System.out.println("Monday");
        break;
    case 2:
        System.out.println("Tuesday");
        break;
    case 3:
        System.out.println([3]);
        break;
    default:
        System.out.println("Invalid day");
}
Drag options to blanks, or click blank then click option'
Aday
B1
C"Wednesday"
D"Monday"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a number instead of the variable in the switch.
Forgetting quotes around the day name string.

Practice

(1/5)
1. What is the main purpose of a switch statement in Java?
easy
A. To repeat a block of code multiple times
B. To select one action from many based on a value
C. To define a new class
D. To handle errors and exceptions

Solution

  1. Step 1: Understand the role of switch

    The switch statement chooses one path from many based on a value.
  2. Step 2: Compare with other options

    Loops repeat code, classes define objects, and exceptions handle errors, so they don't match.
  3. Final Answer:

    To select one action from many based on a value -> Option B
  4. Quick Check:

    Switch = Select action based on value [OK]
Hint: Switch picks one case based on value, like a menu choice [OK]
Common Mistakes:
  • Confusing switch with loops
  • Thinking switch handles errors
  • Mixing switch with class definitions
2. Which of the following is the correct syntax to start a switch statement in Java?
easy
A. switch (variable) { ... }
B. switch variable { ... }
C. switch [variable] { ... }
D. switch variable() { ... }

Solution

  1. Step 1: Recall switch syntax

    The correct syntax uses parentheses around the variable: switch (variable) { ... }.
  2. Step 2: Check other options

    Options A, B, and D miss parentheses or use wrong brackets, causing syntax errors.
  3. Final Answer:

    switch (variable) { ... } -> Option A
  4. Quick Check:

    Switch needs parentheses around variable [OK]
Hint: Switch always uses parentheses around the variable [OK]
Common Mistakes:
  • Omitting parentheses
  • Using square brackets instead of parentheses
  • Adding parentheses after variable name
3. What will be the output of this 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. Wednesday
B. Tuesday
C. Invalid day
D. Monday

Solution

  1. Step 1: Identify the value of day

    The variable day is set to 3.
  2. Step 2: Match the case in switch

    Case 3 matches, so it prints "Wednesday" and then breaks out of the switch.
  3. Final Answer:

    Wednesday -> Option A
  4. Quick Check:

    day=3 matches case 3 = Wednesday [OK]
Hint: Match variable value to case label to find output [OK]
Common Mistakes:
  • Forgetting break causes fall-through
  • Choosing default when case matches
  • Mixing case numbers and output strings
4. Identify the error in this switch statement:
int num = 2;
switch (num) {
  case 1:
    System.out.println("One");
  case 2:
    System.out.println("Two");
    break;
  default:
    System.out.println("Other");
}
medium
A. case labels must be strings
B. Missing default case
C. Switch variable must be a String
D. Missing break after case 1

Solution

  1. Step 1: Check breaks in cases

    Case 1 lacks a break, so execution falls through to case 2.
  2. Step 2: Understand effect of missing break

    Without break, both "One" and "Two" print when num=1, which may be unintended.
  3. Final Answer:

    Missing break after case 1 -> Option D
  4. Quick Check:

    Each case needs break to stop fall-through [OK]
Hint: Always add break after each case unless fall-through wanted [OK]
Common Mistakes:
  • Forgetting break causes multiple prints
  • Thinking default is mandatory
  • Believing switch only works with strings
5. You want to write a switch statement that prints "Weekend" for days 6 and 7, "Weekday" for days 1 to 5, and "Invalid" otherwise. Which code correctly implements this?
hard
A. switch(day) { case 1,2,3,4,5: System.out.println("Weekday"); break; case 6,7: System.out.println("Weekend"); break; default: System.out.println("Invalid"); }
B. switch(day) { case 1 || 2 || 3 || 4 || 5: System.out.println("Weekday"); break; case 6 || 7: System.out.println("Weekend"); break; default: System.out.println("Invalid"); }
C. switch(day) { case 1: case 2: case 3: case 4: case 5: System.out.println("Weekday"); break; case 6: case 7: System.out.println("Weekend"); break; default: System.out.println("Invalid"); }
D. switch(day) { case (1,2,3,4,5): System.out.println("Weekday"); break; case (6,7): System.out.println("Weekend"); break; default: System.out.println("Invalid"); }

Solution

  1. Step 1: Understand Java switch syntax for multiple cases

    Java allows multiple case labels by stacking them without commas, like case 1: case 2:.
  2. Step 2: Check each option's syntax

    switch(day) { case 1: case 2: case 3: case 4: case 5: System.out.println("Weekday"); break; case 6: case 7: System.out.println("Weekend"); break; default: System.out.println("Invalid"); } correctly stacks cases. switch(day) { case 1,2,3,4,5: System.out.println("Weekday"); break; case 6,7: System.out.println("Weekend"); break; default: System.out.println("Invalid"); } uses commas (invalid), C uses || (invalid), D uses parentheses (invalid).
  3. Final Answer:

    switch(day) { case 1: case 2: case 3: case 4: case 5: System.out.println("Weekday"); break; case 6: case 7: System.out.println("Weekend"); break; default: System.out.println("Invalid"); } -> Option C
  4. Quick Check:

    Stack multiple cases by repeating case lines [OK]
Hint: Stack multiple cases by repeating case lines without commas [OK]
Common Mistakes:
  • Using commas between case labels
  • Using logical OR (||) in case labels
  • Using parentheses around case labels
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