Recall & Review
beginner
What is an enum class in Kotlin?An enum class in Kotlin is a special class that represents a group of constants. Each constant is an object of the enum type.Click to reveal answer
intermediate
How can you add properties to enum constants in Kotlin?
You add properties by defining a constructor in the enum class and passing values to each enum constant.Click to reveal answer
intermediate
Can enum classes in Kotlin have methods? How?
Yes, enum classes can have methods just like regular classes. You define them inside the enum class body.Click to reveal answer
beginner
Example: What does this enum represent?
<pre>enum class Direction(val degrees: Int) {
NORTH(0), EAST(90), SOUTH(180), WEST(270)
}</pre>This enum represents directions with their corresponding degrees on a compass.
Click to reveal answer
advanced
How do you override a method for each enum constant individually?
You can override methods by defining them in the enum class and then providing a specific implementation for each constant using an anonymous class syntax.Click to reveal answer
How do you define a property for each enum constant in Kotlin?
✗ Incorrect
Properties for enum constants are defined by adding a constructor with parameters to the enum class and passing values to each constant.
Can enum classes in Kotlin have functions?
✗ Incorrect
Enum classes in Kotlin can have functions just like regular classes.
What keyword is used to define an enum class in Kotlin?
✗ Incorrect
The keyword 'enum class' is used to define an enum class in Kotlin.
How do you override a method for a specific enum constant?
✗ Incorrect
You override a method for a specific enum constant by providing an anonymous class implementation for that constant.
What will this code print?
enum class Color(val rgb: Int) {
RED(0xFF0000), GREEN(0x00FF00), BLUE(0x0000FF)
}
fun main() {
println(Color.GREEN.rgb)
}✗ Incorrect
The rgb property is an integer. 0x00FF00 in decimal is 65280, so it prints 65280.
Explain how to add properties and methods to an enum class in Kotlin with an example.
Think about how you pass values to enum constants and how you write functions inside the enum.
You got /4 concepts.
Describe how to override a method for individual enum constants in Kotlin.
Remember each constant can have its own implementation block.
You got /3 concepts.