Challenge - 5 Problems
Coroutine Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2: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"
}Attempts:
2 left
💡 Hint
launch starts a coroutine that runs asynchronously and can delay without blocking the main thread.
✗ Incorrect
The launch builder starts a coroutine that runs asynchronously. The delay suspends the coroutine for 1 second without blocking the UI thread. After the delay, the textView text is updated.
🧠 Conceptual
intermediate2:00remaining
Difference between launch and async builders?
Which statement correctly describes the difference between launch and async coroutine builders in Kotlin?
Attempts:
2 left
💡 Hint
Think about which builder lets you get a result back.
✗ Incorrect
launch starts a coroutine and returns a Job, which represents the coroutine but does not produce a result. async starts a coroutine and returns a Deferred, which is a future result you can await.
📝 Syntax
advanced2: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)
Attempts:
2 left
💡 Hint
await waits for the async coroutine to finish and returns its result.
✗ Incorrect
The async builder returns a Deferred that produces 42 after 500ms delay. runBlocking waits for deferred.await() to complete and returns 42, which is printed.
❓ lifecycle
advanced2: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?
Attempts:
2 left
💡 Hint
lifecycleScope is tied to the lifecycle and cancels coroutines when destroyed.
✗ Incorrect
lifecycleScope is cancelled when the lifecycle owner is destroyed. Launching a coroutine after onDestroy means the scope is cancelled, so the coroutine is immediately cancelled and does not run.
🔧 Debug
expert2: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)
Attempts:
2 left
💡 Hint
Consider what happens when you block the main thread waiting for a coroutine that also runs on the main thread.
✗ Incorrect
Calling deferred.await() on the main thread blocks it. Since lifecycleScope.async runs on the main thread by default, the coroutine cannot resume to complete, causing a deadlock.