0
0
Kotlinprogramming~20 mins

Enum class declaration in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Enum Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of enum property access
What is the output of this Kotlin code?
Kotlin
enum class Direction {
    NORTH, SOUTH, EAST, WEST
}

fun main() {
    val dir = Direction.EAST
    println(dir.name)
}
AEAST
BDirection.EAST
CError: Cannot access enum property
Deast
Attempts:
2 left
💡 Hint
The 'name' property of an enum returns the exact name as declared.
Predict Output
intermediate
2:00remaining
Enum ordinal value output
What will this Kotlin program print?
Kotlin
enum class Color {
    RED, GREEN, BLUE
}

fun main() {
    println(Color.GREEN.ordinal)
}
AError: ordinal not accessible
B2
C0
D1
Attempts:
2 left
💡 Hint
Ordinal starts counting from zero for the first enum constant.
🔧 Debug
advanced
2:30remaining
Identify the error in enum constructor usage
Which option correctly fixes the error in this Kotlin enum declaration?
Kotlin
enum class Size(val cm: Int) {
    SMALL(30), MEDIUM, LARGE(50)
}
AAdd a constructor argument to MEDIUM: MEDIUM(40)
BRemove constructor arguments from all constants
CChange val cm to var cm
DAdd default value to constructor: val cm: Int = 0
Attempts:
2 left
💡 Hint
All enum constants must provide constructor arguments if the enum class has a constructor.
🧠 Conceptual
advanced
2:30remaining
Behavior of enum with overridden methods
What will this Kotlin code print when run?
Kotlin
enum class Mood {
    HAPPY {
        override fun feeling() = "Joy"
    },
    SAD {
        override fun feeling() = "Tears"
    };
    abstract fun feeling(): String
}

fun main() {
    println(Mood.HAPPY.feeling())
    println(Mood.SAD.feeling())
}
AHAPPY\nSAD
Bfeeling\nfeeling
CJoy\nTears
DError: abstract method not implemented
Attempts:
2 left
💡 Hint
Each enum constant can override abstract methods individually.
Predict Output
expert
3:00remaining
Output of enum with companion object and custom method
What is the output of this Kotlin program?
Kotlin
enum class Level(val code: Int) {
    LOW(1), MEDIUM(5), HIGH(10);

    companion object {
        fun fromCode(code: Int) = values().find { it.code == code } ?: LOW
    }
}

fun main() {
    println(Level.fromCode(5))
    println(Level.fromCode(7))
}
AMEDIUM\nnull
BMEDIUM\nLOW
C5\n1
DError: companion object method not accessible
Attempts:
2 left
💡 Hint
The method returns LOW if no matching code is found.