Explain the differences between `withContext`, `async`, and `launch` and provide examples showing when each is most appropriate.

Android interview question for Advanced practice.

Answer

withContext is for switching contexts; async returns a Deferred for results; launch is for fire-and-forget operations.

Explanation

Option A correctly summarizes the key differences. withContext is for changing the coroutine's context (e.g., dispatcher), async returns a Deferred that can be used to get a result later, and launch is for starting a coroutine without waiting for its completion. Options B, C, and D are incorrect. withContext, async, and launch are distinct and serve different purposes in concurrent programming.

Related Questions