0
0
Android Kotlinmobile~20 mins

Sealed classes in Android 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!
🧠 Conceptual
intermediate
1:30remaining
What is the main purpose of sealed classes in Kotlin?
Choose the best explanation for why sealed classes are used in Kotlin.
ATo enable multiple inheritance of classes.
BTo restrict class inheritance to a fixed set of subclasses known at compile time.
CTo allow any class to inherit from them without restrictions.
DTo create classes that cannot have any subclasses.
Attempts:
2 left
💡 Hint
Think about how sealed classes control which classes can extend them.
ui_behavior
intermediate
1: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")))
AData: Hello
BRuntime exception
CCompilation error due to missing else branch
DError: Hello
Attempts:
2 left
💡 Hint
Check which subclass instance is passed and how when handles sealed classes.
lifecycle
advanced
2: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?
ACompilation error due to non-exhaustive when expression.
BThe when expression runs but skips subclass C silently.
CRuntime exception when subclass C is passed.
DThe code compiles and runs without issues.
Attempts:
2 left
💡 Hint
Think about Kotlin's compiler checks for sealed classes and when expressions.
navigation
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?
ABy automatically generating navigation graphs without code.
BBy allowing dynamic creation of navigation states at runtime.
CBy defining a fixed set of navigation states, enabling safe and clear state handling in UI.
DBy replacing fragments with sealed classes.
Attempts:
2 left
💡 Hint
Think about how sealed classes limit possible states and help with exhaustive checks.
📝 Syntax
expert
2: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()
ASyntax error: sealed class cannot have subclasses.
BNo error, code compiles successfully.
CRuntime error due to missing constructor in Square.
DSyntax error: Square class missing parentheses after Shape superclass.
Attempts:
2 left
💡 Hint
Check the syntax for subclass declarations in Kotlin.