Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to safely execute the block using RunCatching.
Kotlin
val result = runCatching([1] { val number = "123".toInt() number * 2 })
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect lambda syntax like () -> Unit or {} which are not valid here.
✗ Incorrect
The runCatching function takes a lambda with no parameters, so we use () -> before the lambda block.
2fill in blank
mediumComplete the code to get the result value or null if an exception occurs.
Kotlin
val result = runCatching {
"abc".toInt()
}.[1]() Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using getOrThrow() which throws the exception instead of returning null.
✗ Incorrect
getOrNull() returns the value if successful or null if an exception was caught.
3fill in blank
hardFix the error in the code to handle failure with onFailure.
Kotlin
runCatching {
val x = 10 / 0
}.[1] {
println("Error: ${it.message}")
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using onSuccess which runs only if no exception occurred.
✗ Incorrect
onFailure is used to handle exceptions caught by runCatching.
4fill in blank
hardFill both blanks to create a map of word lengths only for words longer than 3 characters.
Kotlin
val words = listOf("cat", "house", "dog", "elephant") val lengths = words.associateWith { [1] } .filter { it.value [2] 3 }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using it.size which is invalid for String.
Using < instead of > in filter condition.
✗ Incorrect
Use it.length to get word length and filter with > 3 to keep longer words.
5fill in blank
hardFill all three blanks to safely get uppercase words longer than 4 characters.
Kotlin
val words = listOf("apple", "bat", "carrot", "dog") val safeWords = runCatching { words.map { it.[1]() } }.getOrElse { emptyList() } .filter { it.length [2] 4 } .associateWith { it.length [3] 0 }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase without parentheses which is not a function call.
Using < instead of > in filter.
Using + or - instead of * in multiplication.
✗ Incorrect
Use uppercase() to convert words, filter length > 4, and multiply length by 0 to get 0.