0
0
Android Kotlinmobile~10 mins

launch and async builders in Android Kotlin - Interactive Code Practice

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'
Alaunch
Basync
CrunBlocking
Ddelay
Attempts:
3 left
💡 Hint
Common Mistakes
Using async instead of launch when no result is needed.
Using delay as a builder, which is a suspending function.
2fill in blank
medium

Complete the code to start an async coroutine that returns a value.

Android Kotlin
val deferred = GlobalScope.[1] {
    delay(1000)
    return@[1] 42
}
Drag options to blanks, or click blank then click option'
Aasync
Blaunch
CrunBlocking
DwithContext
Attempts:
3 left
💡 Hint
Common Mistakes
Using launch when a result is needed.
Using runBlocking inside GlobalScope.
3fill in blank
hard

Fix the error in the code by choosing the correct coroutine builder.

Android Kotlin
val result = GlobalScope.[1] {
    delay(500)
    10 + 20
}
println(result.await())
Drag options to blanks, or click blank then click option'
Aproduce
Basync
CrunBlocking
Dlaunch
Attempts:
3 left
💡 Hint
Common Mistakes
Using launch and trying to call await() on Job.
Using runBlocking inside GlobalScope.
4fill in blank
hard

Fill both blanks to create a coroutine that launches and then waits for an async result.

Android Kotlin
GlobalScope.[1] {
    val deferred = async {
        delay(300)
        5 * 5
    }
    val result = deferred.[2]()
    println(result)
}
Drag options to blanks, or click blank then click option'
Alaunch
Basync
Cawait
Djoin
Attempts:
3 left
💡 Hint
Common Mistakes
Using async for the outer coroutine when launch is better for fire-and-forget.
Using join() instead of await() to get the result.
5fill in blank
hard

Fill all three blanks to create an async coroutine that returns a computed value after delay.

Android Kotlin
val deferred = GlobalScope.[1] {
    delay(200)
    val x = 10
    val y = 20
    x [2] y
}
println(deferred.[3]())
Drag options to blanks, or click blank then click option'
Aasync
B+
Cawait
Dlaunch
Attempts:
3 left
💡 Hint
Common Mistakes
Using launch instead of async when a result is needed.
Using join() instead of await() to get the result.