0
0
Kotlinprogramming~10 mins

When with ranges and types 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 check if number is in the range 1 to 10 using when.

Kotlin
val number = 5
val result = when (number) {
    in [1] -> "Number is between 1 and 10"
    else -> "Number is out of range"
}
Drag options to blanks, or click blank then click option'
A0..9
B1 until 10
C10..1
D1..10
Attempts:
3 left
💡 Hint
Common Mistakes
Using '1 until 10' excludes 10, so it won't match number 10.
Using reversed ranges like '10..1' does not work as expected.
2fill in blank
medium

Complete the code to check if the input is a String using when.

Kotlin
fun checkType(input: Any) = when (input) {
    is [1] -> "Input is a String"
    else -> "Input is not a String"
}
Drag options to blanks, or click blank then click option'
ADouble
BInt
CString
DBoolean
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase type names like 'string' instead of 'String'.
Checking for wrong types like Int when expecting String.
3fill in blank
hard

Fix the error in the when expression to correctly check if number is in 1..5 or 6..10.

Kotlin
val number = 7
val result = when (number) {
    [1] -> "Between 1 and 5"
    in 6..10 -> "Between 6 and 10"
    else -> "Out of range"
}
Drag options to blanks, or click blank then click option'
Anumber in 1..5
Bin 1..5
C1..5
Din 1 until 5
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting 'in' keyword before the range.
Using 'number in 1..5' inside when case which is not valid syntax.
4fill in blank
hard

Fill both blanks to check if input is Int in 1..10 or String of length > 5.

Kotlin
fun describe(input: Any) = when (input) {
    is [1] -> if (input in 1..10) "Int in range" else "Int out of range"
    is [2] -> if (input.length > 5) "Long String" else "Short String"
    else -> "Unknown"
}
Drag options to blanks, or click blank then click option'
AInt
BString
CDouble
DBoolean
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up types or using wrong type names.
Forgetting to use 'is' keyword before type.
5fill in blank
hard

Fill all three blanks to create a when expression that returns different messages for Int ranges and String lengths.

Kotlin
fun analyze(input: Any) = when (input) {
    is [1] -> when {
        input [2] 1..5 -> "Small number"
        input [3] 6..10 -> "Medium number"
        else -> "Large number"
    }
    is String -> "String of length ${input.length}"
    else -> "Unknown type"
}
Drag options to blanks, or click blank then click option'
AInt
Bin
C!in
DDouble
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong type names or forgetting 'is'.
Confusing 'in' and '!in' operators.