Sample questions
Kt Kotlin Coroutines FlowDifficulty 1
What does marking a function with the suspend modifier actually mean in Kotlin?
- aIt lets the function suspend without blocking the caller thread.✓
- bIt runs the function on a background thread automatically.
- cIt makes the function's return value nullable.
- dIt forces the function to run synchronously before any other code.
Explanation:suspend is a compiler marker: it lets the function call other suspend functions and be paused/resumed by the coroutine machinery. It says nothing about which thread runs it — that's decided by the dispatcher.
Kt Kotlin Coroutines FlowDifficulty 1
Why can't a suspend function be called directly from ordinary (non-suspend) code?
- aBecause suspend functions always throw an exception when called synchronously.
- bBecause suspending needs a coroutine context to manage pause/resume.✓
- cBecause the Kotlin compiler physically removes suspend functions from release builds.
- dBecause suspend functions can only exist inside
object declarations.
Explanation:A suspend function needs a Continuation and a coroutine scope to actually suspend/resume; ordinary code has no such mechanism, so the compiler requires a coroutine builder (launch, runBlocking, etc.) or another suspend function as the caller.
Kt Kotlin Coroutines FlowDifficulty 2
What is the key difference between launch and async as coroutine builders?
- a
launch returns a result-less Job; async returns an awaitable Deferred<T>.✓ - b
async always runs on Dispatchers.IO, launch always runs on Dispatchers.Main. - c
launch can only be called inside runBlocking, async cannot. - dThere is no real difference; they are aliases for the same function.
Explanation:Both start a new coroutine, but launch is fire-and-forget (its Job only tracks completion/cancellation), whereas async is meant for computing a value you'll retrieve later via await().
Kt Kotlin Coroutines FlowDifficulty 2
Inside a coroutineScope { ... } block, one of two concurrently launched child coroutines throws an exception. What happens to the sibling coroutine and the scope?
- aThe sibling keeps running to completion unaffected; only the failing child's result is lost.
- bNothing happens until the scope's parent function returns.
- cThe exception is silently swallowed since
coroutineScope catches all child exceptions. - dThe sibling is cancelled and the exception propagates out of the
coroutineScope block.✓
Explanation:coroutineScope uses structured concurrency with fail-fast semantics: an uncaught child failure cancels all sibling children and re-throws from the scope itself. This differs from supervisorScope, where siblings are isolated.
Kt Kotlin Coroutines FlowDifficulty 1
What is Dispatchers.Main generally used for in Android coroutines?
- aRunning disk or network I/O operations.
- bRunning CPU-intensive background computation.
- cRunning code that must touch the UI thread.✓
- dRunning code that must never be cancelled.
Explanation:Dispatchers.Main confines execution to the UI (main) thread — required for any View/UI update — while Dispatchers.IO and Dispatchers.Default are meant for blocking I/O and CPU-bound work respectively.
Kt Kotlin Coroutines FlowDifficulty 2
Why does Dispatchers.IO typically use a much larger thread pool than Dispatchers.Default?
- aBecause I/O tasks require more CPU cores than computation tasks.
- bBecause
Dispatchers.IO runs on the UI thread and needs extra capacity for that. - cBecause I/O mostly waits on external resources instead of using the CPU.✓
- dThere is no real difference in pool size between the two.
Explanation:Dispatchers.Default is sized around the number of CPU cores (compute-bound work), while Dispatchers.IO allows a much larger number of concurrently blocked threads since I/O work spends most of its time waiting, not consuming CPU.