Sample questions
Question 1Easy
Which dispatcher should be used for network or disk I/O?
- Dispatchers.IO
- Dispatchers.Default
- Dispatchers.Unconfined
- 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
- I/O work mostly waits on network or disk.
- Dispatchers.IO uses a large thread pool built for blocking I/O.
- 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:
- Can be called from any normal function
- Can pause without blocking its thread
- Blocks the main thread until done
- 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
- A suspend function can pause at suspension points such as delay or network calls.
- While paused it frees its thread for other work.
- 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:
- Start a new independent coroutine
- Block the main thread
- Cancel the current coroutine
- 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
- withContext switches the current coroutine to another dispatcher.
- It runs the block there and suspends the caller until it finishes.
- 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?
- They move to GlobalScope
- They keep running forever
- They are cancelled
- They restart automatically
Answer: C. They are cancelled
Explanation
viewModelScope is cancelled in onCleared, preventing leaks.
Step-by-step solution
- viewModelScope is tied to the ViewModel's lifetime.
- When the ViewModel is cleared, onCleared runs and the scope is cancelled.
- 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?
- single
- first
- take
- combine
Answer: D. combine
Explanation
combine(a, b) { x, y -> ... } emits whenever either source emits, using the latest of each.
Step-by-step solution
- combine takes two or more flows.
- Whenever any emits, it calls the lambda with the latest value of each.
- 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")
}
- B then A
- Only A
- Only B
- 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
- launch starts a child coroutine and returns immediately.
- println("B") runs first while the child waits in delay(100).
- 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:
- Returns a Deferred with a value
- Blocks the current thread
- Returns a Job and no result
- 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
launch returns a Job: you can cancel or join it, but it has no result.async returns a Deferred<T>; await() gives the result.- 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?
toLiveData()launchIn(GlobalScope)observeForever()collectAsStateWithLifecycle()
Answer: D. collectAsStateWithLifecycle()
Explanation
It stops collecting when the lifecycle falls below STARTED.
Step-by-step solution
- collectAsStateWithLifecycle() collects a Flow into Compose State.
- It stops collecting when the lifecycle drops below STARTED.
- 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?
- A global static variable
- Private mutable, public read-only
- Public MutableStateFlow the UI can set
- 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
- Keep a private MutableStateFlow inside the ViewModel.
- Expose a public StateFlow (read-only) to the UI.
- 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?
- The app retries the child
- The exception is ignored
- The whole scope is cancelled
- 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
- In coroutineScope, a failing child cancels its parent.
- The parent then cancels all other children.
- 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 →