0
0
Android Kotlinmobile~20 mins

Coroutines for async networking in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Coroutine Networking Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What happens when you launch a coroutine on the Main dispatcher?
Consider this Kotlin coroutine code snippet in an Android app:
GlobalScope.launch(Dispatchers.Main) {
  val data = fetchDataFromNetwork()
  updateUI(data)
}

What is the expected behavior of this code?
Android Kotlin
GlobalScope.launch(Dispatchers.Main) {
  val data = fetchDataFromNetwork()
  updateUI(data)
}
AThe network call runs asynchronously on a background thread, then updates the UI on the main thread.
BThe network call runs on the main thread, freezing the UI until it finishes.
CThe coroutine throws an exception because network calls are not allowed on the main thread.
DThe updateUI function runs on a background thread, causing UI errors.
Attempts:
2 left
💡 Hint
Remember that Dispatchers.Main runs coroutine code on the UI thread but suspend functions can switch threads internally.
lifecycle
intermediate
2:00remaining
What is the risk of using GlobalScope for network calls in Android?
In Android, what problem can occur if you launch network coroutines using GlobalScope.launch without tying them to a lifecycle?
AThe coroutine may continue running after the activity is destroyed, causing memory leaks or crashes.
BThe coroutine will automatically cancel when the activity is destroyed.
CGlobalScope does not support network calls, so the coroutine will fail immediately.
DThe coroutine will run on the main thread and block the UI.
Attempts:
2 left
💡 Hint
Think about what happens when the user leaves the screen while a coroutine is still running.
📝 Syntax
advanced
2:00remaining
Which code snippet correctly performs a network call asynchronously using coroutines?
Choose the Kotlin code that correctly fetches data from a network on a background thread and updates the UI on the main thread.
A
launch {
  val data = withContext(Dispatchers.Main) { fetchData() }
  updateUI(data)
}
B
launch(Dispatchers.Main) {
  val data = fetchData()
  updateUI(data)
}
C
launch(Dispatchers.IO) {
  val data = fetchData()
  withContext(Dispatchers.Main) {
    updateUI(data)
  }
}
D
launch(Dispatchers.IO) {
  val data = fetchData()
  updateUI(data)
}
Attempts:
2 left
💡 Hint
Network calls should run on IO dispatcher, UI updates on Main dispatcher.
🔧 Debug
advanced
2:00remaining
Why does this coroutine code cause a crash when updating UI?
Given this code:
GlobalScope.launch(Dispatchers.IO) {
  val data = fetchData()
  updateUI(data)
}

Why does the app crash when updateUI is called?
Android Kotlin
GlobalScope.launch(Dispatchers.IO) {
  val data = fetchData()
  updateUI(data)
}
AfetchData() is not a suspend function, causing a runtime error.
BupdateUI is called from a background thread, which is not allowed for UI updates.
CGlobalScope does not support Dispatchers.IO, causing a crash.
DThe coroutine is not launched, so updateUI is never called.
Attempts:
2 left
💡 Hint
UI updates must happen on the main thread.
🧠 Conceptual
expert
2:00remaining
What is the main advantage of using suspend functions with coroutines for networking?
Why do suspend functions combined with coroutines improve networking code in Android apps?
AThey run all network calls on the main thread for faster UI updates.
BThey automatically retry failed network calls without extra code.
CThey block the main thread until the network call finishes, ensuring data consistency.
DThey allow writing asynchronous code in a sequential style without blocking threads.
Attempts:
2 left
💡 Hint
Think about how suspend functions let you write code that looks simple but runs asynchronously.