Challenge - 5 Problems
When Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of when with Int range check
What is the output of this Kotlin code snippet?
Kotlin
fun main() { val x = 15 val result = when (x) { in 1..10 -> "Low" in 11..20 -> "Medium" else -> "High" } println(result) }
Attempts:
2 left
💡 Hint
Check which range the value 15 belongs to.
✗ Incorrect
The value 15 falls within the range 11..20, so the when expression returns "Medium".
❓ Predict Output
intermediate2:00remaining
When with type checking
What will be printed by this Kotlin code?
Kotlin
fun main() { val obj: Any = "Hello" val output = when (obj) { is Int -> "Integer" is String -> "String" else -> "Unknown" } println(output) }
Attempts:
2 left
💡 Hint
Check the type of obj at runtime.
✗ Incorrect
The variable obj holds a String, so the when expression matches is String and returns "String".
❓ Predict Output
advanced2:00remaining
When with mixed ranges and types
What is the output of this Kotlin program?
Kotlin
fun main() { val input: Any = 25 val result = when (input) { in 1..10 -> "Small number" is Int -> "Integer number" in 20..30 -> "Medium number" else -> "Other" } println(result) }
Attempts:
2 left
💡 Hint
Remember that when checks branches in order and stops at the first match.
✗ Incorrect
The input 25 is not in 1..10, but is an Int, so the second branch matches and returns "Integer number". The third branch is never reached.
❓ Predict Output
advanced2:00remaining
When with smart casting and type mismatch
What will this Kotlin code print?
Kotlin
fun main() { val data: Any = 5.5 val output = when (data) { is Int -> "Int: ${data + 1}" is Double -> "Double: ${data + 1}" else -> "Unknown type" } println(output) }
Attempts:
2 left
💡 Hint
Check the actual type of data and how smart casting works.
✗ Incorrect
data is a Double 5.5, so the when branch is Double matches and adds 1 to print "Double: 6.5".
❓ Predict Output
expert3:00remaining
When with complex type and range conditions
What is the output of this Kotlin code?
Kotlin
fun main() { val value: Any = 42 val result = when (value) { is Int -> when (value) { in 1..10 -> "Small Int" in 11..50 -> "Medium Int" else -> "Large Int" } is String -> "String value" else -> "Unknown" } println(result) }
Attempts:
2 left
💡 Hint
Look carefully at the nested when and the ranges for the Int value.
✗ Incorrect
value is an Int 42, which falls in 11..50, so the nested when returns "Medium Int".