0
0
Kotlinprogramming~5 mins

Launch coroutine builder in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AA Job
BA Deferred
CA Thread
DA Future
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
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>
Which coroutine builder should you use if you want a result from the coroutine?
Alaunch
Basync
CrunBlocking
Ddelay
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
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.