0
0
Kotlinprogramming~10 mins

CancellationException behavior 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 catch a CancellationException.

Kotlin
try {
    // some suspend function call
} catch (e: [1]) {
    println("Cancelled")
}
Drag options to blanks, or click blank then click option'
ACancellationException
BTimeoutException
CIllegalArgumentException
DIOException
Attempts:
3 left
💡 Hint
Common Mistakes
Catching a generic Exception instead of CancellationException
Using unrelated exception types like IOException
2fill in blank
medium

Complete the code to rethrow a CancellationException inside a catch block.

Kotlin
try {
    // some suspend function
} catch (e: Exception) {
    if (e is [1]) throw e
    println("Other exception")
}
Drag options to blanks, or click blank then click option'
AIOException
BCancellationException
CNullPointerException
DIllegalStateException
Attempts:
3 left
💡 Hint
Common Mistakes
Not rethrowing CancellationException causes coroutine to continue unexpectedly
Rethrowing unrelated exceptions
3fill in blank
hard

Fix the error in the code to properly handle coroutine cancellation.

Kotlin
try {
    delay(1000)
} catch (e: [1]) {
    println("Cancelled")
}
Drag options to blanks, or click blank then click option'
AException
BRuntimeException
CCancellationException
DError
Attempts:
3 left
💡 Hint
Common Mistakes
Catching generic Exception hides cancellation
Catching Error or RuntimeException is incorrect
4fill in blank
hard

Fill both blanks to create a map of words to their lengths only if length is greater than 3.

Kotlin
val words = listOf("apple", "bat", "carrot", "dog")
val lengths = words.filter { word -> [2] > 3 }.associate { word -> word to [1] }
Drag options to blanks, or click blank then click option'
Aword.length
Blen(word)
Cword.length()
Dword.size
Attempts:
3 left
💡 Hint
Common Mistakes
Using Python's len() function
Using method call syntax like length() which is invalid in Kotlin
5fill in blank
hard

Fill all three blanks to create a map of uppercase words to their lengths if length is greater than 3.

Kotlin
val words = listOf("apple", "bat", "carrot", "dog")
val result = words.filter { word -> [3] > 3 }.associate { word -> [1] to [2] }
Drag options to blanks, or click blank then click option'
Aword.uppercase()
Bword.length
Dword.toUpperCase()
Attempts:
3 left
💡 Hint
Common Mistakes
Using deprecated toUpperCase() instead of uppercase()
Using Python style functions like len()