Challenge - 5 Problems
Sealed Class Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate1:30remaining
What is the main purpose of sealed classes in Kotlin?
Choose the best explanation for why sealed classes are used in Kotlin.
Attempts:
2 left
💡 Hint
Think about how sealed classes control which classes can extend them.
✗ Incorrect
Sealed classes restrict inheritance to a known set of subclasses, enabling exhaustive when expressions and safer code.
❓ ui_behavior
intermediate1:30remaining
What will be the output of this Kotlin code using a sealed class?
Given the sealed class and when expression below, what will be printed?
Android Kotlin
sealed class Result class Success(val data: String) : Result() class Error(val error: String) : Result() fun handle(result: Result) = when(result) { is Success -> "Data: ${result.data}" is Error -> "Error: ${result.error}" } println(handle(Success("Hello")))
Attempts:
2 left
💡 Hint
Check which subclass instance is passed and how when handles sealed classes.
✗ Incorrect
The handle function matches the Success subclass and returns its data string. No else is needed because sealed classes cover all cases.
❓ lifecycle
advanced2:00remaining
What happens if you add a new subclass to a sealed class but forget to update the when expression?
Consider a sealed class with subclasses A and B. You add subclass C but do not update the when expression that handles A and B. What is the result?
Attempts:
2 left
💡 Hint
Think about Kotlin's compiler checks for sealed classes and when expressions.
✗ Incorrect
Kotlin requires when expressions on sealed classes to be exhaustive. Adding a new subclass without updating when causes a compile error.
advanced
2:00remaining
How can sealed classes improve navigation state management in Android apps?
Which option best describes how sealed classes help manage navigation states in Android apps?
Attempts:
2 left
💡 Hint
Think about how sealed classes limit possible states and help with exhaustive checks.
✗ Incorrect
Sealed classes let you define all possible navigation states explicitly, making UI code safer and easier to maintain.
📝 Syntax
expert2:00remaining
What error does this Kotlin code produce?
Examine the sealed class and subclass below. What error occurs when compiling?
Android Kotlin
sealed class Shape class Circle(val radius: Double) : Shape() class Square(val side: Double) : Shape()
Attempts:
2 left
💡 Hint
Check the syntax for subclass declarations in Kotlin.
✗ Incorrect
In Kotlin, when inheriting from a class, parentheses are required even if the superclass has no constructor parameters.