Recall & Review
beginner
What is a CoroutineScope in Kotlin coroutines?
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:<br>• Dispatchers.Main - runs on the main thread (UI thread)<br>• Dispatchers.IO - optimized for disk or network I/O<br>• Dispatchers.Default - for CPU-intensive work<br>• 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?
✗ Incorrect
Dispatchers.Main runs coroutines on the main thread, which is required for UI updates.
What does CoroutineScope control?
✗ Incorrect
CoroutineScope manages when coroutines start and stop, controlling their lifecycle.
Which dispatcher is best for CPU-intensive tasks?
✗ Incorrect
Dispatchers.Default is optimized for CPU-heavy work like sorting or calculations.
What happens when a CoroutineScope is cancelled?
✗ 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?
✗ 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.