Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return the correct day type using a when expression.
Kotlin
fun dayType(day: String): String {
return when(day) {
"Saturday", "Sunday" -> [1]
else -> "Weekday"
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a wrong string like "Holiday" instead of "Weekend".
✗ Incorrect
The when expression returns "Weekend" for Saturday and Sunday, else "Weekday".
2fill in blank
mediumComplete the code to return the number of letters in the color name using when.
Kotlin
fun colorLength(color: String): Int {
return when(color) {
"Red" -> 3
"Blue" -> 4
else -> [1]
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using color.length() which is invalid in Kotlin.
✗ Incorrect
color.length returns the number of characters in the string color.
3fill in blank
hardFix the error in the when expression to correctly match the number 1.
Kotlin
fun numberName(num: Int): String {
return when(num) {
[1] -> "One"
2 -> "Two"
else -> "Other"
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using "1" or '1' which are strings or chars, not integers.
✗ Incorrect
The when expression matches the integer 1, so the correct case is 1 without quotes.
4fill in blank
hardFill both blanks to return "Positive", "Negative", or "Zero" using when with condition.
Kotlin
fun numberSign(num: Int): String {
return when {
num [1] 0 -> "Positive"
num [2] 0 -> "Negative"
else -> "Zero"
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using == for positive or negative checks.
✗ Incorrect
num > 0 means positive, num < 0 means negative, else zero.
5fill in blank
hardFill all three blanks to create a when expression that returns the season based on month number.
Kotlin
fun season(month: Int): String {
return when(month) {
in 1[1]2 -> "Winter"
in 3[2]5 -> "Spring"
in 6[3]8 -> "Summer"
else -> "Fall"
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using until which excludes the end value.
✗ Incorrect
The .. operator creates ranges like 1..2, 3..5, and 6..8 for Winter, Spring, and Summer respectively.