Sample questions
Question 1Easy
Which keyword marks a function that can be paused and resumed in a coroutine?
- suspend
- async
- pause
- yield
Answer: A. suspend
Explanation
A suspend function can call other suspending functions and suspend without blocking a thread.
Step-by-step solution
- Functions that can pause and resume inside a coroutine are declared with the suspend modifier.
- Only suspend functions can call delay or other suspend functions. Marked answer: option A.
Why the other options are wrong
- B. Async starts a coroutine that returns a Deferred; it does not mark a function as pausable.
- C. Pause is not a Kotlin keyword.
- D. Yield passes a value out of a sequence builder; it does not declare a suspend function.
Tip: If a function calls delay, it needs the suspend modifier.
Question 2Easy
Where can a suspend function be called from?
- Another suspend function or a coroutine builder
- Any normal function
- Only from main
- Only from Java
Answer: A. Another suspend function or a coroutine builder
Explanation
A suspend function can pause, so it must run inside a coroutine or another suspend function.
Step-by-step solution
- suspend marks pausable code.
- Needs a coroutine to resume.
- Use launch or runBlocking.
Why the other options are wrong
- B. It needs a coroutine context.
- C. main alone isn't enough.
- D. Java can't call it normally.
Question 3Medium
What does coroutineScope { ... } do if one child coroutine fails?
- It cancels the other children and rethrows the exception
- Other children continue normally
- It ignores the exception
- It restarts the failing child
Answer: A. It cancels the other children and rethrows the exception
Explanation
In structured concurrency a failing child cancels its parent scope and siblings.
Step-by-step solution
- Children are tied to the scope.
- Failure propagates up.
- Siblings are cancelled.
Why the other options are wrong
- B. Structured concurrency cancels siblings.
- C. The exception propagates.
- D. No retry happens.
Question 4Medium
How do launch and async differ?
- launch blocks; async does not
- launch returns a Job with no result; async returns a Deferred holding a result
- async needs no scope
- They are the same
Answer: B. launch returns a Job with no result; async returns a Deferred holding a result
Explanation
Use launch for fire-and-forget and async when you need to await a value.
Step-by-step solution
- launch -> Job.
- async -> Deferred<T>.
- await to get T.
Why the other options are wrong
- A. Neither blocks the caller.
- C. Both need a scope.
- D. They differ in returned type.
Question 5Medium
- A Job that represents the coroutine
- A Deferred holding a result
- The result value directly
- A Thread object
Answer: A. A Job that represents the coroutine
Explanation
launch is fire-and-forget and returns a Job; async returns a Deferred for a result.
Step-by-step solution
- Launch starts a coroutine and returns a Job.
- The Job lets you cancel or join the coroutine, but it carries no result. Marked answer: option A.
Why the other options are wrong
- B. A Deferred is returned by async, not by launch.
- C. Launch does not return the coroutine's result.
- D. Launch does not create a Thread; coroutines run on dispatchers.
Tip: Launch gives a Job with no result; async gives a Deferred with a result.
Question 6Medium
What does runBlocking do?
- Starts a coroutine and returns immediately without waiting for it
- Blocks the current thread until the coroutine inside completes
- Cancels every other coroutine that is currently running in the program
- Switches execution over to the main UI thread of the app
Answer: B. Blocks the current thread until the coroutine inside completes
Explanation
runBlocking bridges blocking and suspending code by blocking the calling thread until its body finishes.
Step-by-step solution
runBlocking blocks the current thread until the coroutine inside completes.- It allows suspending functions to be called from non-coroutine code.
- The function returns only after the coroutine finishes execution.
- This ensures the main thread waits for the coroutine's result.
- So,
runBlocking blocks the current thread until the coroutine inside completes.
Why the other options are wrong
- A. runBlocking waits for the coroutine to finish before returning; it does not return immediately.
- C. runBlocking does not cancel other coroutines; it only blocks the current thread.
- D. runBlocking blocks the current thread; it does not switch to a UI main thread.
Tip: Use runBlocking to run coroutines in blocking code, like main functions.
Question 7Medium
How do you get the result of async { compute() }?
- Call join() on the Job
- Read the .value property
- Call get() on the thread
- Call await() on the Deferred
Answer: D. Call await() on the Deferred
Explanation
async returns Deferred<T>; await() suspends until the result is ready.
Step-by-step solution
- Async returns a Deferred, which carries the result of the coroutine.
- Calling await() on the Deferred suspends until the result is ready and returns it. Marked answer: option D.
Why the other options are wrong
- A. join() waits for a Job to finish; it does not return a value.
- B. Deferred has no value property; the result comes from await().
- C. Coroutines are not threads, so there is no get() call on a thread here.
Tip: Use await() on a Deferred to get its result.
Question 8Medium
What is the output?
runBlocking {
val d = async { 2 }
val e = async { 3 }
println(d.await() + e.await())
}
- 23
- Deferred(2)Deferred(3)
- 6
- 5
Answer: D. 5
Explanation
async returns Deferred results; await() fetches each and the sum is 5.
Step-by-step solution
- d = 2, e = 3.
- await both.
- 2 + 3 = 5.
Why the other options are wrong
- A. await returns Int values that are added.
- B. await returns the result, not the Deferred.
- C. 2 + 3 = 5.
Question 9Hard
Which is true of structured concurrency?
- Child coroutines continue running freely after the parent is cancelled
- A parent waits for all its children, and cancelling it cancels them
- Coroutines can run only on the single global scope, never nested inside each other
- Exceptions thrown in a child coroutine are never propagated to the parent
Answer: B. A parent waits for all its children, and cancelling it cancels them
Explanation
Coroutines form a hierarchy through scope and Job, so lifetimes and cancellation are tied to the parent.
Step-by-step solution
- Structured concurrency ties parent and child coroutine lifetimes.
- Parent waits for all children to finish before completing.
- Cancelling parent automatically cancels all its children.
Why the other options are wrong
- A. Cancelling a parent cancels its children in structured concurrency, so they do not run freely.
- C. Coroutines can be nested in a parent scope; the global scope is not required.
- D. Child failures propagate to the parent by default, which cancels siblings.
Tip: In structured concurrency, parents control children's lifecycle and cancellation.
Question 10Hard
What is true about a Flow built with flow { emit(1) }?
- It is cold: the block runs only when collected
- It starts emitting as soon as it is created
- It is shared between all collectors
- It cannot be cancelled
Answer: A. It is cold: the block runs only when collected
Explanation
A cold Flow does nothing until a terminal operator like collect runs, and each collector restarts the block.
Step-by-step solution
- Define flow: nothing runs.
- collect starts it.
- Each collector runs the block again.
Why the other options are wrong
- B. That is hot behaviour.
- C. Each collector triggers a new run.
- D. Collection can be cancelled.
Practise all 13 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 →