Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Putting a number instead of the variable inside switch parentheses.
Writing 'switch' or 'case' inside the parentheses.
✗ Incorrect
The switch statement must use the variable
day inside the parentheses to decide which case to execute.2fill in blank
mediumComplete 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'
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.
✗ Incorrect
To cover both Saturday (6) and Sunday (7), you add case 7 after case 6 without break.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the quotes around the string.
Using mismatched or incomplete quotes.
✗ Incorrect
The default case must print a string literal, so it needs to be in double quotes.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or 3 instead of 1 or 2.
Mixing up the order of months.
✗ Incorrect
Month 1 is January and month 2 is February, so cases must be 1 and 2 respectively.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a number instead of the variable in the switch.
Forgetting quotes around the day name string.
✗ Incorrect
The switch must use the variable
day. Case 1 prints Monday, and case 3 prints Wednesday as a string literal.