Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'failure' instead of 'success' creates an error Result.
Using 'runCatching' requires a lambda, not a direct value.
✗ Incorrect
Use Result.success(value) to create a successful Result wrapping the value.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'success' instead of 'failure' creates a success Result.
Using 'runCatching' requires a lambda, not an exception directly.
✗ Incorrect
Use Result.failure(exception) to create a failed Result wrapping the exception.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'success' or 'failure' directly does not execute the lambda.
Using 'of' is not a standard Result function.
✗ Incorrect
runCatching runs the lambda and catches exceptions, returning a Result.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'fold' instead of 'map' changes the Result type.
Using '+' instead of '*' changes the operation.
✗ Incorrect
Use map to transform the success value, and * to multiply by 2.
5fill in blank
hardFill 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'
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.
✗ Incorrect
fold handles both success and failure. The failure lambda parameter is named e to access the exception.