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 back to the Main dispatcher after an IO operation.
suspend fun updateUI() {
withContext(Dispatchers.IO) {
// fetch data
}
withContext([1]) {
// update UI
}
}UI updates must happen on the Main dispatcher to avoid crashes.
Fix the error in the code by choosing the correct dispatcher for CPU-intensive work.
suspend fun calculate() {
withContext([1]) {
// heavy computation
}
}Dispatchers.Default is optimized for CPU-intensive tasks like calculations.
Fill both blanks to correctly switch context for network call and UI update.
suspend fun loadData() {
withContext([1]) {
// network call
}
withContext([2]) {
// update UI
}
}Network calls run on Dispatchers.IO, UI updates on Dispatchers.Main.
Fill all three blanks to create a map of word lengths filtering words longer than 4 characters.
suspend fun processWords(words: List<String>): Map<String, Int> = withContext([1]) { words.associateWith { word -> word.length } }.filter { (word, length) -> length [2] [3] }
Use Dispatchers.Default for CPU work, filter lengths greater than 4.