Complete the code to switch to the IO dispatcher using withContext.
suspend fun fetchData() {
withContext([1]) {
// perform IO operation
}
}The Dispatchers.IO is used for IO-bound tasks like reading from disk or network calls.
Complete the code to switch to the Default dispatcher inside withContext.
suspend fun calculate() {
withContext([1]) {
// CPU-intensive calculation
}
}Dispatchers.Default is used for CPU-intensive tasks like calculations.
Fix the error in the code by choosing the correct dispatcher for UI updates.
suspend fun updateUI() {
withContext([1]) {
// update UI elements
}
}UI updates must happen on the Dispatchers.Main to avoid threading issues.
Fill both blanks to create a map of words to their lengths, filtering words longer than 4 characters.
val words = listOf("apple", "cat", "banana", "dog") val lengths = words.associateWith { [1] } .filter { it.value [2] 4 }
it.length gets the length of each word, and filtering with > 4 keeps words longer than 4 characters.
Fill all three blanks to create a map of uppercase words to their lengths, filtering lengths greater than 3.
val words = listOf("one", "two", "three", "four") val result = words.associateBy([1]) { [2] } .filter { it.value [3] 3 }
We use it.uppercase() as the key, it.length as the value, and filter values greater than 3.