The when expression in Kotlin is used as a replacement for the switch statement in other languages. It allows you to check a value against multiple conditions, including ranges and types, and execute code based on the matching case.
Click to reveal answer
beginner
How do you check if a value is within a range using when in Kotlin?
You use the in keyword inside a when branch to check if a value falls within a range. For example: in 1..10 checks if the value is between 1 and 10 inclusive.
Click to reveal answer
beginner
How can you check the type of a variable in a when expression?
You can use the is keyword in a when branch to check if a variable is of a certain type. For example: is String checks if the variable is a String.
Click to reveal answer
intermediate
What happens if none of the when branches match a value?
If none of the branches match, and there is no else branch, the when expression will throw a NoWhenBranchMatchedException. To avoid this, always include an else branch as a fallback.
Click to reveal answer
beginner
Write a simple Kotlin when expression that prints "Small" if a number is between 1 and 10, "Medium" if between 11 and 100, and "Large" otherwise.
```kotlin
val number = 25
when (number) {
in 1..10 -> println("Small")
in 11..100 -> println("Medium")
else -> println("Large")
}
```
Click to reveal answer
Which keyword is used in Kotlin's when expression to check if a value is within a range?
Ain
Bis
Crange
Dwithin
✗ Incorrect
The keyword in is used to check if a value falls inside a range, like in 1..10.
How do you check if a variable is of type String in a when expression?
Ais String
Bin String
Ctype String
Dequals String
✗ Incorrect
The is keyword checks the type of a variable, for example is String.
What must you include in a when expression to handle all unmatched cases?
Acatch
Bdefault
Celse
Dfinally
✗ Incorrect
The else branch acts as a fallback for any unmatched cases in a when expression.
What will this when expression print if value = 15? when(value) { in 1..10 -> "Low"; in 11..20 -> "Medium"; else -> "High" }
ALow
BMedium
CHigh
DError
✗ Incorrect
Since 15 is in the range 11 to 20, the expression returns "Medium".
Can when be used without an argument in Kotlin?
ANo, it always needs an argument
BOnly with types
COnly with ranges
DYes, it can be used as an if-else chain
✗ Incorrect
When used without an argument, when acts like an if-else chain checking conditions.
Explain how to use the when expression in Kotlin to check if a number falls within certain ranges.
Think about how you check if a number is between two values.
You got /5 concepts.
Describe how to use when to check the type of a variable and why this might be useful.
Consider how Kotlin can automatically treat a variable as a specific type after checking.
You got /5 concepts.
Practice
(1/5)
1. What does the when expression in Kotlin do when used with ranges like in 1..5?
easy
A. It throws an error if the value is outside the range.
B. It creates a new range from 1 to 5.
C. It assigns the value to the range variable.
D. It checks if the value falls within the specified range.
Solution
Step 1: Understand the when with range syntax
The syntax in 1..5 inside when checks if the value is between 1 and 5 inclusive.
Step 2: Interpret the check
If the value is inside the range, the corresponding branch executes; otherwise, it moves to the next case.
Final Answer:
It checks if the value falls within the specified range. -> Option D
Quick Check:
when with in checks range membership [OK]
Hint: Remember: in means inside the range [OK]
Common Mistakes:
Thinking in creates a range instead of checking membership
Confusing in with assignment
Assuming it throws errors for out-of-range values
2. Which of the following is the correct syntax to check if a variable x is NOT in the range 10 to 20 using when in Kotlin?
easy
A. when(x) { !in 10..20 -> "Outside" }
B. when(x) { not in 10..20 -> "Outside" }
C. when(x) { x !in 10..20 -> "Outside" }
D. when(x) { outside 10..20 -> "Outside" }
Solution
Step 1: Recall the correct negation syntax in when
In Kotlin, !in is used to check if a value is NOT in a range.
Step 2: Match the syntax with options
when(x) { !in 10..20 -> "Outside" } uses !in 10..20 correctly inside when. Other options use invalid keywords or syntax.
Final Answer:
when(x) { !in 10..20 -> "Outside" } -> Option A
Quick Check:
Use !in to check exclusion from range [OK]
Hint: Use !in to say NOT in range [OK]
Common Mistakes:
Using 'not in' instead of '!in'
Writing variable name inside when case
Using undefined keywords like 'outside'
3. What will be the output of this Kotlin code?
val x: Any = 42
val result = when (x) {
is Int -> "Integer"
in 1..10 -> "Small number"
else -> "Unknown"
}
println(result)
medium
A. Small number
B. Unknown
C. Integer
D. Compilation error
Solution
Step 1: Check the type of x
Variable x is of type Any but holds the value 42, which is an Int.
Step 2: Evaluate when branches in order
The first branch checks is Int, which is true, so it returns "Integer". The second branch is not checked because the first matched.
Final Answer:
Integer -> Option C
Quick Check:
when matches first true branch [OK]
Hint: Type checks with is come before range checks [OK]
Common Mistakes:
Assuming range check runs before type check
Confusing value 42 as outside 1..10
Thinking when checks all branches
4. Identify the error in this Kotlin when expression:
val y: Any = "Hello"
val output = when (y) {
in 1..5 -> "Number"
is String -> "Text"
else -> "Other"
}
println(output)
medium
A. Cannot use in with Any type without casting
B. The is String check should come before the range check
C. Missing break statements in when
D. No error, code runs fine
Solution
Step 1: Analyze the in 1..5 check on Any
The variable y is of type Any. The in operator with ranges requires a comparable type like Int. Using in 1..5 on Any causes a type mismatch error.
Step 2: Check order of branches and syntax
The is String check is valid but unreachable if the first branch errors. Also, when in Kotlin does not require break.
Final Answer:
Cannot use in with Any type without casting -> Option A
Quick Check:
Range checks need compatible types [OK]
Hint: Check variable type before using in with ranges [OK]
Common Mistakes:
Ignoring type mismatch for in operator
Thinking when needs break
Assuming order of branches causes error
5. You want to write a Kotlin when expression that returns: - "Small Int" if the value is an Int between 1 and 10 inclusive, - "Large Int" if the value is an Int greater than 10, - "String" if the value is a String, - "Other" for anything else.
Which of the following when expressions correctly implements this?
hard
A. when (v) {
in 1..10 -> "Small Int"
is Int -> "Large Int"
is String -> "String"
else -> "Other"
}
B. when (v) {
is Int -> if (v in 1..10) "Small Int" else "Large Int"
is String -> "String"
else -> "Other"
}
C. when (v) {
is Int in 1..10 -> "Small Int"
is Int -> "Large Int"
is String -> "String"
else -> "Other"
}
D. when (v) {
is Int && v in 1..10 -> "Small Int"
is Int -> "Large Int"
is String -> "String"
else -> "Other"
}
Solution
Step 1: Understand the conditions needed
We need to check if v is an Int and then check its value range for "Small Int" or "Large Int". Also check if v is a String.
Step 2: Analyze each option's correctness
when (v) {
in 1..10 -> "Small Int"
is Int -> "Large Int"
is String -> "String"
else -> "Other"
} incorrectly uses in 1..10 without checking type first, causing errors if v is not Int. when (v) {
is Int in 1..10 -> "Small Int"
is Int -> "Large Int"
is String -> "String"
else -> "Other"
} uses invalid syntax is Int in 1..10. when (v) {
is Int && v in 1..10 -> "Small Int"
is Int -> "Large Int"
is String -> "String"
else -> "Other"
} uses invalid syntax is Int && v in 1..10 inside when. when (v) {
is Int -> if (v in 1..10) "Small Int" else "Large Int"
is String -> "String"
else -> "Other"
} correctly checks type first, then uses an if inside the branch to distinguish ranges.
Final Answer:
Option B correctly implements the logic. -> Option B
Quick Check:
Use nested if inside when for complex conditions [OK]
Hint: Check type first, then range inside branch with if [OK]
Common Mistakes:
Using invalid syntax combining is and in
Not checking type before range
Trying to use logical operators directly in when cases
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.