Complete the code to define an enum class named Direction.
enum class [1] { NORTH, SOUTH, EAST, WEST }
The enum class must be named Direction with capital D as per Kotlin naming conventions.
Complete the when expression to check the direction variable.
fun describeDirection(direction: Direction): String {
return when ([1]) {
Direction.NORTH -> "Going north"
Direction.SOUTH -> "Going south"
Direction.EAST -> "Going east"
Direction.WEST -> "Going west"
}
}The parameter name is direction, so the when expression must check direction.
Fix the error by making the when expression exhaustive.
fun describeDirection(direction: Direction): String {
return when (direction) {
Direction.NORTH -> "North"
Direction.SOUTH -> "South"
Direction.EAST -> "East"
[1] -> "West"
// Missing WEST case causes error
}
}The enum has a WEST value, so the when must handle Direction.WEST to be exhaustive.
Fill both blanks to create an exhaustive when expression without else.
fun describeDirection(direction: Direction): String {
return when ([1]) {
Direction.NORTH -> "North"
Direction.SOUTH -> "South"
Direction.EAST -> "East"
[2] -> "West"
}
}The when expression must check the variable direction and include all enum values, including Direction.WEST, to be exhaustive without else.
Fill all three blanks to return a message using when with exhaustive check and smart casting.
sealed class Result { data class Success(val data: String): Result() object Failure: Result() } fun handleResult(result: Result): String { return when ([1]) { is Result.Success -> "Data: [2]" [3] -> "Failed" } }
The when expression checks the result variable. For Success, it accesses result.data. For failure, it matches the singleton object Result.Failure.