0
0
Android Kotlinmobile~10 mins

Why coroutines simplify async programming in Android Kotlin - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to launch a coroutine in the main scope.

Android Kotlin
GlobalScope.[1] {
  println("Hello from coroutine")
}
Drag options to blanks, or click blank then click option'
Aasync
BrunBlocking
Cdelay
Dlaunch
Attempts:
3 left
💡 Hint
Common Mistakes
Using runBlocking blocks the thread, which is not desired here.
Using delay is a suspending function, not a coroutine builder.
2fill in blank
medium

Complete the code to suspend a coroutine for 1 second.

Android Kotlin
suspend fun waitOneSecond() {
  [1](1000L)
}
Drag options to blanks, or click blank then click option'
Await
BThread.sleep
Cdelay
Dsleep
Attempts:
3 left
💡 Hint
Common Mistakes
Using Thread.sleep blocks the thread, which defeats the purpose of coroutines.
3fill in blank
hard

Fix the error in the coroutine builder to return a deferred result.

Android Kotlin
val deferred = GlobalScope.[1] {
  42
}
Drag options to blanks, or click blank then click option'
Aasync
Bdelay
CrunBlocking
Dlaunch
Attempts:
3 left
💡 Hint
Common Mistakes
Using launch returns Job, not a result.
Using delay is not a builder.
4fill in blank
hard

Fill both blanks to create a coroutine scope and launch a coroutine.

Android Kotlin
val scope = CoroutineScope([1] + Job())
scope.[2] {
  println("Running in scope")
}
Drag options to blanks, or click blank then click option'
ADispatchers.Main
Blaunch
CDispatchers.IO
Dasync
Attempts:
3 left
💡 Hint
Common Mistakes
Using async when no result is needed.
Using Dispatchers.IO for UI work.
5fill in blank
hard

Fill the two blanks to create a suspending function that fetches data asynchronously.

Android Kotlin
suspend fun fetchData(): String {
  return withContext([1]) {
    val data = apiCall()
    if (data.isNotEmpty()) data else [2]
  }
}
Drag options to blanks, or click blank then click option'
ADispatchers.IO
Breturn
C"No data"
Dprintln
Attempts:
3 left
💡 Hint
Common Mistakes
Using Dispatchers.Main for network calls.
Using println instead of returning data.