0
0
Android Kotlinmobile~20 mins

launch and async builders in Android Kotlin - Practice Problems & Coding Challenges

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!
ui_behavior
intermediate
2:00remaining
What happens when using launch in a CoroutineScope?
Consider this Kotlin code snippet inside an Android activity:
lifecycleScope.launch {
  delay(1000)
  textView.text = "Hello"
}
What will the user see immediately after this code runs?
Android Kotlin
lifecycleScope.launch {
  delay(1000)
  textView.text = "Hello"
}
AThe textView updates to "Hello" after 1 second delay.
BThe app crashes because delay cannot be used here.
CThe textView updates immediately without delay.
DThe textView never updates because launch does not run.
Attempts:
2 left
💡 Hint
launch starts a coroutine that runs asynchronously and can delay without blocking the main thread.
🧠 Conceptual
intermediate
2:00remaining
Difference between launch and async builders?
Which statement correctly describes the difference between launch and async coroutine builders in Kotlin?
Alaunch can only be used in background threads; async only on main thread.
Blaunch returns a Job and does not produce a result; async returns a Deferred and produces a result.
Claunch blocks the thread; async runs asynchronously without blocking.
Dlaunch returns a Deferred; async returns a Job.
Attempts:
2 left
💡 Hint
Think about which builder lets you get a result back.
📝 Syntax
advanced
2:00remaining
What is the output of this async coroutine code?
Given this Kotlin code snippet:
val deferred = lifecycleScope.async {
  delay(500)
  42
}

val result = runBlocking {
  deferred.await()
}
println(result)
What will be printed?
Android Kotlin
val deferred = lifecycleScope.async {
  delay(500)
  42
}

val result = runBlocking {
  deferred.await()
}
println(result)
AThe code never prints because of deadlock.
B0
CThe code throws an exception because await is called outside coroutine.
D42
Attempts:
2 left
💡 Hint
await waits for the async coroutine to finish and returns its result.
lifecycle
advanced
2:00remaining
What happens if you launch a coroutine in lifecycleScope after onDestroy?
In an Android activity, what happens if you call lifecycleScope.launch { /* some code */ } after the onDestroy() method has been called?
AThe coroutine is immediately cancelled and does not run.
BThe coroutine runs normally even after onDestroy.
CThe app crashes with IllegalStateException.
DThe coroutine runs but leaks memory.
Attempts:
2 left
💡 Hint
lifecycleScope is tied to the lifecycle and cancels coroutines when destroyed.
🔧 Debug
expert
2:00remaining
Why does this async coroutine code cause a deadlock?
Analyze this Kotlin code snippet:
val deferred = lifecycleScope.async {
  delay(1000)
  10
}

val result = deferred.await()
println(result)
Why might this code cause the app to freeze or deadlock?
Android Kotlin
val deferred = lifecycleScope.async {
  delay(1000)
  10
}

val result = deferred.await()
println(result)
ABecause lifecycleScope.async returns null causing NullPointerException.
BBecause delay cannot be used inside async builder.
CBecause await is called on the main thread blocking it, preventing async from completing.
DBecause println blocks the coroutine indefinitely.
Attempts:
2 left
💡 Hint
Consider what happens when you block the main thread waiting for a coroutine that also runs on the main thread.