A CoroutineScope defines the lifecycle for coroutines. It controls when coroutines start and when they are cancelled, helping manage background work safely.
Click to reveal answer
beginner
Name the main types of Dispatchers in Kotlin coroutines.
The main dispatchers are: • Dispatchers.Main - runs on the main thread (UI thread) • Dispatchers.IO - optimized for disk or network I/O • Dispatchers.Default - for CPU-intensive work • Dispatchers.Unconfined - starts coroutine in current thread but can resume in another
Click to reveal answer
beginner
Why use Dispatchers.IO for network or disk operations?
Dispatchers.IO uses a shared pool of threads optimized for blocking I/O tasks. This keeps the main thread free and prevents UI freezes.
Click to reveal answer
intermediate
What happens if you launch a coroutine without specifying a dispatcher?
It inherits the dispatcher from its CoroutineScope. If the scope uses Dispatchers.Main, the coroutine runs on the main thread by default.
Click to reveal answer
intermediate
Explain the role of CoroutineScope in managing coroutine lifecycles.
CoroutineScope ties coroutines to a lifecycle, like an Activity or ViewModel. When the scope is cancelled, all coroutines inside it are also cancelled, preventing memory leaks.
Click to reveal answer
Which dispatcher should you use for updating the UI in Android?
ADispatchers.Default
BDispatchers.IO
CDispatchers.Main
DDispatchers.Unconfined
✗ Incorrect
Dispatchers.Main runs coroutines on the main thread, which is required for UI updates.
What does CoroutineScope control?
AThe speed of coroutines
BThe memory usage of coroutines
CThe number of threads used
DThe lifecycle and cancellation of coroutines
✗ Incorrect
CoroutineScope manages when coroutines start and stop, controlling their lifecycle.
Which dispatcher is best for CPU-intensive tasks?
ADispatchers.IO
BDispatchers.Default
CDispatchers.Main
DDispatchers.Unconfined
✗ Incorrect
Dispatchers.Default is optimized for CPU-heavy work like sorting or calculations.
What happens when a CoroutineScope is cancelled?
AAll coroutines in that scope are cancelled
BOnly the first coroutine is cancelled
CCoroutines continue running
DThe app crashes
✗ Incorrect
Cancelling a CoroutineScope cancels all coroutines launched in it to avoid leaks.
Which dispatcher starts a coroutine in the current thread but can resume in another?
ADispatchers.Unconfined
BDispatchers.IO
CDispatchers.Default
DDispatchers.Main
✗ Incorrect
Dispatchers.Unconfined starts in the current thread but resumes wherever the suspending function resumes.
Describe how CoroutineScope and dispatchers work together to manage background tasks in Android.
Think about how you keep coroutines organized and where they run.
You got /3 concepts.
Explain why choosing the right dispatcher is important for app performance and user experience.
Consider what happens if you do heavy work on the main thread.
You got /4 concepts.
Practice
(1/5)
1. What is the main role of CoroutineScope in Kotlin coroutines?
easy
A. It pauses the coroutine execution.
B. It decides which thread the coroutine runs on.
C. It controls the lifecycle and context where coroutines run.
D. It cancels all running threads in the app.
Solution
Step 1: Understand CoroutineScope purpose
CoroutineScope defines the lifecycle and context for coroutines, managing their start and cancellation.
Step 2: Differentiate from Dispatchers
Dispatchers decide the thread, but CoroutineScope manages the coroutine's lifecycle and context.
Final Answer:
It controls the lifecycle and context where coroutines run. -> Option C
Quick Check:
CoroutineScope = lifecycle control [OK]
Hint: CoroutineScope manages lifecycle, not threads [OK]
Common Mistakes:
Confusing CoroutineScope with Dispatchers
Thinking CoroutineScope controls threads
Assuming CoroutineScope pauses coroutines
2. Which of the following is the correct way to launch a coroutine on the main thread using CoroutineScope and Dispatchers?
easy
A. CoroutineScope(Dispatchers.IO).launch { /* code */ }
B. CoroutineScope(Dispatchers.Main).launch { /* code */ }
C. CoroutineScope(Dispatchers.Default).launch { /* code */ }
D. CoroutineScope(Dispatchers.Unconfined).launch { /* code */ }
Solution
Step 1: Identify dispatcher for main thread
Dispatchers.Main is used to run coroutines on the main (UI) thread.
Step 2: Match with CoroutineScope usage
Using CoroutineScope(Dispatchers.Main).launch { ... } correctly launches coroutine on main thread.
Final Answer:
CoroutineScope(Dispatchers.Main).launch { /* code */ } -> Option B
Quick Check:
Main thread = Dispatchers.Main [OK]
Hint: Main thread uses Dispatchers.Main [OK]
Common Mistakes:
Using Dispatchers.IO for UI updates
Confusing Default with Main dispatcher
Using Unconfined without understanding
3. What will be the output of this Kotlin code snippet?
fun main() = runBlocking {
launch(Dispatchers.Default) {
println("Running on thread: ${Thread.currentThread().name}")
}
}
medium
A. Running on thread: DefaultDispatcher-worker-1
B. Running on thread: Unconfined
C. Running on thread: IO-thread-1
D. Running on thread: main
Solution
Step 1: Understand Dispatchers.Default behavior
Dispatchers.Default runs coroutines on a shared background thread pool, named like DefaultDispatcher-worker-1.
Step 2: Analyze thread name printed
The println shows the thread name, which will be a DefaultDispatcher worker thread, not main or IO.
Final Answer:
Running on thread: DefaultDispatcher-worker-1 -> Option A
Instead of Thread.sleep(), use delay() which suspends without blocking the thread.
Final Answer:
Using Thread.sleep() blocks the IO dispatcher thread. -> Option D
Quick Check:
Blocking calls like Thread.sleep() cause issues [OK]
Hint: Avoid Thread.sleep() in coroutines; use delay() [OK]
Common Mistakes:
Thinking withContext can't switch dispatchers
Believing CoroutineScope can't use Dispatchers.Main
Assuming updateUI() can't run in coroutine
5. You want to perform a long-running network request without blocking the UI, then update the UI with the result. Which coroutine setup is best practice?
hard
A. Launch coroutine in Dispatchers.Main, then use withContext(Dispatchers.IO) for network call, update UI after.
B. Launch coroutine in Dispatchers.IO, then update UI directly inside it.
C. Launch coroutine in Dispatchers.Default, then use Thread.sleep() for delay, update UI after.
D. Launch coroutine in Dispatchers.Unconfined, then update UI after network call.
Solution
Step 1: Identify correct dispatcher for network call
Network calls should run on Dispatchers.IO to avoid blocking the main thread.
Step 2: Update UI on main thread
UI updates must happen on Dispatchers.Main to avoid errors and keep UI responsive.
Step 3: Combine withContext and launch properly
Launch on Main, switch to IO for network call with withContext, then back to Main for UI update.
Final Answer:
Launch coroutine in Dispatchers.Main, then use withContext(Dispatchers.IO) for network call, update UI after. -> Option A
Quick Check:
Network on IO, UI on Main [OK]
Hint: Use withContext(IO) inside Main coroutine for network [OK]
Common Mistakes:
Updating UI from IO dispatcher
Blocking threads with Thread.sleep()
Using Unconfined dispatcher for UI updates
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.