0
0
Android Kotlinmobile~20 mins

withContext for thread switching in Android Kotlin - Practice Problems & Coding Challenges

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

What is the effect of using withContext(Dispatchers.IO) here?
Android Kotlin
GlobalScope.launch(Dispatchers.Main) {
  val data = withContext(Dispatchers.IO) {
    fetchDataFromNetwork()
  }
  updateUI(data)
}
AThe network call runs on a background thread, keeping the UI responsive while updating UI on the main thread.
BThe network call runs on the main thread, causing the UI to freeze until it finishes.
CThe UI updates happen on the IO thread, which can cause crashes.
DThe entire coroutine runs on the IO thread, so UI updates are delayed.
Attempts:
2 left
💡 Hint
Think about which thread handles UI updates and which handles long-running tasks.
🧠 Conceptual
intermediate
1:30remaining
What does withContext do in Kotlin coroutines?
Choose the best description of what withContext does in Kotlin coroutines.
AIt cancels the coroutine and all its child coroutines.
BIt creates a new coroutine that runs concurrently with the current one.
CIt pauses the coroutine indefinitely until manually resumed.
DIt switches the coroutine context to a different thread or dispatcher for the block of code inside it.
Attempts:
2 left
💡 Hint
Think about how you run code on different threads inside a coroutine.
lifecycle
advanced
2:00remaining
What happens if you call withContext(Dispatchers.Main) inside withContext(Dispatchers.IO)?
Given this nested coroutine code:
withContext(Dispatchers.IO) {
  // background work
  withContext(Dispatchers.Main) {
    updateUI()
  }
}

What is the behavior of this code?
Android Kotlin
withContext(Dispatchers.IO) {
  // background work
  withContext(Dispatchers.Main) {
    updateUI()
  }
}
AIt causes a deadlock because switching threads inside withContext is not allowed.
BThe code switches to IO thread, then back to Main thread to update UI safely.
CThe updateUI runs on the IO thread, risking UI crashes.
DThe entire block runs on the Main thread ignoring the IO dispatcher.
Attempts:
2 left
💡 Hint
Think about how withContext can be nested to switch threads multiple times.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in this coroutine with withContext usage
What is wrong with this Kotlin code snippet?
launch(Dispatchers.Main) {
  val result = withContext(Dispatchers.IO) 
    fetchData()
  display(result)
}
Android Kotlin
launch(Dispatchers.Main) {
  val result = withContext(Dispatchers.IO) 
    fetchData()
  display(result)
}
AMissing braces {} around the withContext block causes a syntax error.
Blaunch must be called with GlobalScope.launch, otherwise it fails.
CfetchData() must be called on the main thread, so this is a runtime error.
Ddisplay(result) must be inside withContext block to access result.
Attempts:
2 left
💡 Hint
Check how withContext expects its code block syntax.
🔧 Debug
expert
2:30remaining
Why does this withContext code cause a crash?
Analyze this code:
GlobalScope.launch(Dispatchers.IO) {
  val data = fetchData()
  withContext(Dispatchers.Main) {
    updateUI(data)
  }
}

fun updateUI(data: String) {
  textView.text = data
}

Why might this code crash the app?
Android Kotlin
GlobalScope.launch(Dispatchers.IO) {
  val data = fetchData()
  withContext(Dispatchers.Main) {
    updateUI(data)
  }
}

fun updateUI(data: String) {
  textView.text = data
}
AtextView is accessed without being on the main thread, causing a crash.
BGlobalScope.launch with Dispatchers.IO does not allow switching to Main dispatcher.
CtextView is null or not initialized, causing a NullPointerException.
DfetchData() must be called inside withContext(Dispatchers.Main) to update UI.
Attempts:
2 left
💡 Hint
Check if UI elements are properly initialized before use.