0
0
Javaprogramming~10 mins

Switch vs if comparison in Java - Interactive Practice

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

Complete the code to print the day name using switch.

Java
int day = 3;
switch(day) {
    case 1:
        System.out.println("Monday");
        break;
    case 2:
        System.out.println("Tuesday");
        break;
    case [1]:
        System.out.println("Wednesday");
        break;
    default:
        System.out.println("Other day");
}
Drag options to blanks, or click blank then click option'
A3
B4
C5
D6
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong case number that does not match the day variable.
Forgetting the break statement after the case.
2fill in blank
medium

Complete the if statement to check if the variable 'score' is greater than or equal to 90.

Java
int score = 95;
if (score [1] 90) {
    System.out.println("Excellent");
} else {
    System.out.println("Keep trying");
}
Drag options to blanks, or click blank then click option'
A>=
B<
C==
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>='.
Using '==' which checks for equality only.
3fill in blank
hard

Fix the error in the switch statement to correctly handle the variable 'option'.

Java
char option = 'B';
switch([1]) {
    case 'A':
        System.out.println("Option A selected");
        break;
    case 'B':
        System.out.println("Option B selected");
        break;
    default:
        System.out.println("Invalid option");
}
Drag options to blanks, or click blank then click option'
AOption
Boption
C'option'
Doptions
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name with wrong capitalization.
Putting the variable name in quotes, making it a char literal.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps words to their lengths only if length is greater than 3.

Java
Map<String, Integer> wordLengths = words.stream()
    .filter(word -> word.length() [1] 3)
    .collect(Collectors.toMap(word -> word, word -> word.[2]()));
Drag options to blanks, or click blank then click option'
A>
Blength()
CtoUpperCase()
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in filter.
Using toUpperCase() instead of length() for value mapping.
5fill in blank
hard

Fill all three blanks to create a switch statement that prints the season name based on the month number.

Java
int month = 4;
switch (month) {
    case [1]:
    case [2]:
    case [3]:
        System.out.println("Spring");
        break;
    default:
        System.out.println("Other season");
}
Drag options to blanks, or click blank then click option'
A3
B4
C5
D6
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong month numbers for Spring.
Not grouping multiple cases before break.