Recall & Review
beginner
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?✗ Incorrect
launch returns a Job which represents the coroutine's lifecycle.Which of the following is true about
launch?✗ Incorrect
launch starts a coroutine that runs concurrently without blocking the current thread.How can you cancel a coroutine started with
launch?✗ 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?
✗ Incorrect
async returns a Deferred which holds the result of the coroutine.What is the typical use case for
launch?✗ 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.