0
0
Kotlinprogramming~10 mins

Result type for functional error handling 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 create a successful Result with value 42.

Kotlin
val result: Result<Int> = Result.[1](42)
Drag options to blanks, or click blank then click option'
Aof
BrunCatching
Csuccess
Dfailure
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'failure' instead of 'success' creates an error Result.
Using 'runCatching' requires a lambda, not a direct value.
2fill in blank
medium

Complete the code to create a failed Result with an exception.

Kotlin
val errorResult: Result<Int> = Result.[1](IllegalArgumentException("Invalid input"))
Drag options to blanks, or click blank then click option'
Aof
BrunCatching
Csuccess
Dfailure
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'success' instead of 'failure' creates a success Result.
Using 'runCatching' requires a lambda, not an exception directly.
3fill in blank
hard

Fix the error in the code to catch exceptions and return a Result.

Kotlin
val result = Result.[1] { 10 / 0 }
Drag options to blanks, or click blank then click option'
ArunCatching
Bsuccess
Cfailure
Dof
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'success' or 'failure' directly does not execute the lambda.
Using 'of' is not a standard Result function.
4fill in blank
hard

Fill both blanks to create a map operation on a Result.

Kotlin
val mapped = result.[1] { it [2] 2 }
Drag options to blanks, or click blank then click option'
Amap
B*
C+
Dfold
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'fold' instead of 'map' changes the Result type.
Using '+' instead of '*' changes the operation.
5fill in blank
hard

Fill all three blanks to handle success and failure with fold.

Kotlin
val message = result.[1](
  onSuccess = { "Success: " + it.toString() },
  onFailure = { [2] -> "Error: " + [3].message }
)
Drag options to blanks, or click blank then click option'
Afold
Be
Dmap
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'map' instead of 'fold' only handles success.
Using different names for the failure parameter and usage causes errors.