Bird
Raised Fist0
Kotlinprogramming~20 mins

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

Choose your learning style10 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().

Practice

(1/5)
1. What is the main purpose of using an enum with properties and methods in Kotlin?
easy
A. To group related fixed values with additional data and behavior
B. To create a list of unrelated variables
C. To define functions without any fixed values
D. To replace all classes in Kotlin

Solution

  1. Step 1: Understand enum basics

    Enums group fixed related values, like days or colors.
  2. Step 2: Recognize added properties and methods

    Adding properties and methods lets each enum value hold extra data and behavior.
  3. Final Answer:

    To group related fixed values with additional data and behavior -> Option A
  4. Quick Check:

    Enums = fixed values + extra data [OK]
Hint: Enums bundle fixed options with data and actions [OK]
Common Mistakes:
  • Thinking enums are for unrelated variables
  • Confusing enums with regular classes
  • Assuming enums can't have methods
2. Which of the following is the correct syntax to declare an enum with a property color of type String in Kotlin?
easy
A. enum class Fruit { APPLE(color = "Red"), BANANA(color = "Yellow") }
B. enum Fruit { APPLE: String = "Red", BANANA: String = "Yellow" }
C. enum class Fruit(val color: String) { APPLE("Red"), BANANA("Yellow") }
D. enum class Fruit { val color: String = "Red"; APPLE, BANANA }

Solution

  1. Step 1: Check enum class declaration with property

    In Kotlin, enum classes can have constructor parameters like val color: String.
  2. Step 2: Verify enum values initialization

    Each enum value must pass the property value in parentheses, e.g., APPLE("Red").
  3. Final Answer:

    enum class Fruit(val color: String) { APPLE("Red"), BANANA("Yellow") } -> Option C
  4. Quick Check:

    Enum with constructor and values = enum class Fruit(val color: String) { APPLE("Red"), BANANA("Yellow") } [OK]
Hint: Use constructor parameters and pass values in parentheses [OK]
Common Mistakes:
  • Using colon syntax instead of constructor parameters
  • Trying to assign properties inside enum body incorrectly
  • Omitting parentheses for enum values
3. What will be the output of the following Kotlin code?
enum class Size(val label: String) {
    SMALL("S"), MEDIUM("M"), LARGE("L");
    fun description() = "Size is $label"
}

fun main() {
    println(Size.MEDIUM.description())
}
medium
A. Size is M
B. Size is MEDIUM
C. MEDIUM
D. Size.MEDIUM

Solution

  1. Step 1: Understand enum property and method

    Each enum value has a label property: MEDIUM has "M".
  2. Step 2: Evaluate the description() method call

    The method returns "Size is $label", so for MEDIUM it returns "Size is M".
  3. Final Answer:

    Size is M -> Option A
  4. Quick Check:

    Method uses label property = "Size is M" [OK]
Hint: Method uses property value, not enum name [OK]
Common Mistakes:
  • Printing enum name instead of property
  • Confusing property label with enum constant name
  • Ignoring method call output
4. Identify the error in this Kotlin enum declaration:
enum class Direction(val degrees: Int) {
    NORTH(0),
    EAST(90),
    SOUTH(180),
    WEST
}
medium
A. Semicolon missing after enum constants
B. Cannot have properties in enum classes
C. Enum constants must be lowercase
D. Missing property value for WEST enum constant

Solution

  1. Step 1: Check enum constructor parameters

    The enum class requires an Int parameter degrees for each constant.
  2. Step 2: Verify each enum constant initialization

    All constants except WEST provide a value; WEST is missing (value).
  3. Final Answer:

    Missing property value for WEST enum constant -> Option D
  4. Quick Check:

    All enum constants must pass constructor values [OK]
Hint: All enum constants must provide constructor arguments [OK]
Common Mistakes:
  • Forgetting to pass constructor arguments for all constants
  • Assuming semicolon is mandatory after constants
  • Thinking enum constants must be lowercase
5. Given this enum with a method:
enum class TrafficLight(val duration: Int) {
    RED(30) {
        override fun signal() = "Stop"
    },
    YELLOW(5) {
        override fun signal() = "Caution"
    },
    GREEN(25) {
        override fun signal() = "Go"
    };
    abstract fun signal(): String
}

fun main() {
    val lights = TrafficLight.values()
    val signals = lights.map { it.signal() }
    println(signals)
}

What is the output when running main()?
hard
A. [30, 5, 25]
B. ["Stop", "Caution", "Go"]
C. [RED, YELLOW, GREEN]
D. ["Go", "Caution", "Stop"]

Solution

  1. Step 1: Understand enum with abstract method

    Each enum constant overrides signal() to return a specific string.
  2. Step 2: Map enum values to their signals

    The map calls signal() on each constant, producing ["Stop", "Caution", "Go"].
  3. Final Answer:

    ["Stop", "Caution", "Go"] -> Option B
  4. Quick Check:

    Abstract method overridden per constant = correct signals [OK]
Hint: Abstract method overridden per enum constant returns correct string [OK]
Common Mistakes:
  • Printing enum names instead of method results
  • Confusing property values with method outputs
  • Ignoring abstract method override requirement
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard