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.
Which of the following is the correct way to declare a data class for a Person with 'name' and 'age' properties in Kotlin?
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.
What is the primary purpose of sealed classes in Kotlin when modeling complex state or hierarchies?
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.
When you create a data class in Kotlin, which of the following methods is automatically generated?
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.
Suppose you want to model days of the week as a fixed set of possibilities in Kotlin. Which feature should you use?
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.
Given a sealed class Shape and subclasses Circle and Square, which statement about subclassing is correct?
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.
Which feature allows you to create a new instance of a data class with some properties changed while copying others?
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.
How can you define an enum class Color in Kotlin with each value having an associated integer code?
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.
Why is using a sealed class in a 'when' expression safer in Kotlin than using a regular open class?
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.
Which requirement must be met for a class to be declared as a data class in Kotlin?
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.
Can you directly inherit from an existing enum class in Kotlin to add more enum values?
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.