Bird
Raised Fist0
Android Kotlinmobile~10 mins

Sealed classes in Android Kotlin - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a sealed class named Shape.

Android Kotlin
sealed class [1]
Drag options to blanks, or click blank then click option'
AShape
Bshape
CShapes
DshapeClass
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or plural forms for the class name.
2fill in blank
medium

Complete the code to declare a subclass Circle inside the sealed class Shape.

Android Kotlin
sealed class Shape {
    data class [1](val radius: Double) : Shape()
}
Drag options to blanks, or click blank then click option'
ASquare
BCircle
CTriangle
DRectangle
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different shape name than Circle.
Forgetting to inherit from Shape.
3fill in blank
hard

Fix the error in the when expression to handle all Shape subclasses without else.

Android Kotlin
fun area(shape: Shape): Double = when(shape) {
    is Shape.Circle -> 3.14 * shape.radius * shape.radius
    is Shape.Square -> shape.side * shape.side
    [1]
}
Drag options to blanks, or click blank then click option'
Aelse -> 0.0
Bis Shape.Shape -> 0.0
Cis Shape.Triangle -> 0.5 * shape.base * shape.height
Dis Shape.Rectangle -> shape.length * shape.width
Attempts:
3 left
💡 Hint
Common Mistakes
Using else instead of explicit subclass cases.
Adding a case for the sealed class itself.
4fill in blank
hard

Fill both blanks to declare two subclasses Rectangle and Triangle inside sealed class Shape.

Android Kotlin
sealed class Shape {
    data class [1](val length: Double, val width: Double) : Shape()
    data class [2](val base: Double, val height: Double) : Shape()
}
Drag options to blanks, or click blank then click option'
ARectangle
BCircle
CTriangle
DSquare
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up class names or using unrelated names.
5fill in blank
hard

Fill all three blanks to create a when expression that returns area for each Shape subclass.

Android Kotlin
fun area(shape: Shape): Double = when(shape) {
    is Shape.Circle -> 3.14 * shape.radius * shape.radius
    is Shape.Rectangle -> shape.length * shape.width
    is Shape.Triangle -> [1] * shape.base * shape.height
}
Drag options to blanks, or click blank then click option'
A3
B1
C2
D0.5
Attempts:
3 left
💡 Hint
Common Mistakes
Using 2 or 3 instead of 0.5.
Forgetting the multiplier.

Practice

(1/5)
1. What is the main purpose of a sealed class in Kotlin?
easy
A. To replace interfaces completely
B. To allow unlimited subclassing from any package
C. To create anonymous classes only
D. To restrict class inheritance to a fixed set of subclasses

Solution

  1. Step 1: Understand sealed class inheritance

    A sealed class restricts which classes can inherit from it, limiting subclasses to those defined in the same file.
  2. Step 2: Compare with other class types

    Unlike open classes, sealed classes do not allow unlimited subclassing, ensuring all subclasses are known at compile time.
  3. Final Answer:

    To restrict class inheritance to a fixed set of subclasses -> Option D
  4. Quick Check:

    Sealed class = fixed subclass set [OK]
Hint: Sealed classes limit subclasses to known types only [OK]
Common Mistakes:
  • Thinking sealed classes allow any subclass anywhere
  • Confusing sealed classes with abstract classes
  • Assuming sealed classes create anonymous classes
2. Which of the following is the correct syntax to declare a sealed class named Result in Kotlin?
easy
A. sealed Result class {}
B. sealed class Result {}
C. class sealed Result {}
D. sealed interface Result {}

Solution

  1. Step 1: Recall sealed class declaration syntax

    The keyword sealed precedes class followed by the class name and braces.
  2. Step 2: Check each option

    sealed class Result {} matches the correct syntax. Options B and C have wrong keyword order. sealed interface Result {} declares a sealed interface, not a sealed class.
  3. Final Answer:

    sealed class Result {} -> Option B
  4. Quick Check:

    sealed class syntax = "sealed class Name {}" [OK]
Hint: sealed class always starts with 'sealed class' keywords [OK]
Common Mistakes:
  • Swapping 'sealed' and 'class' keywords
  • Using 'sealed interface' instead of 'sealed class'
  • Omitting braces after class name
3. Given the sealed class and when expression below, what will be printed?
sealed class Response {
  object Success : Response()
  object Error : Response()
}

fun handle(response: Response) {
  when(response) {
    is Response.Success -> println("Success")
    is Response.Error -> println("Error")
  }
}

handle(Response.Success)
medium
A. Nothing is printed
B. Error
C. Success
D. Compile-time error

Solution

  1. Step 1: Identify the passed object type

    The function is called with Response.Success, which matches the first when branch.
  2. Step 2: Check the when branches

    The when expression prints "Success" for Response.Success and "Error" for Response.Error.
  3. Final Answer:

    Success -> Option C
  4. Quick Check:

    Response.Success triggers "Success" print [OK]
Hint: Match sealed subclasses exactly in when to get correct output [OK]
Common Mistakes:
  • Confusing Success and Error outputs
  • Forgetting to call the function with correct subclass
  • Expecting compile error due to sealed class
4. What is wrong with this sealed class declaration?
sealed class State {}
class Loading : State()
class Success : State()
class Error : State()
medium
A. Subclasses must be declared inside the sealed class file
B. Sealed classes cannot have subclasses
C. Subclasses must be objects, not classes
D. Sealed class must be abstract

Solution

  1. Step 1: Recall sealed class subclass rules

    All subclasses of a sealed class must be declared in the same file as the sealed class.
  2. Step 2: Analyze the code snippet

    The subclasses Loading, Success, Error are declared separately after the sealed class. This violates the rule because direct subclasses of a sealed class must be declared in the same file as the sealed class.
  3. Final Answer:

    Subclasses must be declared inside the sealed class file -> Option A
  4. Quick Check:

    Sealed subclasses must be in same file [OK]
Hint: Keep all sealed subclasses in the same file [OK]
Common Mistakes:
  • Declaring subclasses in different files
  • Thinking sealed classes cannot have subclasses
  • Confusing sealed classes with abstract classes
5. You want to represent a network request state with a sealed class NetworkState having Loading, Success with data, and Error with message. Which is the best way to define Success and Error subclasses?
hard
A. Use data classes with properties inside the sealed class
B. Use object declarations for all subclasses
C. Use abstract classes for Success and Error
D. Use interfaces for Success and Error

Solution

  1. Step 1: Understand subclass data needs

    Success needs to hold data, and Error needs to hold a message, so they require properties.
  2. Step 2: Choose subclass type

    Data classes are ideal for subclasses with properties because they provide equals, hashCode, and toString automatically.
  3. Final Answer:

    Use data classes with properties inside the sealed class -> Option A
  4. Quick Check:

    Data classes hold data in sealed subclasses [OK]
Hint: Use data classes for sealed subclasses with data [OK]
Common Mistakes:
  • Using object for subclasses needing data
  • Using abstract classes unnecessarily
  • Using interfaces instead of sealed subclasses
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard