Challenge - 5 Problems
Coroutine Context Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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() }
Attempts:
2 left
💡 Hint
Think about which thread the Default dispatcher uses in Kotlin coroutines.
✗ Incorrect
The Dispatchers.Default uses a shared pool of background threads named like DefaultDispatcher-worker-x. The main thread is named 'main', so option B is incorrect. IO dispatcher uses threads named IO-x, and CommonPool is deprecated.
❓ Predict Output
intermediate2: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}") }
Attempts:
2 left
💡 Hint
When merging contexts, the last CoroutineName overrides the previous one.
✗ Incorrect
When two CoroutineName elements are combined, the one from the right side (context2) overrides the left. So the name is 'Second'.
🔧 Debug
advanced2: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() }
Attempts:
2 left
💡 Hint
Dispatchers.Main requires a special setup in JVM environments.
✗ Incorrect
On JVM without Android or JavaFX, Dispatchers.Main is not available by default and throws IllegalStateException about missing module.
🧠 Conceptual
advanced1:30remaining
Which dispatcher is best for CPU-intensive tasks?
In Kotlin coroutines, which dispatcher is designed to efficiently handle CPU-intensive work?
Attempts:
2 left
💡 Hint
Think about which dispatcher uses a shared pool of threads optimized for CPU work.
✗ Incorrect
Dispatchers.Default uses a shared pool of threads optimized for CPU-intensive tasks. Dispatchers.IO is for blocking IO tasks. Unconfined starts coroutine in current thread but is not optimized. Main is for UI thread.
❓ Predict Output
expert3: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() }
Attempts:
2 left
💡 Hint
withContext changes both dispatcher and CoroutineName temporarily inside the block.
✗ Incorrect
Inside withContext, the coroutine context is replaced with Dispatchers.IO and CoroutineName("Inner"). After withContext block, the context reverts to the outer one.