0
0
Kotlinprogramming~20 mins

Async and await for concurrent results in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Async Await 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 Kotlin coroutine code?

Consider this Kotlin code using async and await to run two tasks concurrently. What will be printed?

Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    val deferred1 = async {
        delay(100)
        "Hello"
    }
    val deferred2 = async {
        delay(50)
        "World"
    }
    println(deferred1.await() + " " + deferred2.await())
}
AHelloHello
BWorld Hello
CHello World
DWorldWorld
Attempts:
2 left
💡 Hint

Remember that async starts both tasks immediately, but await waits for the result.

Predict Output
intermediate
2:00remaining
What is the output when awaiting multiple async tasks?

What will this Kotlin program print?

Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    val deferreds = (1..3).map { n ->
        async {
            delay(10L * n)
            n * n
        }
    }
    val results = deferreds.awaitAll()
    println(results.sum())
}
A9
B0
C6
D14
Attempts:
2 left
💡 Hint

Think about the squares of 1, 2, and 3 and their sum.

🔧 Debug
advanced
2:00remaining
Which option causes a runtime exception in this coroutine code?

Given this Kotlin coroutine snippet, which option will cause a runtime exception when run?

Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    val deferred = async {
        throw IllegalStateException("Error in coroutine")
    }
    try {
        deferred.await()
    } catch (e: Exception) {
        println("Caught: ${e.message}")
    }
}
ARemove the try-catch block around deferred.await()
BCall deferred.cancel() before await
CAdd a delay before deferred.await()
DReplace async with launch
Attempts:
2 left
💡 Hint

Think about what happens if exceptions inside async are not caught.

🧠 Conceptual
advanced
2:00remaining
What is the main benefit of using async and await in Kotlin coroutines?

Choose the best explanation for why async and await are used in Kotlin coroutines.

AThey automatically parallelize code on multiple CPU cores without programmer control.
BThey allow running multiple tasks concurrently and waiting for their results without blocking the main thread.
CThey replace all uses of threads and locks in Kotlin programs.
DThey guarantee tasks run in a specific order sequentially.
Attempts:
2 left
💡 Hint

Think about concurrency and non-blocking waits.

📝 Syntax
expert
2:00remaining
Which option correctly uses async and await to fetch two results concurrently?

Which Kotlin code snippet correctly fetches two values concurrently using async and await and prints their sum?

A
import kotlinx.coroutines.*

fun main() = runBlocking {
    val deferred1 = async { 10 }
    val deferred2 = async { 20 }
    println(deferred1.await() + deferred2.await())
}
B
val result = async { 10 } + async { 20 }
println(result)
C
val deferred1 = async { 10 }
val deferred2 = async { 20 }
println(deferred1 + deferred2)
D
val result1 = async { 10 }.await()
val result2 = async { 20 }.await()
println(result1 + result2)
Attempts:
2 left
💡 Hint

Remember that async returns a Deferred, and you must call await() to get the value.