0
0
Kotlinprogramming~10 mins

When expression as powerful switch in Kotlin - Interactive Code Practice

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

Complete 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'
A"Weekend"
B"Holiday"
C"Workday"
D"Funday"
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a wrong string like "Holiday" instead of "Weekend".
2fill in blank
medium

Complete 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'
Acolor.size
Bcolor.count()
Ccolor.length()
Dcolor.length
Attempts:
3 left
💡 Hint
Common Mistakes
Using color.length() which is invalid in Kotlin.
3fill in blank
hard

Fix 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'
A"1"
B1
Cone
D'1'
Attempts:
3 left
💡 Hint
Common Mistakes
Using "1" or '1' which are strings or chars, not integers.
4fill in blank
hard

Fill 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'
A>
B<
C==
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using == for positive or negative checks.
5fill in blank
hard

Fill 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'
A..
Buntil
CrangeTo
DdownTo
Attempts:
3 left
💡 Hint
Common Mistakes
Using until which excludes the end value.