Master Kotlin’s advanced coroutine concepts, including coroutine scopes and contexts, structured concurrency, and the power of channels and flows. Unlock scalable, concurrent programming in your Kotlin applications.
Coroutine Scopes and Contexts
What does a coroutine scope define in Kotlin? a) The lifetime of a coroutine b) The priority of the coroutine c) The thread used by the coroutine d) The return type of the coroutine
Which function is used to create a new coroutine scope? a) launch b) runBlocking c) CoroutineScope d) createScope
What is the default dispatcher for a coroutine launched inside runBlocking? a) Dispatchers.IO b) Dispatchers.Main c) Dispatchers.Default d) Dispatchers.Unconfined
What does Dispatchers.IO handle in Kotlin coroutines? a) Background tasks and computation b) Input/output operations c) UI thread operations d) Thread scheduling
How can you explicitly specify a coroutine context in Kotlin? a) By calling CoroutineContext constructor b) By passing a context as an argument to launch or async c) By using a context keyword d) By setting a global variable
Which coroutine context element is used to manage the job of a coroutine? a) Job b) Dispatcher c) CoroutineScope d) CoroutineContext
What is the purpose of SupervisorJob in Kotlin coroutines? a) It cancels the parent coroutine when a child fails. b) It allows child coroutines to fail independently. c) It retries failed child coroutines. d) It creates a global context for all coroutines.
What does the withContext function do in Kotlin? a) Starts a new coroutine in the provided context b) Cancels the current coroutine c) Launches a new thread d) Suspends the coroutine without changing its context
Structured Concurrency
What does structured concurrency aim to achieve in Kotlin? a) More flexible error handling b) Avoiding unstructured and orphaned coroutines c) Better thread management d) Faster execution of coroutines
Which function ensures structured concurrency by blocking until the coroutines are completed? a) async b) launch c) runBlocking d) delay
What happens if a coroutine launched by launch inside runBlocking fails in structured concurrency? a) The failure will be ignored. b) All coroutines in the scope will be cancelled. c) Only the failed coroutine will be cancelled. d) The failure will cause a crash.
Which method can be used to handle the failure of a coroutine and allow others to continue in structured concurrency? a) try-catch b) SupervisorScope c) finally d) waitFor
What does supervisorScope allow in Kotlin? a) It cancels all coroutines in case of a failure. b) It prevents a failure from affecting other coroutines. c) It allows nested coroutines to run asynchronously. d) It provides an isolated coroutine context.
How do you properly ensure the cancellation of all coroutines in a scope upon failure? a) Use SupervisorScope b) Use runBlocking c) Use CoroutineScope.cancel() d) Use Job.cancel()
In structured concurrency, what does the join() function do? a) Starts a coroutine and blocks until it completes b) Cancels a running coroutine c) Waits for the result of a coroutine d) Waits for a coroutine to finish and handles exceptions
What is the default behavior of a coroutine when it’s launched inside a scope? a) It runs on a background thread b) It executes and is automatically cancelled after use c) It executes until the parent scope is cancelled d) It executes in parallel with all coroutines in the scope
Channels and Flows
What is the purpose of Channel in Kotlin coroutines? a) To handle multiple coroutines concurrently b) To send and receive data between coroutines c) To dispatch data to a specific thread d) To launch coroutines
Which of the following is true about channels in Kotlin? a) Channels are blocking and cannot be used for asynchronous tasks. b) Channels allow multiple coroutines to send and receive values. c) Channels are used for handling thread synchronization. d) Channels are not thread-safe.
How do you create a channel in Kotlin? a) val channel = Channel<Int>() b) val channel = CoroutineChannel() c) val channel = Channel() d) val channel = createChannel()
What function is used to send data into a channel in Kotlin? a) sendData() b) offer() c) send() d) push()
How do you receive data from a channel in Kotlin? a) receive() b) get() c) take() d) receiveData()
What is the primary difference between Flow and Channel in Kotlin? a) Flow allows for concurrent writes, whereas Channel does not. b) Flow is cold and Channel is hot. c) Flow requires thread synchronization, but Channel does not. d) Flow is used for thread management, while Channel is used for UI operations.
Which function is used to create a simple flow in Kotlin? a) flow() b) createFlow() c) Flow.create() d) create()
What does collect() do in Kotlin’s Flow? a) Starts the flow and collects values emitted by it b) Stops the flow c) Suspends the coroutine d) Returns the result of the flow
What is the purpose of flowOn() in Kotlin flows? a) It executes the flow on a specified dispatcher. b) It cancels the flow. c) It defines the coroutine scope for the flow. d) It suspends the flow for a certain period.
What is a key advantage of using Flow over Channel in Kotlin? a) Flow is cold, which means it only starts emitting when collected. b) Flow allows data to be sent only once. c) Flow is always blocking. d) Flow can send data synchronously.
What is buffer() in Kotlin’s Flow used for? a) It handles exception throwing. b) It adds elements to a buffer and allows for non-blocking data emission. c) It waits for all elements to be collected. d) It suspends the flow until the buffer is full.
Which operator is used to transform items emitted by a flow? a) map() b) flatMap() c) transform() d) convert()
What is collectLatest() used for in Kotlin’s Flow? a) It collects the latest emitted item and cancels any ongoing collection. b) It collects all items, even the older ones. c) It waits until all items are processed. d) It converts items to a list and returns the result.
What happens if a coroutine is cancelled while receiving data from a channel in Kotlin? a) The coroutine continues execution after receiving the data. b) The channel will throw an exception. c) The coroutine will be suspended. d) The channel closes, and no more data can be received.
Answer Key
QNo
Answer (Option with text)
1
a) The lifetime of a coroutine
2
c) CoroutineScope
3
c) Dispatchers.Default
4
b) Input/output operations
5
b) By passing a context as an argument to launch or async
6
a) Job
7
b) It allows child coroutines to fail independently
8
a) Starts a new coroutine in the provided context
9
b) Avoiding unstructured and orphaned coroutines
10
c) runBlocking
11
b) All coroutines in the scope will be cancelled.
12
b) SupervisorScope
13
b) It prevents a failure from affecting other coroutines
14
c) Use CoroutineScope.cancel()
15
a) Starts a coroutine and blocks until it completes
16
c) It executes until the parent scope is cancelled
17
b) To send and receive data between coroutines
18
b) Channels allow multiple coroutines to send and receive values.
19
a) val channel = Channel<Int>()
20
c) send()
21
a) receive()
22
b) Flow is cold and Channel is hot.
23
a) flow()
24
a) Starts the flow and collects values emitted by it
25
a) It executes the flow on a specified dispatcher.
26
a) Flow is cold, which means it only starts emitting when collected.
27
b) It adds elements to a buffer and allows for non-blocking data emission.
28
a) map()
29
a) It collects the latest emitted item and cancels any ongoing collection.
30
d) The channel closes, and no more data can be received.