Kotlin Quick Quiz: Data Classes, Sealed Classes, and Enums Quiz

Challenge your Kotlin programming skills with these essential questions on data classes, sealed classes, and enums, designed to strengthen your understanding and boost your confidence in handling these key features for safe and expressive code modeling.

  1. Identifying Data Class Syntax

    Which of the following is the correct way to declare a data class for a Person with 'name' and 'age' properties in Kotlin?

    1. class Person(data val name: String, val age: Int)
    2. class data Person(val name: String, val age: Int)
    3. data class Person(val name: String, val age: Int)
    4. data Person(val name: String, val age: Int)

    Explanation: The correct way to define a data class in Kotlin is by prefixing the 'class' keyword with 'data', as in option A. The other options display incorrect syntax: B places 'data' in the constructor instead of before the class, C omits the 'class' keyword, and D places 'data' after 'class', which is not valid in Kotlin.

  2. Main Use of Sealed Classes

    What is the primary purpose of sealed classes in Kotlin when modeling complex state or hierarchies?

    1. Enabling objects to be serialized automatically.
    2. Ensuring exhaustive 'when' expressions with restricted subclassing.
    3. Providing faster performance than abstract classes.
    4. Allowing multiple inheritance of implementations.

    Explanation: Sealed classes are mainly used to restrict subclassing, ensuring all subclasses are known at compile time, which enables 'when' expressions to be checked for exhaustiveness. Serialization is not automatic just because a class is sealed, performance is not inherently faster than with abstract classes, and Kotlin still does not allow multiple inheritance of implementations.

  3. Automatic Functions in Data Classes

    When you create a data class in Kotlin, which of the following methods is automatically generated?

    1. open()
    2. add()
    3. equals()
    4. compareTo()

    Explanation: Kotlin data classes automatically generate several utility methods, including equals(), for comparing contents. The open() modifier is not a function, compareTo() is not provided unless the class implements Comparable, and add() has no relation to data classes by default.

  4. Enum Class Usage

    Suppose you want to model days of the week as a fixed set of possibilities in Kotlin. Which feature should you use?

    1. Enum class
    2. Companion object
    3. Data class
    4. Abstract class

    Explanation: An enum class provides a set of predefined constants and is ideal for modeling a fixed set of related values like days of the week. Data classes are used for data containers, abstract classes define base classes, and companion objects aren't related to enumerations.

  5. Inheritance in Sealed Classes

    Given a sealed class Shape and subclasses Circle and Square, which statement about subclassing is correct?

    1. All subclasses of Shape must be declared in the same file.
    2. Subclasses of Shape can only be public.
    3. Subclasses of Shape must override equals().
    4. Shape can only have one subclass.

    Explanation: Sealed classes in Kotlin require all their direct subclasses to be defined in the same file. They do not need to be public, and there can be more than one subclass. Overriding equals() is optional unless custom comparison logic is needed.

  6. Copying Data Classes

    Which feature allows you to create a new instance of a data class with some properties changed while copying others?

    1. transform() function
    2. clone() function
    3. update() function
    4. copy() function

    Explanation: Data classes in Kotlin provide a built-in copy() function that allows you to create a new object with selected properties modified. Kotlin does not provide a clone(), transform(), or update() function specifically for this purpose in data classes.

  7. Enum Classes and Properties

    How can you define an enum class Color in Kotlin with each value having an associated integer code?

    1. data enum Color(val code: Int) { RED = 1, GREEN = 2, BLUE = 3 }
    2. enum Color { RED(code=1), GREEN(code=2), BLUE(code=3) }
    3. enum class Color(val code: Int) { RED(1), GREEN(2), BLUE(3) }
    4. class enum Color(val code: Int) = { RED, GREEN, BLUE }

    Explanation: The correct approach is to declare the enum with a constructor parameter, like in option A. Option B uses incorrect keywords and assignment, C uses invalid syntax, and D’s format is not accepted in Kotlin.

  8. Using Sealed Classes in 'when'

    Why is using a sealed class in a 'when' expression safer in Kotlin than using a regular open class?

    1. Sealed classes require abstract methods to be implemented.
    2. Only sealed classes can be used with 'when' in Kotlin.
    3. The compiler checks for all possible subclasses in a 'when' expression.
    4. Sealed classes execute faster than open classes in 'when' statements.

    Explanation: With sealed classes, the compiler knows all possible subclasses at compile time and alerts you if you miss a branch in a 'when' expression. Speed of execution is not guaranteed to be faster, abstract methods are not mandatory in sealed classes, and 'when' expressions also work with open classes, just without exhaustiveness checking.

  9. Data Class Requirements

    Which requirement must be met for a class to be declared as a data class in Kotlin?

    1. All properties must be mutable.
    2. The class must inherit from Enum.
    3. The class must be abstract.
    4. The primary constructor must have at least one parameter.

    Explanation: A data class must have at least one parameter in its primary constructor to generate component functions and other methods. It cannot be abstract or inherit from Enum, and properties can be val (immutable); mutability is not required.

  10. Extending Enums

    Can you directly inherit from an existing enum class in Kotlin to add more enum values?

    1. No, enum classes cannot be subclassed or extended.
    2. No, but you can create a sealed class for that.
    3. Yes, using the 'extends' keyword.
    4. Yes, by using the 'open' modifier.

    Explanation: Enum classes in Kotlin are implicitly final and cannot be subclassed or have new constants added through inheritance. The 'open' modifier, as in B, isn't allowed on enums. 'Extends' is a Java keyword and inapplicable here. Sealed classes (option D) are a separate mechanism for modeling constrained hierarchies but do not extend enums.