Consider this Kotlin code using launch to start a coroutine. What will it print?
import kotlinx.coroutines.* fun main() = runBlocking { launch { delay(100L) println("World!") } println("Hello,") }
Remember that launch starts a new coroutine concurrently, and runBlocking waits for all child coroutines.
The launch coroutine starts and delays for 100ms before printing "World!". Meanwhile, the main coroutine prints "Hello," immediately. Because runBlocking waits for the launched coroutine to finish, both lines print in order: "Hello," then "World!".
launch coroutine builder is true?Choose the correct statement about the launch coroutine builder in Kotlin.
Think about what launch returns and how it behaves with threads.
launch starts a coroutine and immediately returns a Job object. It does not block the current thread. The coroutine runs concurrently until it completes or is cancelled.
Examine this Kotlin code snippet. What error will it raise when run?
import kotlinx.coroutines.* fun main() { launch { println("Hello from coroutine") } }
Consider where launch can be called and what context is required.
The launch coroutine builder must be called from a coroutine scope or suspend function. Calling it directly in main without a scope causes a compilation error: "Unresolved reference: launch".
Choose the correct Kotlin code snippet that launches a coroutine to print "Done" inside runBlocking.
Check the syntax of launch and its lambda parameter.
Option D correctly calls launch with a lambda block inside runBlocking. Options B and C have syntax errors due to misplaced parentheses or braces. Option D misses the lambda block for launch.
Given this Kotlin code, how many coroutines run concurrently?
import kotlinx.coroutines.* fun main() = runBlocking { repeat(3) { launch { delay(100L * it) println("Coroutine $it done") } } }
Think about how launch and repeat work inside runBlocking.
The repeat(3) launches 3 separate coroutines concurrently. Each coroutine delays for a different time but all run concurrently inside runBlocking.