0
0
Kotlinprogramming~20 mins

Coroutine context and dispatchers in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Coroutine Context Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this coroutine dispatcher code?
Consider the following Kotlin coroutine code snippet. What will be printed to the console?
Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    launch(Dispatchers.Default) {
        println("Running on: ${Thread.currentThread().name}")
    }.join()
}
ARunning on: IO-1
BRunning on: DefaultDispatcher-worker-1
CRunning on: main
DRunning on: CommonPool-worker-1
Attempts:
2 left
💡 Hint
Think about which thread the Default dispatcher uses in Kotlin coroutines.
Predict Output
intermediate
2:00remaining
What is the value of 'result' after this coroutine context merge?
Given the following Kotlin code, what will be the value of 'result' printed?
Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    val context1 = Dispatchers.Default + CoroutineName("First")
    val context2 = Dispatchers.IO + CoroutineName("Second")
    val mergedContext = context1 + context2
    println("CoroutineName: ${mergedContext[CoroutineName]?.name}")
}
ACoroutineName: Default
BCoroutineName: First
CCoroutineName: null
DCoroutineName: Second
Attempts:
2 left
💡 Hint
When merging contexts, the last CoroutineName overrides the previous one.
🔧 Debug
advanced
2:00remaining
Identify the error in this coroutine dispatcher usage
What error will this Kotlin coroutine code produce when run?
Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    launch(Dispatchers.Main) {
        println("Hello from Main")
    }.join()
}
Ajava.lang.IllegalStateException: Module with the Main dispatcher is missing.
BNo error, prints 'Hello from Main'
Cjava.lang.NullPointerException
Djava.lang.IllegalArgumentException: Dispatcher not found
Attempts:
2 left
💡 Hint
Dispatchers.Main requires a special setup in JVM environments.
🧠 Conceptual
advanced
1:30remaining
Which dispatcher is best for CPU-intensive tasks?
In Kotlin coroutines, which dispatcher is designed to efficiently handle CPU-intensive work?
ADispatchers.Unconfined
BDispatchers.IO
CDispatchers.Default
DDispatchers.Main
Attempts:
2 left
💡 Hint
Think about which dispatcher uses a shared pool of threads optimized for CPU work.
Predict Output
expert
3:00remaining
What is the output of this nested coroutine context example?
Analyze the following Kotlin code and determine what will be printed to the console.
Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    val outerContext = Dispatchers.Default + CoroutineName("Outer")
    val job = launch(outerContext) {
        println("Outer context: ${coroutineContext[CoroutineName]?.name}")
        withContext(Dispatchers.IO + CoroutineName("Inner")) {
            println("Inner context: ${coroutineContext[CoroutineName]?.name}")
            println("Inner dispatcher: ${coroutineContext[CoroutineDispatcher]}")
        }
        println("Back to outer context: ${coroutineContext[CoroutineName]?.name}")
    }
    job.join()
}
A
Outer context: Outer
Inner context: Inner
Inner dispatcher: Dispatchers.IO
Back to outer context: Outer
B
Outer context: Outer
Inner context: Outer
Inner dispatcher: Dispatchers.Default
Back to outer context: Outer
C
Outer context: Outer
Inner context: Inner
Inner dispatcher: Dispatchers.Default
Back to outer context: Inner
D
Outer context: Outer
Inner context: Inner
Inner dispatcher: Dispatchers.IO
Back to outer context: Inner
Attempts:
2 left
💡 Hint
withContext changes both dispatcher and CoroutineName temporarily inside the block.