Challenge - 5 Problems
Enum When Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of exhaustive when with enum
What is the output of this Kotlin code using an exhaustive
when expression with an enum?Kotlin
enum class Direction { NORTH, SOUTH, EAST, WEST } fun describe(direction: Direction): String { return when(direction) { Direction.NORTH -> "Up" Direction.SOUTH -> "Down" Direction.EAST -> "Right" Direction.WEST -> "Left" } } fun main() { println(describe(Direction.EAST)) }
Attempts:
2 left
💡 Hint
The when expression covers all enum cases, so no else branch is needed.
✗ Incorrect
The when expression is exhaustive because it handles all enum values. For Direction.EAST, it returns "Right".
❓ Predict Output
intermediate2:00remaining
What error occurs with non-exhaustive when on enum?
What error does this Kotlin code produce when compiled?
Kotlin
enum class Color { RED, GREEN, BLUE } fun colorName(color: Color): String { return when(color) { Color.RED -> "Red" Color.GREEN -> "Green" } } fun main() { println(colorName(Color.BLUE)) }
Attempts:
2 left
💡 Hint
Kotlin requires when expressions on enums to cover all cases or have an else branch.
✗ Incorrect
The when expression is missing a branch for Color.BLUE and no else branch is provided, so Kotlin reports a compilation error.
🔧 Debug
advanced2:00remaining
Why does this exhaustive when cause unreachable code?
This Kotlin code causes a compilation error about unreachable code. Why?
Kotlin
enum class Status { STARTED, RUNNING, FINISHED } fun checkStatus(status: Status): String { return when(status) { Status.STARTED -> "Started" Status.RUNNING -> "Running" Status.FINISHED -> "Finished" else -> "Unknown" } }
Attempts:
2 left
💡 Hint
When all enum cases are covered, else branch is not needed and causes unreachable code.
✗ Incorrect
Since all enum values are handled explicitly, the else branch can never be reached, causing a compilation error.
🧠 Conceptual
advanced2:00remaining
Exhaustive when with sealed class vs enum
Which statement is true about using
when expressions with enums and sealed classes in Kotlin?Attempts:
2 left
💡 Hint
Both enums and sealed classes have a fixed set of subclasses or values known at compile time.
✗ Incorrect
Kotlin enforces exhaustive when expressions for both enums and sealed classes if all cases are covered, so else branches are not needed.
❓ Predict Output
expert2:00remaining
Output of nested when with enum and smart cast
What is the output of this Kotlin code that uses nested when expressions and smart casts with enums?
Kotlin
enum class Mode { ON, OFF } fun modeMessage(mode: Mode?): String { return when(mode) { null -> "No mode" Mode.ON -> when(mode) { Mode.ON -> "Mode is ON" Mode.OFF -> "Mode is OFF" } Mode.OFF -> "Mode is OFF" } } fun main() { println(modeMessage(Mode.ON)) }
Attempts:
2 left
💡 Hint
The inner when uses the same enum value as the outer when branch.
✗ Incorrect
The outer when matches Mode.ON, then the inner when also matches Mode.ON, returning "Mode is ON".