Challenge - 5 Problems
Enum Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of enum with property and method
What is the output of this Kotlin code?
Kotlin
enum class Direction(val degrees: Int) { NORTH(0) { override fun description() = "Upwards" }, EAST(90) { override fun description() = "Rightwards" }, SOUTH(180) { override fun description() = "Downwards" }, WEST(270) { override fun description() = "Leftwards" }; abstract fun description(): String } fun main() { val dir = Direction.EAST println("${dir.name} is at ${dir.degrees} degrees and means ${dir.description()}") }
Attempts:
2 left
💡 Hint
Check how each enum constant overrides the abstract method description().
✗ Incorrect
Each enum constant provides its own implementation of the abstract method description(). EAST returns "Rightwards" and has degrees 90.
❓ Predict Output
intermediate2:00remaining
Enum method call result
What does this Kotlin program print?
Kotlin
enum class Light(val duration: Int) { RED(30), GREEN(45), YELLOW(5); fun isSafeToGo() = this == GREEN } fun main() { val current = Light.YELLOW println(current.isSafeToGo()) }
Attempts:
2 left
💡 Hint
Check which enum constant returns true for isSafeToGo().
✗ Incorrect
Only GREEN returns true for isSafeToGo(). YELLOW returns false.
🔧 Debug
advanced2:00remaining
Identify the error in enum with method override
This Kotlin enum tries to override a method but causes a compilation error. What is the error?
Kotlin
enum class Status { SUCCESS { override fun message() = "Operation succeeded" }, FAILURE { override fun message() = "Operation failed" }; fun message(): String = "Unknown" }
Attempts:
2 left
💡 Hint
Check if the method message() is abstract or not.
✗ Incorrect
You cannot override a non-abstract method in enum constants. To override, message() must be abstract.
❓ Predict Output
advanced2:00remaining
Output of enum with companion object method
What is the output of this Kotlin code?
Kotlin
enum class Planet(val mass: Double, val radius: Double) { EARTH(5.972e24, 6371.0), MARS(0.64171e24, 3389.5); companion object { fun heaviest(): Planet = values().maxByOrNull { it.mass } ?: EARTH } } fun main() { println(Planet.heaviest()) }
Attempts:
2 left
💡 Hint
Check which planet has the greater mass.
✗ Incorrect
EARTH has a larger mass than MARS, so heaviest() returns EARTH.
🧠 Conceptual
expert2:00remaining
Number of enum constants with overridden methods
Given this Kotlin enum, how many constants override the abstract method `sound()`?
Kotlin
enum class Animal { DOG { override fun sound() = "Bark" }, CAT { override fun sound() = "Meow" }, COW { override fun sound() = "Moo" }, FISH; abstract fun sound(): String }
Attempts:
2 left
💡 Hint
Check which constants provide a method body for sound().
✗ Incorrect
DOG, CAT, and COW override sound(). FISH does not and will cause a compilation error if used.