0
0
Kotlinprogramming~10 mins

Why enums constrain values in Kotlin - Test Your Understanding

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

Complete the code to declare an enum class named Direction.

Kotlin
enum class [1] { NORTH, SOUTH, EAST, WEST }
Drag options to blanks, or click blank then click option'
ADirection
Bdirection
CDirections
DDir
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase names for enum classes.
Using plural form which is not declared in the code.
2fill in blank
medium

Complete the code to access the enum value EAST.

Kotlin
val dir = Direction.[1]
Drag options to blanks, or click blank then click option'
Aeast
BEAST
CEast
DeAST
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or mixed case for enum constants.
Trying to access a non-existent enum value.
3fill in blank
hard

Fix the error in the code to assign a valid enum value to variable dir.

Kotlin
val dir: Direction = Direction.[1]
Drag options to blanks, or click blank then click option'
ANORTH
BUP
CDOWN
DLEFT
Attempts:
3 left
💡 Hint
Common Mistakes
Using values not declared in the enum class.
Trying to assign strings or other types instead of enum constants.
4fill in blank
hard

Fill both blanks to create a function that returns true if the direction is WEST.

Kotlin
fun isWest(dir: Direction): Boolean = dir [1] Direction.[2]
Drag options to blanks, or click blank then click option'
A==
B!=
CWEST
DEAST
Attempts:
3 left
💡 Hint
Common Mistakes
Using inequality operator instead of equality.
Comparing to the wrong enum constant.
5fill in blank
hard

Fill all three blanks to create a when expression that returns a message for each direction.

Kotlin
fun directionMessage(dir: Direction): String = when(dir) {
    Direction.NORTH -> "Go [1]"
    Direction.SOUTH -> "Go [2]"
    Direction.EAST -> "Go [3]"
    else -> "Stay still"
}
Drag options to blanks, or click blank then click option'
Anorth
Bsouth
Ceast
Dwest
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase or mixed case in the message.
Mixing up directions in the messages.