Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The correct way to iterate over all enum entries in Kotlin is to use the values() function.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using entries() which is not a Kotlin enum function.
Trying to get size directly from the enum class.
✗ Incorrect
The values() function returns an array of all enum entries, so you can get the size from it.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting parentheses after values.
Using entries or all which are not valid.
✗ Incorrect
The correct function to get all enum entries is values(), with parentheses.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using entries() which is not valid.
Using name instead of it.name inside the lambda.
✗ Incorrect
Use values() to get all entries, and it.name to get the name of each enum entry.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using entries() which is invalid.
Using < instead of > in the filter.
Printing m instead of m.name.
✗ Incorrect
Use values() to get all entries, filter with > 0, and print the name property.