0
0
Kotlinprogramming~20 mins

Enum with properties and methods in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Enum Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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()}")
}
AEAST is at 90 degrees and means Rightwards
BEAST is at 90 degrees and means Upwards
CEAST is at 180 degrees and means Rightwards
DCompilation error due to abstract method
Attempts:
2 left
💡 Hint
Check how each enum constant overrides the abstract method description().
Predict Output
intermediate
2: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())
}
Atrue
BRuntime exception
CCompilation error: missing method body
Dfalse
Attempts:
2 left
💡 Hint
Check which enum constant returns true for isSafeToGo().
🔧 Debug
advanced
2: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"
}
ACannot override non-abstract method message() without marking it abstract
BMissing semicolon after enum constants
CAbstract method message() must be declared abstract in enum
DEnum constants cannot have method bodies
Attempts:
2 left
💡 Hint
Check if the method message() is abstract or not.
Predict Output
advanced
2: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())
}
ACompilation error: companion object syntax
BMARS
CEARTH
DRuntime exception: null pointer
Attempts:
2 left
💡 Hint
Check which planet has the greater mass.
🧠 Conceptual
expert
2: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
}
A0
B3
C1
D4
Attempts:
2 left
💡 Hint
Check which constants provide a method body for sound().