Complete the code to launch a coroutine in the Main dispatcher.
CoroutineScope(Dispatchers.[1]).launch {
// UI update code here
}The Main dispatcher runs coroutines on the main thread, suitable for UI updates.
Complete the code to switch context to a background thread for heavy work.
withContext(Dispatchers.[1]) {
// heavy computation
}Default dispatcher is optimized for CPU-intensive work on background threads.
Fix the error in the coroutine scope declaration to use the IO dispatcher.
val scope = CoroutineScope(Dispatchers.[1])IO dispatcher is used for blocking IO tasks like file or network operations.
Fill both blanks to create a CoroutineScope with a Job and the Default dispatcher.
val job = Job() val scope = CoroutineScope(job + Dispatchers.[1]) scope.launch { // work } job.[2]()
start() instead of cancel() on Job.The scope uses Default dispatcher and the job is cancelled with cancel() to stop coroutines.
Fill all three blanks to launch a coroutine on the IO dispatcher and switch to Main for UI update.
CoroutineScope(Dispatchers.[1]).launch { val data = fetchData() withContext(Dispatchers.[2]) { updateUI(data) } } fun fetchData() = "Data" fun updateUI(data: String) { println("UI updated with $[3]") }
The coroutine starts on IO for background work, switches to Main for UI update, and prints the data string.