Kotlin and Android From Scratch Practice Test - Exam Prep Guide

Session length

1 / 20

Which of the following statements is false about async() and runBlocking() in Kotlin?

Both functions take a CoroutineScope as a parameter

Both functions return a Deferred

The reason this assertion is considered false is that while both `async()` and `runBlocking()` are part of Kotlin's coroutine library, they behave differently in terms of their return types. The `async()` function is designed to start a coroutine that will return a value, specifically a `Deferred<T>` type, which allows you to retrieve the result asynchronously using the `await()` function.

On the other hand, `runBlocking()` does not return a `Deferred`. Instead, it is used to bridge blocking code and coroutines—a function that runs a new coroutine and blocks the current thread until its completion. When using `runBlocking()`, you typically receive the value returned by the block you executed as a result of `runBlocking`, rather than a `Deferred`.

In summary, the key distinction is that `async()` does return a `Deferred`, while `runBlocking()` does not, making the statement about both functions returning a `Deferred` false. This clarity helps reinforce the understanding of when to use each function effectively, especially in the context of Kotlin coroutines in Android development.

You'll typically not use runBlocking in Android app code

When using async, you need to use await() to access the returned value

Next Question
Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy