Challenge - 5 Problems
Enum Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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) }
Attempts:
2 left
💡 Hint
The 'name' property of an enum returns the exact name as declared.
✗ Incorrect
In Kotlin, each enum constant has a 'name' property that returns its name as a string exactly as declared, including case.
❓ Predict Output
intermediate2:00remaining
Enum ordinal value output
What will this Kotlin program print?
Kotlin
enum class Color { RED, GREEN, BLUE } fun main() { println(Color.GREEN.ordinal) }
Attempts:
2 left
💡 Hint
Ordinal starts counting from zero for the first enum constant.
✗ Incorrect
The 'ordinal' property returns the position of the enum constant starting at 0. RED=0, GREEN=1, BLUE=2.
🔧 Debug
advanced2: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) }
Attempts:
2 left
💡 Hint
All enum constants must provide constructor arguments if the enum class has a constructor.
✗ Incorrect
When an enum class has a constructor, all constants must call it with appropriate arguments. MEDIUM lacks an argument causing a compile error.
🧠 Conceptual
advanced2: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()) }
Attempts:
2 left
💡 Hint
Each enum constant can override abstract methods individually.
✗ Incorrect
The enum declares an abstract method 'feeling'. Each constant overrides it with its own implementation, so the calls print their respective strings.
❓ Predict Output
expert3: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)) }
Attempts:
2 left
💡 Hint
The method returns LOW if no matching code is found.
✗ Incorrect
The 'fromCode' method searches for a Level with matching code. 5 matches MEDIUM, 7 matches none so returns LOW by default.