Coroutines & Flow Questions with Answers

21 practice questions on Coroutines & Flow (Android (Kotlin & Compose)) with answers and step-by-step solutions. Try 10 samples below, then practise all 21 free in the AptiNest app.

Sample questions

Question 1Easy

Which dispatcher should be used for network or disk I/O?

  1. Dispatchers.IO
  2. Dispatchers.Default
  3. Dispatchers.Unconfined
  4. Dispatchers.Main

Answer: A. Dispatchers.IO

Explanation

IO has a pool sized for blocking I/O; Default is for CPU-heavy work; Main is the UI thread.

Step-by-step solution

  1. I/O work mostly waits on network or disk.
  2. Dispatchers.IO uses a large thread pool built for blocking I/O.
  3. Use withContext(Dispatchers.IO) for such calls.

Why the other options are wrong

  • B. Default is sized for CPU work, roughly one thread per core.
  • C. Unconfined runs code wherever it resumes; it is not meant for I/O.
  • D. Main is the UI thread; blocking it freezes the app.

Question 2Easy

A suspend function in Kotlin:

  1. Can be called from any normal function
  2. Can pause without blocking its thread
  3. Blocks the main thread until done
  4. Always runs on a new thread

Answer: B. Can pause without blocking its thread

Explanation

Suspend functions can only be called from coroutines or other suspend functions.

Step-by-step solution

  1. A suspend function can pause at suspension points such as delay or network calls.
  2. While paused it frees its thread for other work.
  3. It resumes later, possibly on another thread.

Why the other options are wrong

  • A. suspend functions can be called only from coroutines or other suspend functions.
  • C. Suspending is designed not to block.
  • D. suspend does not create threads; the dispatcher decides where code runs.

Question 3Medium

withContext(Dispatchers.IO) { ... } is used to:

  1. Start a new independent coroutine
  2. Block the main thread
  3. Cancel the current coroutine
  4. Run a block on another dispatcher

Answer: D. Run a block on another dispatcher

Explanation

It switches context for the block and resumes in the original context afterwards.

Step-by-step solution

  1. withContext switches the current coroutine to another dispatcher.
  2. It runs the block there and suspends the caller until it finishes.
  3. It returns the block's result, then continues on the original dispatcher.

Why the other options are wrong

  • A. It does not start an independent coroutine; launch does that.
  • B. It suspends rather than blocks.
  • C. It does not cancel anything.

Question 4Medium

What happens to coroutines launched in viewModelScope when the ViewModel is cleared?

  1. They move to GlobalScope
  2. They keep running forever
  3. They are cancelled
  4. They restart automatically

Answer: C. They are cancelled

Explanation

viewModelScope is cancelled in onCleared, preventing leaks.

Step-by-step solution

  1. viewModelScope is tied to the ViewModel's lifetime.
  2. When the ViewModel is cleared, onCleared runs and the scope is cancelled.
  3. All its coroutines are cancelled.

Why the other options are wrong

  • A. Coroutines never move scopes.
  • B. Scopes exist precisely to stop that.
  • D. Cancelled coroutines do not restart.

Question 5Medium

Which Flow operator combines the latest values of two flows whenever either changes?

  1. single
  2. first
  3. take
  4. combine

Answer: D. combine

Explanation

combine(a, b) { x, y -> ... } emits whenever either source emits, using the latest of each.

Step-by-step solution

  1. combine takes two or more flows.
  2. Whenever any emits, it calls the lambda with the latest value of each.
  3. This suits merging, e.g., a search query with a filter setting.

Why the other options are wrong

  • A. single expects exactly one value.
  • B. first takes only the first value.
  • C. take limits the count.

Tip: combine = latest of each; zip = pair by pair.

Question 6Medium

What does this print?

runBlocking {
    launch { delay(100); println("A") }
    println("B")
}
  1. B then A
  2. Only A
  3. Only B
  4. A then B

Answer: A. B then A

Explanation

launch starts a coroutine that suspends on delay, so B prints first; runBlocking waits for the child before returning.

Step-by-step solution

  1. launch starts a child coroutine and returns immediately.
  2. println("B") runs first while the child waits in delay(100).
  3. After 100 ms the child prints A; runBlocking waits for it.

Why the other options are wrong

  • B. B prints right away.
  • C. runBlocking waits for its children, so A prints too.
  • D. The child is delayed, so B comes first.

Question 7Medium

launch differs from async in that launch:

  1. Returns a Deferred with a value
  2. Blocks the current thread
  3. Returns a Job and no result
  4. Can only run on Dispatchers.IO

Answer: C. Returns a Job and no result

Explanation

Use async/await when you need a result; launch is fire-and-forget within a scope.

Step-by-step solution

  1. launch returns a Job: you can cancel or join it, but it has no result.
  2. async returns a Deferred<T>; await() gives the result.
  3. Use launch for fire-and-forget work, async for results.

Why the other options are wrong

  • A. Deferred with a value comes from async.
  • B. Neither blocks the thread.
  • D. Both can run on any dispatcher.

Question 8Medium

In Jetpack Compose, which function converts a StateFlow from a ViewModel into Compose state in a lifecycle-aware way?

  1. toLiveData()
  2. launchIn(GlobalScope)
  3. observeForever()
  4. collectAsStateWithLifecycle()

Answer: D. collectAsStateWithLifecycle()

Explanation

It stops collecting when the lifecycle falls below STARTED.

Step-by-step solution

  1. collectAsStateWithLifecycle() collects a Flow into Compose State.
  2. It stops collecting when the lifecycle drops below STARTED.
  3. Changes trigger recomposition.

Why the other options are wrong

  • A. That is not the Compose API for this.
  • B. GlobalScope ignores lifecycle.
  • C. observeForever is a LiveData method that ignores lifecycle.

Question 9Hard

A ViewModel exposes UI state. Which pattern is recommended?

  1. A global static variable
  2. Private mutable, public read-only
  3. Public MutableStateFlow the UI can set
  4. A public var List the UI mutates

Answer: B. Private mutable, public read-only

Explanation

Keeping the mutable type private ensures only the ViewModel changes state (unidirectional data flow).

Step-by-step solution

  1. Keep a private MutableStateFlow inside the ViewModel.
  2. Expose a public StateFlow (read-only) to the UI.
  3. Only the ViewModel can change state; the UI just observes and sends events.

Why the other options are wrong

  • A. Global statics break lifecycle and testability.
  • C. Letting the UI change state directly breaks unidirectional data flow.
  • D. A mutable public list cannot be observed safely.

Question 10Hard

In structured concurrency, if one child launched with launch inside coroutineScope throws an exception, what happens by default?

  1. The app retries the child
  2. The exception is ignored
  3. The whole scope is cancelled
  4. Only that child stops; siblings continue

Answer: C. The whole scope is cancelled

Explanation

Failure propagates up and cancels the scope; supervisorScope or SupervisorJob isolates child failures.

Step-by-step solution

  1. In coroutineScope, a failing child cancels its parent.
  2. The parent then cancels all other children.
  3. The exception is rethrown to the caller.

Why the other options are wrong

  • A. Coroutines do not retry automatically.
  • B. Exceptions propagate; they are not ignored.
  • D. That is supervisorScope or SupervisorJob behaviour.

Tip: coroutineScope: one fails, all cancel. supervisorScope: failures stay isolated.

Practise all 21 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Android (Kotlin & Compose) topics · All topics