0
0
Kotlinprogramming~20 mins

Enum with when exhaustive check in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Enum When Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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))
}
AEast
BRight
CDirection.EAST
DCompilation error due to missing else
Attempts:
2 left
💡 Hint
The when expression covers all enum cases, so no else branch is needed.
Predict Output
intermediate
2: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))
}
ACompilation error: 'when' expression must be exhaustive
BRuntime exception: No matching branch found
CPrints null
DPrints empty string
Attempts:
2 left
💡 Hint
Kotlin requires when expressions on enums to cover all cases or have an else branch.
🔧 Debug
advanced
2: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"
    }
}
AThe when expression is missing a case
BThe else branch is required for exhaustive when on enums
CThe enum has a hidden value causing unreachable code
DThe else branch is unreachable because all enum cases are already handled
Attempts:
2 left
💡 Hint
When all enum cases are covered, else branch is not needed and causes unreachable code.
🧠 Conceptual
advanced
2:00remaining
Exhaustive when with sealed class vs enum
Which statement is true about using when expressions with enums and sealed classes in Kotlin?
ASealed classes require else branches but enums do not
BEnums require else branches but sealed classes do not
CBoth enums and sealed classes require exhaustive when expressions without else branches if all cases are covered
DNeither enums nor sealed classes support exhaustive when expressions
Attempts:
2 left
💡 Hint
Both enums and sealed classes have a fixed set of subclasses or values known at compile time.
Predict Output
expert
2: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))
}
AMode is ON
BMode is OFF
CNo mode
DCompilation error due to non-exhaustive when
Attempts:
2 left
💡 Hint
The inner when uses the same enum value as the outer when branch.