Complete the code to catch a CancellationException.
try { // some suspend function call } catch (e: [1]) { println("Cancelled") }
The CancellationException is the specific exception thrown when a coroutine is cancelled.
Complete the code to rethrow a CancellationException inside a catch block.
try { // some suspend function } catch (e: Exception) { if (e is [1]) throw e println("Other exception") }
When catching exceptions, CancellationException should be rethrown to properly cancel the coroutine.
Fix the error in the code to properly handle coroutine cancellation.
try { delay(1000) } catch (e: [1]) { println("Cancelled") }
The delay function throws CancellationException when the coroutine is cancelled, so it must be caught specifically.
Fill both blanks to create a map of words to their lengths only if length is greater than 3.
val words = listOf("apple", "bat", "carrot", "dog") val lengths = words.filter { word -> [2] > 3 }.associate { word -> word to [1] }
In Kotlin, word.length gives the length of the string. The condition checks if length is greater than 3.
Fill all three blanks to create a map of uppercase words to their lengths if length is greater than 3.
val words = listOf("apple", "bat", "carrot", "dog") val result = words.filter { word -> [3] > 3 }.associate { word -> [1] to [2] }
toUpperCase() instead of uppercase()word.uppercase() converts the word to uppercase in Kotlin. word.length gives the length. The condition checks if length is greater than 3.