What is the purpose of the launch coroutine builder in Kotlin?
The launch coroutine builder starts a new coroutine without blocking the current thread. It runs concurrently and returns a Job that can be used to manage the coroutine's lifecycle.
Click to reveal answer
intermediate
How does launch differ from async in Kotlin coroutines?
launch starts a coroutine that does not return a result and returns a Job. async starts a coroutine that returns a result wrapped in a Deferred, which can be awaited.
Click to reveal answer
beginner
What does the Job returned by launch allow you to do?
The Job lets you cancel the coroutine, check if it is active or completed, and join to wait for its completion.
Click to reveal answer
beginner
Write a simple Kotlin code snippet using launch to print "Hello from coroutine!".
import kotlinx.coroutines.*
fun main() = runBlocking {
launch {
println("Hello from coroutine!")
}
}
Click to reveal answer
intermediate
What happens if you call launch inside runBlocking?
The launch coroutine starts concurrently inside the runBlocking scope. The runBlocking waits for all its child coroutines, including the launched one, to complete before finishing.
Click to reveal answer
What does the launch coroutine builder return?
AA Job
BA Deferred
CA Thread
DA Future
✗ Incorrect
launch returns a Job which represents the coroutine's lifecycle.
Which of the following is true about launch?
AIt blocks the current thread until completion
BIt returns a result directly
CIt can only be used in main thread
DIt starts a coroutine that runs concurrently
✗ Incorrect
launch starts a coroutine that runs concurrently without blocking the current thread.
How can you cancel a coroutine started with launch?
ABy interrupting the thread
BBy calling <code>stop()</code> on the coroutine
CBy calling <code>cancel()</code> on the Job returned by <code>launch</code>
DBy calling <code>await()</code>
✗ Incorrect
The Job returned by launch has a cancel() method to stop the coroutine.
Which coroutine builder should you use if you want a result from the coroutine?
Alaunch
Basync
CrunBlocking
Ddelay
✗ Incorrect
async returns a Deferred which holds the result of the coroutine.
What is the typical use case for launch?
ATo perform background work without returning a result
BTo compute a value asynchronously
CTo block the main thread
DTo create a new thread
✗ Incorrect
launch is used for fire-and-forget background tasks that do not return a value.
Explain how the launch coroutine builder works and when you would use it.
Think about starting a task that runs alongside your main program.
You got /4 concepts.
Describe the difference between launch and async coroutine builders.
One is for fire-and-forget, the other for getting a value.
You got /5 concepts.
Practice
(1/5)
1. What does the launch coroutine builder do in Kotlin?
easy
A. Starts a new coroutine that runs independently without blocking the current thread.
B. Blocks the current thread until the coroutine completes.
C. Returns a value from the coroutine immediately.
D. Creates a new thread for each coroutine.
Solution
Step 1: Understand the purpose of launch
The launch builder starts a coroutine that runs concurrently without blocking the current thread.
Step 2: Compare with other coroutine builders
Unlike async, launch does not return a result and does not block the thread.
Final Answer:
Starts a new coroutine that runs independently without blocking the current thread. -> Option A
Quick Check:
launch = independent coroutine start [OK]
Hint: Remember: launch starts, doesn't block or return value [OK]
Common Mistakes:
Thinking launch blocks the thread
Expecting launch to return a value
Confusing launch with thread creation
2. Which of the following is the correct syntax to launch a coroutine in Kotlin?
easy
A. launch: { println("Hello") }
B. launch { println("Hello") }
C. launch -> println("Hello")
D. launch() println("Hello")
Solution
Step 1: Recall the syntax of launch
The launch builder requires a lambda block enclosed in curly braces.
Step 2: Check each option
launch { println("Hello") } correctly uses launch { ... }. Others have syntax errors or wrong symbols.
Because of cancellation, the launched coroutine does not complete, so "Done" is not printed, but "Cancelled" is printed after cancel.
Final Answer:
Calling cancel() cancels the runBlocking scope immediately, so "Done" is never printed. -> Option D
Quick Check:
cancel() stops coroutines immediately = Calling cancel() cancels the runBlocking scope immediately, so "Done" is never printed. [OK]
Hint: cancel() stops all coroutines in the scope immediately [OK]
Common Mistakes:
Thinking delay() is invalid in launch
Expecting launch to return a value
Assuming println after cancel won't run
5. You want to launch multiple coroutines that print numbers 1 to 3 with a delay, but ensure the main program waits for all to finish. Which code correctly achieves this?
hard
A. runBlocking {
repeat(3) { i ->
launch {
delay(100L * i)
println(i + 1)
}
}
}
B. runBlocking {
repeat(3) { i ->
launch {
delay(100L * i)
println(i + 1)
}
}
println("Done")
}
C. repeat(3) { i ->
launch {
delay(100L * i)
println(i + 1)
}
}
println("Done")
D. runBlocking {
repeat(3) { i ->
launch {
delay(100L * i)
println(i + 1)
}
}
delay(400)
}
Solution
Step 1: Ensure main waits for coroutines
Using runBlocking with launch inside ensures the main thread waits for all launched coroutines to complete.
Step 2: Check each option's behavior
runBlocking {
repeat(3) { i ->
launch {
delay(100L * i)
println(i + 1)
}
}
} launches coroutines inside runBlocking, so main waits. runBlocking {
repeat(3) { i ->
launch {
delay(100L * i)
println(i + 1)
}
}
println("Done")
} prints "Done" immediately without waiting. repeat(3) { i ->
launch {
delay(100L * i)
println(i + 1)
}
}
println("Done") launches outside runBlocking, so main exits early. runBlocking {
repeat(3) { i ->
launch {
delay(100L * i)
println(i + 1)
}
}
delay(400)
} uses delay but is less reliable than waiting for coroutines.
Final Answer:
runBlocking with launch inside to wait for all coroutines. -> Option A
Quick Check:
runBlocking + launch inside = wait for all [OK]
Hint: Use runBlocking to wait for all launched coroutines [OK]
Common Mistakes:
Launching coroutines outside runBlocking
Printing before coroutines finish
Using delay to wait instead of structured concurrency
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.