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 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)) }
Attempts:
2 left
💡 Hint
Think about how sealed classes allow the compiler to know all subclasses and how 'when' handles them.
✗ Incorrect
The sealed class Shape has two subclasses: Circle and Rectangle. The 'when' expression covers both cases, so it is exhaustive. The instance is a Circle with radius 5.0, so the output is 'Circle with radius 5.0'.
❓ Predict Output
intermediate2: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)) }
Attempts:
2 left
💡 Hint
Sealed classes let the compiler check if all subclasses are handled in 'when'.
✗ Incorrect
Since Result.Loading is a new subclass and the 'when' expression does not handle it, the compiler will report an error that the 'when' is not exhaustive.
🔧 Debug
advanced2: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" }
Attempts:
2 left
💡 Hint
Think about Kotlin's rules for sealed class subclass declarations.
✗ Incorrect
In Kotlin, all subclasses of a sealed class must be declared in the same file as the sealed class. Here, Dog and Cat are declared outside the sealed class file, causing a compilation error.
📝 Syntax
advanced2:00remaining
Correct syntax for sealed interface and its implementations
Which option shows the correct Kotlin syntax for a sealed interface and two implementations?
Attempts:
2 left
💡 Hint
Sealed interfaces can be implemented by classes declared in the same file.
✗ Incorrect
Option D correctly declares a sealed interface Vehicle and two classes Car and Bike implementing it. The classes are not sealed themselves but implement the sealed interface. Option D is invalid syntax because classes cannot be declared inside an interface body like that. Options A and D misuse sealed class/interface declarations.
🚀 Application
expert2: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() } }
Attempts:
2 left
💡 Hint
Count only the immediate subclasses declared directly under Operation.
✗ Incorrect
Operation has three direct subclasses: Add, Subtract, and Multiply. Although Multiply has its own subclasses, they are not direct subclasses of Operation.