0
0
Kotlinprogramming~20 mins

Why coroutines matter for async programming in Kotlin - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Coroutine Mastery Badge
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 example?

Consider this Kotlin coroutine code snippet. What will it print?

Kotlin
import kotlinx.coroutines.*

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

Remember that launch starts a coroutine that runs concurrently, and delay suspends it without blocking the main thread.

🧠 Conceptual
intermediate
2:00remaining
Why are coroutines preferred over threads for async tasks?

Which reason best explains why coroutines are preferred over traditional threads for asynchronous programming in Kotlin?

ACoroutines run on separate CPU cores automatically, unlike threads.
BCoroutines do not require any scheduler to run, unlike threads.
CCoroutines can only run synchronously, making them simpler than threads.
DCoroutines are lightweight and use less memory than threads, allowing many to run concurrently without heavy resource use.
Attempts:
2 left
💡 Hint

Think about resource use and how many tasks you can run at once.

🔧 Debug
advanced
2:00remaining
Identify the runtime error in this coroutine code

What runtime error will this Kotlin coroutine code produce?

Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    val job = launch {
        delay(1000L)
        println("Task finished")
    }
    job.cancel()
    println("Job cancelled")
}
ACancellationException thrown and program crashes
BNo error; output is 'Job cancelled' only
CDeadlock occurs and program hangs
DCompilation error due to missing try-catch
Attempts:
2 left
💡 Hint

Consider what happens when you cancel a coroutine job before it finishes.

📝 Syntax
advanced
2:00remaining
Which option correctly launches a coroutine with a delay?

Which Kotlin code snippet correctly launches a coroutine that waits 500ms and then prints "Done"?

A
launch {
    delay(500)
    println("Done")
}
B
launch {
    Thread.sleep(500L)
    println("Done")
}
C
launch {
    delay(500L)
    println("Done")
}
D
launch {
    delay(500L)
    print("Done")
}
Attempts:
2 left
💡 Hint

Check the delay parameter type and the print function used.

🚀 Application
expert
2:00remaining
How many coroutines run concurrently in this example?

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 because of delay
Attempts:
2 left
💡 Hint

Think about how launch and repeat work together.