Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The case for day 3 should print "Wednesday". So, the blank must be filled with 3.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>='.
Using '==' which checks for equality only.
✗ Incorrect
To check if score is greater than or equal to 90, use '>=' operator.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name with wrong capitalization.
Putting the variable name in quotes, making it a char literal.
✗ Incorrect
The switch must use the variable 'option' exactly as declared, case sensitive and without quotes.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in filter.
Using toUpperCase() instead of length() for value mapping.
✗ Incorrect
The filter should keep words with length greater than 3, so use '>'. The value mapper uses word.length() to get length.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong month numbers for Spring.
Not grouping multiple cases before break.
✗ Incorrect
Months 3, 4, and 5 correspond to Spring season, so these cases print "Spring".