Challenge - 5 Problems
CoroutineScope Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Understanding CoroutineScope context
What is the default dispatcher used by
CoroutineScope when none is specified?Attempts:
2 left
💡 Hint
Think about the dispatcher optimized for CPU-intensive work.
✗ Incorrect
When you create a CoroutineScope without specifying a dispatcher, it uses Dispatchers.Default by default, which is optimized for CPU-intensive tasks.
❓ ui_behavior
intermediate2:00remaining
Choosing dispatcher for network calls
Which dispatcher should you use to perform network requests without blocking the main thread?
Attempts:
2 left
💡 Hint
This dispatcher is optimized for blocking IO tasks like file or network operations.
✗ Incorrect
Dispatchers.IO is designed for offloading blocking IO tasks such as network or disk operations, keeping the main thread free.
❓ lifecycle
advanced2:00remaining
CoroutineScope and Android lifecycle
What happens if you launch a coroutine in a
CoroutineScope tied to an Activity and the Activity is destroyed before the coroutine completes?Attempts:
2 left
💡 Hint
Think about how lifecycle-aware scopes behave to prevent memory leaks.
✗ Incorrect
CoroutineScopes tied to lifecycle components like Activities are cancelled automatically when the lifecycle is destroyed, cancelling all running coroutines.
📝 Syntax
advanced2:00remaining
Correct dispatcher usage in coroutine launch
Which of the following code snippets correctly launches a coroutine on the IO dispatcher?
Android Kotlin
fun fetchData() {
CoroutineScope(Dispatchers.IO).launch {
// network call
}
}Attempts:
2 left
💡 Hint
Remember how to pass dispatcher to CoroutineScope constructor.
✗ Incorrect
You must pass the dispatcher to CoroutineScope constructor and then call launch without parameters to run on that dispatcher.
🔧 Debug
expert2:00remaining
Diagnosing coroutine dispatcher misuse
Given this code snippet, what error or behavior will occur?
fun loadData() {
CoroutineScope(Dispatchers.Main).launch {
Thread.sleep(3000)
println("Data loaded")
}
}
Attempts:
2 left
💡 Hint
Consider what Thread.sleep does on the main thread.
✗ Incorrect
Calling Thread.sleep on Dispatchers.Main blocks the main thread, freezing the UI for the sleep duration.