0
0
Kotlinprogramming~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sealed Class Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of exhaustive when with sealed class
What is the output of this Kotlin code when shape is Shape.Circle(5.0)?
Kotlin
sealed class Shape {
    data class Circle(val radius: Double) : Shape()
    data class Rectangle(val width: Double, val height: Double) : Shape()
}

fun describe(shape: Shape): String {
    return when(shape) {
        is Shape.Circle -> "Circle with radius ${shape.radius}"
        is Shape.Rectangle -> "Rectangle with width ${shape.width} and height ${shape.height}"
    }
}

fun main() {
    val shape: Shape = Shape.Circle(5.0)
    println(describe(shape))
}
ACompilation error: when expression is not exhaustive
BRectangle with width 5.0 and height 5.0
CRuntime exception: Unhandled type
DCircle with radius 5.0
Attempts:
2 left
💡 Hint
Check how the when expression covers all subclasses of the sealed class.
Predict Output
intermediate
2:00remaining
Exhaustive when with sealed interface and else branch
What will this Kotlin code print when status is Status.Loading?
Kotlin
sealed interface Status {
    object Loading : Status
    object Success : Status
    object Error : Status
}

fun statusMessage(status: Status): String {
    return when(status) {
        is Status.Success -> "Success!"
        is Status.Error -> "Error occurred."
        else -> "Loading..."
    }
}

fun main() {
    val status: Status = Status.Loading
    println(statusMessage(status))
}
ALoading...
BSuccess!
CError occurred.
DCompilation error: else branch not allowed with sealed interface
Attempts:
2 left
💡 Hint
Consider how the else branch works with sealed interfaces.
🔧 Debug
advanced
2:00remaining
Why does this when expression cause a compilation error?
Given this sealed class and function, why does the compiler report an error about the when expression not being exhaustive?
Kotlin
sealed class Result {
    data class Success(val data: String) : Result()
    object Failure : Result()
}

fun handle(result: Result): String {
    return when(result) {
        is Result.Success -> "Data: ${result.data}"
    }
}

fun main() {
    println(handle(Result.Failure))
}
ABecause the when expression does not handle the Failure case, so it is not exhaustive.
BBecause sealed classes cannot be used in when expressions without else branch.
CBecause the Failure object is not a subclass of Result.
DBecause the Success data class is missing a default constructor.
Attempts:
2 left
💡 Hint
Check if all subclasses of the sealed class are covered in the when expression.
📝 Syntax
advanced
2:00remaining
Which when expression is exhaustive for this sealed class?
Given the sealed class Command below, which when expression is exhaustive and compiles without error?
Kotlin
sealed class Command {
    object Start : Command()
    object Stop : Command()
    data class Move(val x: Int, val y: Int) : Command()
}
A
when(cmd) {
    is Command.Start -> "Start"
    is Command.Stop -> "Stop"
}
B
when(cmd) {
    is Command.Start -> "Start"
    else -> "Other"
}
C
when(cmd) {
    is Command.Start -> "Start"
    is Command.Stop -> "Stop"
    is Command.Move -> "Move"
}
D
when(cmd) {
    is Command.Move -> "Move"
    else -> "Other"
}
Attempts:
2 left
💡 Hint
An exhaustive when must cover all subclasses or have an else branch.
🚀 Application
expert
2:00remaining
Count how many subclasses are handled in this when expression
Given the sealed class Event and the function below, how many subclasses of Event are handled explicitly in the when expression?
Kotlin
sealed class Event {
    object Click : Event()
    object Hover : Event()
    object Scroll : Event()
    data class KeyPress(val key: Char) : Event()
}

fun handleEvent(event: Event): String {
    return when(event) {
        is Event.Click -> "Clicked"
        is Event.Hover -> "Hovered"
        else -> "Other event"
    }
}
A3
B2
C4
D1
Attempts:
2 left
💡 Hint
Count only the explicit branches, not the else branch.