0
0
Kotlinprogramming~10 mins

Enum entries iteration in Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print all enum entries using a for loop.

Kotlin
enum class Color { RED, GREEN, BLUE }

fun main() {
    for (color in Color.[1]) {
        println(color)
    }
}
Drag options to blanks, or click blank then click option'
Avalues()
Bentries()
Call()
Dlist()
Attempts:
3 left
💡 Hint
Common Mistakes
Using entries() which does not exist for enums in Kotlin.
Trying to call all() or list() which are not valid enum functions.
2fill in blank
medium

Complete the code to get the number of entries in the enum.

Kotlin
enum class Direction { NORTH, SOUTH, EAST, WEST }

fun main() {
    val count = Direction.[1].size
    println("Number of directions: $count")
}
Drag options to blanks, or click blank then click option'
Alist()
Ball()
Centries()
Dvalues()
Attempts:
3 left
💡 Hint
Common Mistakes
Using entries() which is not a Kotlin enum function.
Trying to get size directly from the enum class.
3fill in blank
hard

Fix the error in the code to iterate over enum entries correctly.

Kotlin
enum class Status { ON, OFF }

fun main() {
    for (s in Status.[1]) {
        println(s)
    }
}
Drag options to blanks, or click blank then click option'
Aall()
Bvalues()
CgetValues()
Dentries
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting parentheses after values.
Using entries or all which are not valid.
4fill in blank
hard

Fill both blanks to create a map of enum names to their ordinal values.

Kotlin
enum class Level { LOW, MEDIUM, HIGH }

fun main() {
    val map = Level.[1].associateBy([2]) { it.ordinal }
    println(map)
}
Drag options to blanks, or click blank then click option'
Avalues()
Bname
Cit.name
Dentries()
Attempts:
3 left
💡 Hint
Common Mistakes
Using entries() which is not valid.
Using name instead of it.name inside the lambda.
5fill in blank
hard

Fill all three blanks to filter enum entries with ordinal greater than 0 and print their names.

Kotlin
enum class Mode { BASIC, ADVANCED, EXPERT }

fun main() {
    val filtered = Mode.[1].filter { it.ordinal [2] 0 }
    for (m in filtered) {
        println(m[3])
    }
}
Drag options to blanks, or click blank then click option'
Avalues()
B>
C.name
Dentries()
Attempts:
3 left
💡 Hint
Common Mistakes
Using entries() which is invalid.
Using < instead of > in the filter.
Printing m instead of m.name.