0
0
Kotlinprogramming~20 mins

Async coroutine builder in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Async Coroutine 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 coroutine builder. What will it print?

Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    val deferred = async {
        delay(100)
        "Hello"
    }
    println("Start")
    println(deferred.await())
    println("End")
}
AStart\nHello\nEnd
BHello\nStart\nEnd
CStart\nEnd\nHello
DStart\nHello
Attempts:
2 left
💡 Hint

Remember that async starts a coroutine immediately and await() waits for its result.

Predict Output
intermediate
2:00remaining
What is the output when using async with different CoroutineScopes?

What will this Kotlin program print?

Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    val scope = CoroutineScope(Dispatchers.Default)
    val deferred = scope.async {
        delay(50)
        42
    }
    println("Result: ${deferred.await()}")
}
ACompilation error
BResult: 42
CResult: 0
DRuntime exception
Attempts:
2 left
💡 Hint

async returns a Deferred that you can await() to get the result.

Predict Output
advanced
2:00remaining
What happens if you call async without awaiting?

What will this Kotlin program print?

Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    val deferred = async {
        delay(100)
        "Done"
    }
    println("Before delay")
    delay(200)
    println("After delay")
}
ACompilation error
BBefore delay\nDone\nAfter delay
CBefore delay\nAfter delay
DRuntime exception
Attempts:
2 left
💡 Hint

If you don't call await(), the result is not printed.

Predict Output
advanced
2:00remaining
What is the output of nested async coroutines?

What will this Kotlin code print?

Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    val deferred = async {
        val inner = async {
            delay(50)
            10
        }
        inner.await() * 2
    }
    println(deferred.await())
}
A20
B10
CCompilation error
DRuntime exception
Attempts:
2 left
💡 Hint

Inner async returns 10 after delay, outer async multiplies by 2.

🧠 Conceptual
expert
2:00remaining
Which option correctly describes the behavior of async coroutine builder?

Choose the statement that best describes the async coroutine builder in Kotlin.

A<code>async</code> creates a coroutine that cannot be cancelled once started.
B<code>async</code> blocks the current thread until the coroutine finishes and returns the result directly.
C<code>async</code> launches a coroutine that runs only after all other coroutines complete.
D<code>async</code> starts a coroutine and immediately returns a Deferred that represents a future result, which must be awaited to get the value.
Attempts:
2 left
💡 Hint

Think about how async returns a Deferred and how await() works.