A CoroutineScope defines the lifecycle of coroutines. It controls when coroutines start and when they are cancelled, helping to manage concurrency safely.
Click to reveal answer
beginner
Explain structured concurrency in Kotlin coroutines.
Structured concurrency means that coroutines are launched in a specific scope and must complete or cancel before the scope ends. This keeps concurrency organized and prevents leaks.
Click to reveal answer
intermediate
What happens when a parent coroutine is cancelled in structured concurrency?
All child coroutines launched in the parent's scope are automatically cancelled, ensuring no orphan coroutines run outside the intended lifecycle.
Click to reveal answer
intermediate
How does launch differ from async in a CoroutineScope?
launch starts a coroutine that returns a Job and is used for tasks without a result. async returns Deferred and is used when you want a result from the coroutine.
Click to reveal answer
beginner
Why is it important to use CoroutineScope for launching coroutines?
Using CoroutineScope ties coroutines to a lifecycle, so they can be cancelled properly. This avoids memory leaks and unexpected behavior in apps.
Click to reveal answer
What does structured concurrency ensure in Kotlin coroutines?
AChild coroutines complete before their parent coroutine ends
BCoroutines run only on the main thread
CCoroutines run independently without any lifecycle control
DCoroutines never get cancelled
✗ Incorrect
Structured concurrency ensures that child coroutines complete or cancel before their parent coroutine ends, keeping concurrency organized.
Which Kotlin function launches a coroutine that returns a result?
Aasync
BrunBlocking
Cdelay
Dlaunch
✗ Incorrect
async launches a coroutine that returns a Deferred result, which can be awaited.
If a parent coroutine is cancelled, what happens to its child coroutines?
AThey continue running independently
BThey are also cancelled automatically
CThey pause until the parent restarts
DThey throw an exception
✗ Incorrect
In structured concurrency, cancelling a parent coroutine automatically cancels all its child coroutines.
What is the role of CoroutineScope in Kotlin?
AIt schedules coroutines to run sequentially
BIt defines the thread where coroutines run
CIt converts coroutines to threads
DIt manages the lifecycle of coroutines
✗ Incorrect
CoroutineScope manages the lifecycle of coroutines, controlling their start and cancellation.
Which of these is NOT a benefit of structured concurrency?
APrevents memory leaks
BEnsures coroutines complete in order
CAllows orphan coroutines to run freely
DSimplifies error handling
✗ Incorrect
Structured concurrency prevents orphan coroutines; allowing them to run freely is not a benefit.
Describe how CoroutineScope helps manage coroutines in Kotlin.
Think about how you keep track of tasks in real life to avoid forgetting them.
You got /4 concepts.
Explain the concept of structured concurrency and why it is important.
Imagine a team where everyone finishes their work before the project ends.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a CoroutineScope in Kotlin?
easy
A. To block the main thread until coroutines finish
B. To create threads manually for parallel execution
C. To group coroutines and manage their lifecycle together
D. To convert coroutines into synchronous functions
Solution
Step 1: Understand CoroutineScope role
A CoroutineScope groups coroutines so they can be managed as one unit.
Step 2: Compare other options
The other options describe unrelated or incorrect behaviors for CoroutineScope.
Final Answer:
To group coroutines and manage their lifecycle together -> Option C
Quick Check:
CoroutineScope groups coroutines [OK]
Hint: CoroutineScope manages coroutines lifecycle as a group [OK]
Common Mistakes:
Thinking CoroutineScope creates threads
Confusing CoroutineScope with blocking calls
Assuming CoroutineScope converts coroutines to sync
2. Which of the following is the correct way to launch a coroutine within a CoroutineScope?
easy
A. scope.launch { /* coroutine code */ }
B. launch.scope { /* coroutine code */ }
C. CoroutineScope.launch { /* coroutine code */ }
D. scope.run { /* coroutine code */ }
Solution
Step 1: Recall coroutine launch syntax
Within a CoroutineScope, you call launch as a function on the scope instance.
Step 2: Check options for syntax correctness
scope.launch { /* coroutine code */ } uses scope.launch {} which is correct. Others misuse syntax or functions.
Final Answer:
scope.launch { /* coroutine code */ } -> Option A
Quick Check:
launch is called on CoroutineScope instance [OK]
Hint: Use scope.launch { } to start coroutine inside scope [OK]
Common Mistakes:
Swapping order like launch.scope
Using run instead of launch
Calling launch as a static method
3. What will be the output of this Kotlin code snippet?
import kotlinx.coroutines.*
fun main() = runBlocking {
val job = launch {
delay(100)
println("Task done")
}
println("Waiting")
job.join()
println("Finished")
}
medium
A. Task done\nWaiting\nFinished
B. Waiting\nTask done\nFinished
C. Waiting\nFinished\nTask done
D. Finished\nWaiting\nTask done
Solution
Step 1: Analyze coroutine launch and delay
The coroutine launched delays 100ms before printing "Task done". Meanwhile, "Waiting" prints immediately.
Step 2: Understand job.join() effect
The main coroutine waits for the launched job to finish before printing "Finished".
Final Answer:
Waiting\nTask done\nFinished -> Option B
Quick Check:
Output order respects delay and join [OK]
Hint: join() waits for coroutine to finish before continuing [OK]
Common Mistakes:
Assuming launched coroutine runs instantly
Ignoring delay effect
Printing Finished before job completes
4. Identify the problem in this Kotlin coroutine code and choose the correct fix:
fun main() = runBlocking {
val job = GlobalScope.launch {
delay(1000)
println("Done")
}
println("Waiting")
}
medium
A. Replace delay(1000) with Thread.sleep(1000)
B. Use CoroutineScope().launch {} instead of GlobalScope.launch
C. Remove runBlocking and use GlobalScope.launch only
D. Add job.join() after println("Waiting")
Solution
Step 1: Identify lifecycle issue with GlobalScope
Using GlobalScope.launch starts coroutine that may outlive runBlocking, so "Done" may never print.
Step 2: Fix by waiting for job completion
Calling job.join() ensures main coroutine waits for launched coroutine to finish before exiting.
Final Answer:
Add job.join() after println("Waiting") -> Option D
Quick Check:
join() waits for coroutine to complete [OK]
Hint: Always join launched jobs to wait for completion [OK]
Common Mistakes:
Ignoring job.join() causing premature exit
Replacing delay with blocking sleep
Removing runBlocking causing main to exit early
5. Given this Kotlin code using structured concurrency, what will be printed?