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 multiple conditions combined by commas
What is the output of this Kotlin code snippet?
Kotlin
fun main() { val x = 3 val result = when (x) { 1, 2 -> "One or Two" 3, 4 -> "Three or Four" else -> "Other" } println(result) }
Attempts:
2 left
💡 Hint
Remember that commas separate multiple matching values in a when branch.
✗ Incorrect
The variable x is 3, and the when expression checks if x matches any of the values in each branch. The branch 3, 4 -> "Three or Four" matches because x is 3, so it returns "Three or Four".
❓ Predict Output
intermediate2:00remaining
When with condition ranges and multiple conditions
What will this Kotlin program print?
Kotlin
fun main() { val score = 85 val grade = when (score) { in 90..100 -> "A" in 80..89, 75 -> "B" else -> "C" } println(grade) }
Attempts:
2 left
💡 Hint
Check if the score matches any of the conditions including the single value 75.
✗ Incorrect
The score is 85, which falls in the range 80..89, so the branch in 80..89, 75 -> "B" matches and returns "B".
🔧 Debug
advanced2:00remaining
Identify the error in when with multiple conditions
What error does this Kotlin code produce?
Kotlin
fun main() { val y = 5 val output = when (y) { 1, 2 -> "Low" 3, 4 -> "Medium" else -> "High" } println(output) }
Attempts:
2 left
💡 Hint
Look carefully at the syntax between 3 and 4 in the when expression.
✗ Incorrect
The branch 3 4 -> "Medium" is missing a comma between 3 and 4, which causes a syntax error in Kotlin.
❓ Predict Output
advanced2:00remaining
When with multiple conditions and else branch
What is the output of this Kotlin code?
Kotlin
fun main() { val input = "apple" val category = when (input) { "banana", "orange" -> "Citrus" "apple", "pear" -> "Pome" else -> "Unknown" } println(category) }
Attempts:
2 left
💡 Hint
Check which branch matches the input string.
✗ Incorrect
The input is "apple", which matches the branch "apple", "pear" -> "Pome", so the output is "Pome".
🧠 Conceptual
expert2:00remaining
How does Kotlin's when handle multiple conditions?
Which statement best describes how Kotlin's when expression handles multiple conditions separated by commas in a single branch?
Attempts:
2 left
💡 Hint
Think about how commas work in a when branch.
✗ Incorrect
In Kotlin, when you separate multiple conditions by commas in a single branch, it means the branch matches if the subject equals any one of those conditions.