Challenge - 5 Problems
Sealed Class Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)) }
Attempts:
2 left
💡 Hint
Check how the when expression covers all subclasses of the sealed class.
✗ Incorrect
The sealed class Shape has two subclasses: Circle and Rectangle. The when expression covers both, so it is exhaustive. When shape is Circle(5.0), the first branch matches and returns "Circle with radius 5.0".
❓ Predict Output
intermediate2: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)) }
Attempts:
2 left
💡 Hint
Consider how the else branch works with sealed interfaces.
✗ Incorrect
The sealed interface Status has three objects. The when expression handles Success and Error explicitly, and uses else for Loading. So when status is Loading, it returns "Loading...".
🔧 Debug
advanced2: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)) }
Attempts:
2 left
💡 Hint
Check if all subclasses of the sealed class are covered in the when expression.
✗ Incorrect
The sealed class Result has two subclasses: Success and Failure. The when expression only handles Success, so it is not exhaustive. The compiler requires all subclasses to be handled or an else branch.
📝 Syntax
advanced2: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() }
Attempts:
2 left
💡 Hint
An exhaustive when must cover all subclasses or have an else branch.
✗ Incorrect
Option C covers all three subclasses explicitly, so it is exhaustive. Options A misses Move, so not exhaustive. Options C and D have else branches but miss some subclasses explicitly, so they compile but are not exhaustive in the sense of covering all subclasses explicitly.
🚀 Application
expert2: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" } }
Attempts:
2 left
💡 Hint
Count only the explicit branches, not the else branch.
✗ Incorrect
The when expression explicitly handles Event.Click and Event.Hover. The else branch covers Scroll and KeyPress implicitly. So 2 subclasses are handled explicitly.