0
0
Kotlinprogramming~5 mins

Enum with properties and methods in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
ABy declaring variables outside the enum class
BBy adding a constructor with parameters to the enum class
CBy using global variables
DBy creating a separate class
Can enum classes in Kotlin have functions?
ANo, enums cannot have functions
BYes, but only static functions
CYes, enum classes can have functions like regular classes
DOnly if the enum has no properties
What keyword is used to define an enum class in Kotlin?
Aenum class
Bclass
Cenum
Dobject
How do you override a method for a specific enum constant?
ABy defining the method outside the enum class
BBy using inheritance
CBy creating a new enum class
DBy 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)
}
A65280
BError
CGreen
D0x00FF00
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.