Challenge - 5 Problems
Coroutine Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediateWhy are coroutines preferred over callbacks for async tasks?
Which of the following best explains why coroutines simplify asynchronous programming compared to traditional callbacks?
Attempts:
2 left
💡 Hint
Think about how code looks and feels when using coroutines versus callbacks.
✗ Incorrect
Coroutines let you write async code that looks like normal sequential code, avoiding nested callbacks and making it easier to understand and maintain.
❓ ui_behavior
intermediateEffect of coroutine on UI responsiveness
What happens to the UI when a long-running task is launched inside a coroutine with Dispatchers.IO in Android?
Attempts:
2 left
💡 Hint
Consider which thread Dispatchers.IO uses.
✗ Incorrect
Dispatchers.IO runs coroutines on a background thread pool, so the UI thread is free and remains responsive during long tasks.
❓ lifecycle
advancedCoroutine cancellation and Android lifecycle
What is the correct behavior when a coroutine launched in a ViewModelScope is cancelled due to ViewModel clearing?
Attempts:
2 left
💡 Hint
Think about how ViewModelScope manages coroutines with lifecycle.
✗ Incorrect
ViewModelScope cancels all its coroutines when the ViewModel is cleared, preventing work from continuing unnecessarily and avoiding leaks.
advanced
Handling coroutine results for navigation
In an Android app, which coroutine approach correctly waits for a network call result before navigating to the next screen?
Android Kotlin
fun onButtonClick() {
lifecycleScope.launch {
val data = fetchData() // suspend function
navigateToNextScreen(data)
}
}Attempts:
2 left
💡 Hint
Consider when the navigation happens relative to the suspend function completion.
✗ Incorrect
Launching a coroutine and awaiting fetchData ensures navigation happens only after data is ready, preventing errors or blank screens.
📝 Syntax
expertCorrect syntax for launching a coroutine with exception handling
Which Kotlin coroutine code snippet correctly launches a coroutine and catches exceptions without blocking the main thread?
Android Kotlin
val handler = CoroutineExceptionHandler { _, exception -> println("Error: $exception") }
lifecycleScope.launch(handler) {
// some suspend function call
}Attempts:
2 left
💡 Hint
Look for correct placement of CoroutineExceptionHandler and syntax.
✗ Incorrect
Option B correctly attaches CoroutineExceptionHandler to the coroutine builder and calls the suspend function inside the coroutine block.
