0
0
Kotlinprogramming~20 mins

Launch coroutine builder in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Coroutine Launch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this coroutine launch code?

Consider this Kotlin code using launch to start a coroutine. What will it print?

Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    launch {
        delay(100L)
        println("World!")
    }
    println("Hello,")
}
AHello,\nWorld!
BWorld!\nHello,
CHello,
DWorld!
Attempts:
2 left
💡 Hint

Remember that launch starts a new coroutine concurrently, and runBlocking waits for all child coroutines.

🧠 Conceptual
intermediate
1:30remaining
Which statement about launch coroutine builder is true?

Choose the correct statement about the launch coroutine builder in Kotlin.

A<code>launch</code> returns a <code>Deferred</code> and blocks until the coroutine completes.
B<code>launch</code> returns a <code>Job</code> and does not block the current thread.
C<code>launch</code> always blocks the current thread until completion.
D<code>launch</code> returns the result of the coroutine directly.
Attempts:
2 left
💡 Hint

Think about what launch returns and how it behaves with threads.

🔧 Debug
advanced
2:00remaining
What error does this coroutine launch code produce?

Examine this Kotlin code snippet. What error will it raise when run?

Kotlin
import kotlinx.coroutines.*

fun main() {
    launch {
        println("Hello from coroutine")
    }
}
ACompilation error: missing suspend modifier
BIllegalStateException at runtime
CUnresolved reference: launch
DNo error, prints "Hello from coroutine"
Attempts:
2 left
💡 Hint

Consider where launch can be called and what context is required.

📝 Syntax
advanced
1:30remaining
Which option correctly launches a coroutine that prints "Done"?

Choose the correct Kotlin code snippet that launches a coroutine to print "Done" inside runBlocking.

A
runBlocking {
    launch
    println("Done")
}
B
runBlocking {
    launch() {
        println("Done")
    }
}
C
runBlocking {
    launch
    {
        println("Done")
    }
}
D
runBlocking {
    launch {
        println("Done")
    }
}
Attempts:
2 left
💡 Hint

Check the syntax of launch and its lambda parameter.

🚀 Application
expert
2:30remaining
How many coroutines run concurrently in this code?

Given this Kotlin code, how many coroutines run concurrently?

Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    repeat(3) {
        launch {
            delay(100L * it)
            println("Coroutine $it done")
        }
    }
}
A3 coroutines run concurrently
BOnly 1 coroutine runs at a time sequentially
C2 coroutines run concurrently
DNo coroutines run concurrently
Attempts:
2 left
💡 Hint

Think about how launch and repeat work inside runBlocking.