0
0
Kotlinprogramming~5 mins

Coroutine context and dispatchers in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a Coroutine Context in Kotlin?
A Coroutine Context is a set of elements that define the behavior of a coroutine, such as its job, dispatcher, and other metadata. It controls how and where the coroutine runs.
Click to reveal answer
beginner
What role does a Dispatcher play in Kotlin coroutines?
A Dispatcher decides the thread or thread pool on which the coroutine runs. It controls the execution environment, like running on the main thread, IO thread, or a background thread pool.
Click to reveal answer
intermediate
Name the four standard dispatchers provided by Kotlin coroutines.
The four standard dispatchers are:<br>1. Dispatchers.Main - runs on the main UI thread<br>2. Dispatchers.IO - optimized for IO tasks<br>3. Dispatchers.Default - for CPU-intensive work<br>4. Dispatchers.Unconfined - starts coroutine in the current thread but can resume in another
Click to reveal answer
intermediate
How can you combine multiple elements in a Coroutine Context?
You can combine elements using the + operator. For example, Dispatchers.IO + Job() combines a dispatcher with a job to form a complete context.
Click to reveal answer
intermediate
What happens if you launch a coroutine without specifying a dispatcher?
If no dispatcher is specified, the coroutine inherits the dispatcher from its parent coroutine context. If there is no parent, it uses Dispatchers.Default by default.
Click to reveal answer
Which dispatcher should you use for network or disk operations in Kotlin coroutines?
ADispatchers.Default
BDispatchers.Main
CDispatchers.IO
DDispatchers.Unconfined
What does Dispatchers.Main do?
ARuns coroutines on a background thread pool
BRuns coroutines on the main UI thread
CRuns coroutines without any thread confinement
DRuns coroutines on the IO thread pool
How do you combine a Job and a Dispatcher in a coroutine context?
AUsing the | operator
BUsing the & operator
CUsing the * operator
DUsing the + operator
What is the default dispatcher if none is specified and no parent context exists?
ADispatchers.Default
BDispatchers.Main
CDispatchers.IO
DDispatchers.Unconfined
Which dispatcher starts coroutine in the current thread but can resume in another thread?
ADispatchers.Unconfined
BDispatchers.Main
CDispatchers.IO
DDispatchers.Default
Explain what a coroutine context is and why dispatchers are important in Kotlin coroutines.
Think about how coroutines know where and how to run.
You got /3 concepts.
    List and describe the four main dispatchers in Kotlin coroutines and when to use each.
    Consider UI updates, IO tasks, CPU work, and thread flexibility.
    You got /5 concepts.