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 without argument with conditions
What is the output of this Kotlin code using
when without an argument?Kotlin
fun main() { val x = 15 val result = when { x % 3 == 0 -> "Divisible by 3" x % 5 == 0 -> "Divisible by 5" else -> "Not divisible by 3 or 5" } println(result) }
Attempts:
2 left
💡 Hint
Check which condition matches first in the when block.
✗ Incorrect
The when block checks conditions in order. Since 15 is divisible by 3, the first condition is true, so it returns "Divisible by 3".
❓ Predict Output
intermediate2:00remaining
When without argument with Boolean expressions
What will this Kotlin program print?
Kotlin
fun main() { val a = 10 val b = 20 val output = when { a > b -> "a is greater" a == b -> "a equals b" else -> "a is smaller" } println(output) }
Attempts:
2 left
💡 Hint
Compare the values of a and b carefully.
✗ Incorrect
Since 10 is less than 20, none of the first two conditions are true, so the else branch runs, printing "a is smaller".
🔧 Debug
advanced2:00remaining
Identify the error in when without argument
What error does this Kotlin code produce?
Kotlin
fun main() { val num = 5 val result = when { num > 0 -> "Positive" num < 0 -> "Negative" } println(result) }
Attempts:
2 left
💡 Hint
Check if all possible cases are covered in the when expression.
✗ Incorrect
The when expression without an else branch is not exhaustive. Since num could be zero, the compiler requires an else branch or all cases covered.
🧠 Conceptual
advanced1:30remaining
Behavior of when without argument with multiple true conditions
In Kotlin, when using
when without an argument, what happens if multiple conditions are true?Attempts:
2 left
💡 Hint
Think about how when expressions work as a control flow.
✗ Incorrect
The when expression checks conditions in order and executes only the first branch whose condition is true.
🚀 Application
expert2:30remaining
Using when without argument to classify numbers
What is the output of this Kotlin program?
Kotlin
fun main() { val n = -7 val classification = when { n > 0 && n % 2 == 0 -> "Positive even" n > 0 && n % 2 != 0 -> "Positive odd" n < 0 && n % 2 == 0 -> "Negative even" n < 0 && n % 2 != 0 -> "Negative odd" else -> "Zero" } println(classification) }
Attempts:
2 left
💡 Hint
Check the sign and parity of n carefully.
✗ Incorrect
n is -7, which is negative and odd, so the matching branch is "Negative odd".