0
0
Javaprogramming~10 mins

Switch statement in Java - Interactive Code Practice

Choose your learning style9 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.