0
0
Kotlinprogramming~20 mins

Sealed classes for restricted hierarchies 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 sealed class when matching with 'when'
What is the output of this Kotlin code using a sealed class and a when expression?
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 = 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))
}
ACircle with radius 5.0
BRectangle with width 5.0 and height 5.0
CCompilation error: 'when' expression is not exhaustive
DRuntime error: Unhandled type
Attempts:
2 left
💡 Hint
Think about how sealed classes allow the compiler to know all subclasses and how 'when' handles them.
Predict Output
intermediate
2:00remaining
Exhaustiveness check with sealed classes
What happens if you add a new subclass to a sealed class but forget to update the when expression that matches on it?
Kotlin
sealed class Result {
    object Success : Result()
    object Failure : Result()
    object Loading : Result()
}

fun handle(result: Result): String = when(result) {
    Result.Success -> "Success"
    Result.Failure -> "Failure"
}

fun main() {
    println(handle(Result.Loading))
}
APrints 'Failure'
BCompilation error: 'when' expression is not exhaustive
CPrints 'Success'
DRuntime error: Unhandled type
Attempts:
2 left
💡 Hint
Sealed classes let the compiler check if all subclasses are handled in 'when'.
🔧 Debug
advanced
2:00remaining
Why does this sealed class code fail to compile?
Identify the reason why this Kotlin code with sealed classes does not compile.
Kotlin
sealed class Animal
class Dog : Animal()
class Cat : Animal()

fun sound(animal: Animal) = when(animal) {
    is Dog -> "Bark"
    is Cat -> "Meow"
}
ASealed class subclasses must be declared in the same file
BMissing 'else' branch in 'when' expression
CCannot use 'is' checks with sealed classes
DSealed class cannot have subclasses
Attempts:
2 left
💡 Hint
Think about Kotlin's rules for sealed class subclass declarations.
📝 Syntax
advanced
2:00remaining
Correct syntax for sealed interface and its implementations
Which option shows the correct Kotlin syntax for a sealed interface and two implementations?
A
sealed interface Vehicle
sealed interface Car : Vehicle
sealed interface Bike : Vehicle
B
sealed interface Vehicle {
    class Car : Vehicle
    class Bike : Vehicle
}
C
sealed interface Vehicle
sealed class Car : Vehicle()
sealed class Bike : Vehicle()
D
sealed interface Vehicle
class Car : Vehicle
class Bike : Vehicle
Attempts:
2 left
💡 Hint
Sealed interfaces can be implemented by classes declared in the same file.
🚀 Application
expert
2:00remaining
Number of subclasses in sealed class hierarchy
Given this sealed class hierarchy, how many direct subclasses does the sealed class Operation have?
Kotlin
sealed class Operation {
    data class Add(val x: Int, val y: Int) : Operation()
    data class Subtract(val x: Int, val y: Int) : Operation()
    sealed class Multiply : Operation() {
        data class Simple(val x: Int, val y: Int) : Multiply()
        data class Complex(val x: Int, val y: Int, val z: Int) : Multiply()
    }
}
A4
B5
C3
D2
Attempts:
2 left
💡 Hint
Count only the immediate subclasses declared directly under Operation.