Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The range 1..10 includes numbers from 1 to 10 inclusive, which matches the condition.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase type names like 'string' instead of 'String'.
Checking for wrong types like Int when expecting String.
✗ Incorrect
The 'is' keyword checks the type of the input. We want to check if it is a String.
3fill in blank
hardFix 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'
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.
✗ Incorrect
The 'in' keyword is needed before the range to check if number is inside it.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up types or using wrong type names.
Forgetting to use 'is' keyword before type.
✗ Incorrect
We check if input is Int or String using 'is' keyword and then apply conditions.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong type names or forgetting 'is'.
Confusing 'in' and '!in' operators.
✗ Incorrect
We check if input is Int, then use 'in' and '!in' to check ranges inside nested when.