Bird
Raised Fist0
Kotlinprogramming~20 mins

Enum entries iteration in Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 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 iterating enum entries with for loop
What is the output of this Kotlin code that iterates over enum entries using a for loop?
Kotlin
enum class Color { RED, GREEN, BLUE }

fun main() {
    for (color in Color.values()) {
        println(color)
    }
}
Ared\ngreen\nblue
BRED\nGREEN\nBLUE
CColor.RED\nColor.GREEN\nColor.BLUE
DRED GREEN BLUE
Attempts:
2 left
💡 Hint
Remember that printing enum entries calls their toString() method which returns the name by default.
❓ Predict Output
intermediate
2:00remaining
Output of iterating enum entries with forEach and lambda
What will this Kotlin code print when iterating enum entries with forEach and a lambda?
Kotlin
enum class Direction { NORTH, EAST, SOUTH, WEST }

fun main() {
    Direction.values().forEach { println(it.name.lowercase()) }
}
Anorth\neast\nsouth\nwest
BNORTH\nEAST\nSOUTH\nWEST
CDirection.NORTH\nDirection.EAST\nDirection.SOUTH\nDirection.WEST
Dnorth east south west
Attempts:
2 left
💡 Hint
The name property returns the enum constant name as declared. The lowercase() function converts it to lowercase.
🧠 Conceptual
advanced
2:00remaining
Understanding enum entries iteration with indices
Given this Kotlin code, what is the output when iterating enum entries with their indices?
Kotlin
enum class Level { LOW, MEDIUM, HIGH }

fun main() {
    Level.values().forEachIndexed { index, level ->
        println("$index: $level")
    }
}
ALevel.LOW: 0\nLevel.MEDIUM: 1\nLevel.HIGH: 2
BLOW: 0\nMEDIUM: 1\nHIGH: 2
C0 LOW\n1 MEDIUM\n2 HIGH
D0: LOW\n1: MEDIUM\n2: HIGH
Attempts:
2 left
💡 Hint
forEachIndexed provides the index and the element in order.
🔧 Debug
advanced
2:00remaining
Identify the error in enum iteration code
What error does this Kotlin code produce when trying to iterate enum entries?
Kotlin
enum class Status { STARTED, RUNNING, FINISHED }

fun main() {
    for (s in Status) {
        println(s)
    }
}
ANo error, prints STARTED RUNNING FINISHED
BError: Missing parentheses after Status
CError: 'Status' is not iterable
DError: Cannot use enum class in for loop
Attempts:
2 left
💡 Hint
You must call values() to get an array of enum entries to iterate.
🚀 Application
expert
2:00remaining
Count enum entries with a specific property
Given this enum with a property, what is the output of counting entries with isActive = true?
Kotlin
enum class TaskStatus(val isActive: Boolean) {
    TODO(true), IN_PROGRESS(true), DONE(false), CANCELLED(false)
}

fun main() {
    val activeCount = TaskStatus.values().count { it.isActive }
    println(activeCount)
}
A2
B4
C3
D1
Attempts:
2 left
💡 Hint
Check which enum entries have isActive set to true.

Practice

(1/5)
1. What does EnumName.values() return in Kotlin?
easy
A. An array of all enum entries in the order they are declared
B. A list of enum names as strings
C. The first enum entry only
D. A map of enum entries to their ordinal values

Solution

  1. Step 1: Understand the purpose of values()

    The values() function returns all enum entries as an array in the order they are declared.
  2. Step 2: Compare options with this behavior

    Only An array of all enum entries in the order they are declared correctly describes this behavior; others describe different data types or partial data.
  3. Final Answer:

    An array of all enum entries in the order they are declared -> Option A
  4. Quick Check:

    EnumName.values() returns all entries [OK]
Hint: Remember: values() gives all enum entries as an array [OK]
Common Mistakes:
  • Thinking values() returns strings instead of enum objects
  • Assuming values() returns only one entry
  • Confusing values() with valueOf()
2. Which of the following is the correct syntax to iterate over all entries of an enum Color in Kotlin?
easy
A. for (c in Color.values()) { println(c) }
B. for (c in Color) { println(c) }
C. for (c in Color.entries()) { println(c) }
D. for (c in Color.all()) { println(c) }

Solution

  1. Step 1: Recall the correct method to get enum entries

    In Kotlin, values() returns all enum entries as an array.
  2. Step 2: Check each option's syntax

    The correct syntax is for (c in Color.values()) { println(c) }, which uses Color.values() correctly in a for loop. The other options use invalid syntax: iterating directly over Color, or non-existent methods like entries() or all().
  3. Final Answer:

    for (c in Color.values()) { println(c) } -> Option A
  4. Quick Check:

    Use values() to loop enums [OK]
Hint: Use EnumName.values() to loop enums [OK]
Common Mistakes:
  • Trying to loop directly over enum class without values()
  • Using non-existent methods like entries() or all()
  • Missing parentheses after values()
3. Given the enum:
enum class Direction { NORTH, EAST, SOUTH, WEST }

What is the output of this code?
for (d in Direction.values()) {
    print(d.name.first())
}
medium
A. Direction.NORTHDirection.EASTDirection.SOUTHDirection.WEST
B. north east south west
C. NESW
D. N E S W

Solution

  1. Step 1: Understand the loop and print statement

    The loop goes through all enum entries: NORTH, EAST, SOUTH, WEST. For each, it prints the first character of the name.
  2. Step 2: Determine printed characters

    First letters are 'N', 'E', 'S', 'W'. They are printed without spaces or newlines.
  3. Final Answer:

    NESW -> Option C
  4. Quick Check:

    First letters combined = NESW [OK]
Hint: Check what exactly is printed inside the loop [OK]
Common Mistakes:
  • Assuming spaces or newlines are printed
  • Printing full names instead of first letters
  • Confusing enum name with enum value
4. What is wrong with this Kotlin code snippet?
enum class Status { STARTED, RUNNING, FINISHED }

for (s in Status) {
    println(s)
}
medium
A. The loop variable must be declared as val
B. Enums cannot be iterated in Kotlin
C. The enum class must be declared inside a function
D. Missing call to values() to get enum entries

Solution

  1. Step 1: Identify how to iterate enums in Kotlin

    Enums cannot be iterated directly; you must use Status.values() to get all entries.
  2. Step 2: Check the code for missing values()

    The code tries to loop over Status directly, which is invalid syntax.
  3. Final Answer:

    Missing call to values() to get enum entries -> Option D
  4. Quick Check:

    Use Enum.values() to iterate enums [OK]
Hint: Always use values() to loop enums [OK]
Common Mistakes:
  • Trying to loop enum class directly without values()
  • Thinking enums are iterable by default
  • Adding unnecessary val in loop declaration
5. You have this enum:
enum class Level(val difficulty: Int) {
    EASY(1), MEDIUM(2), HARD(3)
}

How do you create a map from each enum entry to its difficulty using iteration?
hard
A. val map = Level.values().map { it.difficulty }
B. val map = Level.values().associateWith { it.difficulty }
C. val map = Level.associate { it to it.difficulty }
D. val map = Level.values().associateBy { it.difficulty }

Solution

  1. Step 1: Understand the goal

    We want a map where keys are enum entries and values are their difficulty.
  2. Step 2: Use values() and associateWith

    Level.values() gives all entries; associateWith maps each entry to a value.
  3. Step 3: Check options

    val map = Level.values().associateWith { it.difficulty } correctly uses associateWith { it.difficulty }. val map = Level.associate { it to it.difficulty } is invalid because Level is a class, not a collection. val map = Level.values().map { it.difficulty } returns a list, not a map. val map = Level.values().associateBy { it.difficulty } maps difficulty to enum, reversing keys and values.
  4. Final Answer:

    val map = Level.values().associateWith { it.difficulty } -> Option B
  5. Quick Check:

    Use values() + associateWith for map [OK]
Hint: Use values().associateWith { } to map enums [OK]
Common Mistakes:
  • Trying to call associate on enum class directly
  • Using map() instead of associateWith() for key-value pairs
  • Confusing associateBy() which reverses keys and values
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