Bird
Raised Fist0
Android Kotlinmobile~20 mins

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

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

Practice

(1/5)
1. What is the main benefit of using coroutines for network calls in Android apps?
easy
A. They replace the need for any background threads.
B. They make the app run faster by using more CPU cores.
C. They automatically cache network data for offline use.
D. They prevent the app's screen from freezing during network tasks.

Solution

  1. Step 1: Understand what happens during network calls

    Network calls can take time and if done on the main thread, they freeze the UI.
  2. Step 2: Role of coroutines in network calls

    Coroutines run network tasks in the background, keeping the UI responsive.
  3. Final Answer:

    They prevent the app's screen from freezing during network tasks. -> Option D
  4. Quick Check:

    Coroutines keep UI responsive = D [OK]
Hint: Coroutines keep UI smooth by running tasks in background [OK]
Common Mistakes:
  • Thinking coroutines speed up CPU usage
  • Believing coroutines cache data automatically
  • Assuming coroutines remove all threading needs
2. Which of the following is the correct way to start a coroutine for a network call on a background thread?
easy
A. launch(Dispatchers.Main) { fetchData() }
B. launch(Dispatchers.Default) { updateUI() }
C. launch(Dispatchers.IO) { fetchData() }
D. launch { fetchData() }

Solution

  1. Step 1: Identify dispatcher for background network tasks

    Dispatchers.IO is designed for blocking IO tasks like network calls.
  2. Step 2: Match correct coroutine launch syntax

    launch(Dispatchers.IO) { fetchData() } runs fetchData() on IO thread pool.
  3. Final Answer:

    launch(Dispatchers.IO) { fetchData() } -> Option C
  4. Quick Check:

    Network calls use Dispatchers.IO = B [OK]
Hint: Use Dispatchers.IO for network calls in launch [OK]
Common Mistakes:
  • Using Dispatchers.Main for network calls
  • Confusing Dispatchers.Default with IO
  • Omitting dispatcher and running on main thread
3. What will be the output of this code snippet?
fun main() = runBlocking {
  launch(Dispatchers.IO) {
    println("Network call started")
  }
  println("Main thread continues")
}
medium
A. Main thread continues\nNetwork call started
B. Only Network call started
C. Network call started\nMain thread continues
D. Only Main thread continues

Solution

  1. Step 1: Understand runBlocking and launch behavior

    runBlocking blocks main thread until all coroutines complete. launch starts coroutine asynchronously.
  2. Step 2: Order of print statements

    "Main thread continues" prints immediately; "Network call started" prints shortly after from IO dispatcher.
  3. Final Answer:

    Main thread continues\nNetwork call started -> Option A
  4. Quick Check:

    launch is async, main prints first = C [OK]
Hint: launch runs async, main prints before coroutine output [OK]
Common Mistakes:
  • Assuming launch blocks main thread
  • Expecting network print before main print
  • Ignoring coroutine dispatchers
4. Identify the error in this coroutine code snippet:
GlobalScope.launch(Dispatchers.IO) {
  val data = fetchData()
  textView.text = data
}
medium
A. fetchData() must be called on Dispatchers.Main.
B. Cannot update UI from Dispatchers.IO thread.
C. GlobalScope cannot be used with launch.
D. textView.text should be assigned inside fetchData().

Solution

  1. Step 1: Check thread for UI update

    UI elements like textView must be updated on the main thread.
  2. Step 2: Identify misuse of Dispatchers.IO

    Dispatchers.IO runs on background thread; updating UI here causes error.
  3. Final Answer:

    Cannot update UI from Dispatchers.IO thread. -> Option B
  4. Quick Check:

    UI updates require Dispatchers.Main = A [OK]
Hint: Update UI only on Dispatchers.Main thread [OK]
Common Mistakes:
  • Updating UI on background thread
  • Misusing GlobalScope without lifecycle awareness
  • Calling fetchData() on main thread unnecessarily
5. You want to fetch user data from the network and then update the UI with the result. Which coroutine structure correctly handles this?
hard
A. launch(Dispatchers.Main) { val data = withContext(Dispatchers.IO) { fetchUser() }; updateUI(data) }
B. launch(Dispatchers.IO) { val data = fetchUser(); updateUI(data) }
C. launch { val data = fetchUser(); updateUI(data) }
D. launch(Dispatchers.Default) { val data = fetchUser(); updateUI(data) }

Solution

  1. Step 1: Identify correct dispatcher for network call

    Network call fetchUser() should run on Dispatchers.IO to avoid blocking UI.
  2. Step 2: Ensure UI update runs on main thread

    updateUI(data) must run on Dispatchers.Main to safely update UI elements.
  3. Step 3: Analyze launch(Dispatchers.Main) { val data = withContext(Dispatchers.IO) { fetchUser() }; updateUI(data) } structure

    launch on Main dispatcher, inside withContext(Dispatchers.IO) runs fetchUser(), then UI updates on Main.
  4. Final Answer:

    launch(Dispatchers.Main) { val data = withContext(Dispatchers.IO) { fetchUser() }; updateUI(data) } -> Option A
  5. Quick Check:

    Network on IO, UI on Main = A [OK]
Hint: Use withContext(IO) inside Main launch for network + UI update [OK]
Common Mistakes:
  • Updating UI on IO or Default dispatcher
  • Running network call on Main dispatcher directly
  • Not switching context for network call
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