Bird
Raised Fist0
Kotlinprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the async function do in Kotlin coroutines?
easy
A. Starts a background task that runs concurrently
B. Waits for a task to complete and returns its result
C. Blocks the main thread until a task finishes
D. Cancels a running coroutine immediately

Solution

  1. Step 1: Understand the role of async

    async starts a coroutine that runs in the background without blocking the main thread.
  2. Step 2: Differentiate from await

    await waits for the result of the background task started by async, but async itself only starts the task.
  3. Final Answer:

    Starts a background task that runs concurrently -> Option A
  4. Quick Check:

    async = starts background task [OK]
Hint: Async starts tasks without waiting, await waits for results [OK]
Common Mistakes:
  • Confusing async with await
  • Thinking async blocks the main thread
  • Assuming async cancels tasks
2. Which of the following is the correct syntax to start two concurrent tasks and wait for both results in Kotlin?
easy
A. val result1 = async { task1() }.await() val result2 = async { task2() }.await()
B. val result1 = await(async { task1() }) val result2 = await(async { task2() })
C. val deferred1 = async { task1() } val deferred2 = async { task2() } val result1 = deferred1.await() val result2 = deferred2.await()
D. val result1 = task1() val result2 = task2()

Solution

  1. Step 1: Identify correct async-await usage

    In Kotlin, async returns a Deferred object, which you then call await() on to get the result.
  2. Step 2: Check syntax correctness

    val deferred1 = async { task1() } val deferred2 = async { task2() } val result1 = deferred1.await() val result2 = deferred2.await() correctly starts two async tasks and awaits their results separately, allowing concurrency.
  3. Final Answer:

    val deferred1 = async { task1() } val deferred2 = async { task2() } val result1 = deferred1.await() val result2 = deferred2.await() -> Option C
  4. Quick Check:

    async returns Deferred, await gets result [OK]
Hint: Store async in Deferred, then call await separately [OK]
Common Mistakes:
  • Calling await immediately after async (no concurrency)
  • Using await as a standalone function
  • Not storing async result before await
3. What will be the output of the following Kotlin code?
import kotlinx.coroutines.*

fun main() = runBlocking {
    val deferred1 = async {
        delay(100)
        10
    }
    val deferred2 = async {
        delay(50)
        20
    }
    println(deferred1.await() + deferred2.await())
}
medium
A. 10
B. 30
C. 20
D. Compilation error

Solution

  1. Step 1: Understand async and delay behavior

    Two async tasks run concurrently: one delays 100ms and returns 10, the other delays 50ms and returns 20.
  2. Step 2: Await both results and sum

    Awaiting both deferreds gets 10 and 20, summing to 30, printed as output.
  3. Final Answer:

    30 -> Option B
  4. Quick Check:

    10 + 20 = 30 [OK]
Hint: Sum awaited results after async tasks complete [OK]
Common Mistakes:
  • Assuming sequential execution ignoring concurrency
  • Confusing delay times with output values
  • Expecting compilation errors due to coroutines
4. Identify the error in this Kotlin coroutine code snippet:
fun main() = runBlocking {
    val result = async {
        delay(100)
        42
    }.await()
    println(result)
}
medium
A. Calling await() immediately after async() blocks concurrency
B. Missing coroutine scope for async
C. delay() cannot be used inside async
D. runBlocking cannot be used in main function

Solution

  1. Step 1: Analyze async and await usage

    Calling await() immediately after async waits for the task to finish before continuing, so concurrency is lost.
  2. Step 2: Understand concurrency impact

    This pattern runs the async task sequentially, blocking until completion, defeating the purpose of async concurrency.
  3. Final Answer:

    Calling await() immediately after async() blocks concurrency -> Option A
  4. Quick Check:

    Immediate await blocks concurrency [OK]
Hint: Don't await right after async; store Deferred first [OK]
Common Mistakes:
  • Thinking immediate await keeps concurrency
  • Believing delay is disallowed inside async
  • Assuming runBlocking can't be main
5. You want to fetch data from two APIs concurrently and combine their results in Kotlin. Which approach best uses async and await for this task?
hard
A. Call the first API synchronously, then start async for the second API and await it
B. Run both API calls sequentially inside async blocks without await
C. Use await without async for both API calls to simplify code
D. Start two async coroutines for each API call, then await both results before combining

Solution

  1. Step 1: Understand concurrency for API calls

    Starting both API calls concurrently with async allows them to run at the same time, saving time.
  2. Step 2: Await both results before combining

    Using await on both deferred results ensures you get both responses before combining them safely.
  3. Final Answer:

    Start two async coroutines for each API call, then await both results before combining -> Option D
  4. Quick Check:

    Async start + await both = concurrent fetch [OK]
Hint: Async both calls, await both results, then combine [OK]
Common Mistakes:
  • Running API calls sequentially losing concurrency
  • Using await without async causing errors
  • Not awaiting results before combining
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard