0
0
Kotlinprogramming~10 mins

Async and await for concurrent results in 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 start an asynchronous task using Kotlin coroutines.

Kotlin
val deferred = scope.[1] {
    delay(1000L)
    "Hello"
}
Drag options to blanks, or click blank then click option'
AwithContext
Blaunch
CrunBlocking
Dasync
Attempts:
3 left
💡 Hint
Common Mistakes
Using launch instead of async, which does not return a Deferred result.
Using runBlocking inside a coroutine scope incorrectly.
2fill in blank
medium

Complete the code to wait for the result of an asynchronous task.

Kotlin
val result = deferred.[1]()
Drag options to blanks, or click blank then click option'
Aawait
Bcancel
Cjoin
Dstart
Attempts:
3 left
💡 Hint
Common Mistakes
Using join() instead of await(), which does not return the result.
Trying to call start() which is not a function on Deferred.
3fill in blank
hard

Fix the error in the code to run two async tasks concurrently and get their results.

Kotlin
val deferred1 = scope.async { fetchData1() }
val deferred2 = scope.async { fetchData2() }
val result1 = deferred1.[1]()
val result2 = deferred2.[1]()
Drag options to blanks, or click blank then click option'
Ajoin
Bawait
Cstart
Dcancel
Attempts:
3 left
💡 Hint
Common Mistakes
Using join() instead of await(), causing no result to be returned.
Calling start() which is not a method on Deferred.
4fill in blank
hard

Fill both blanks to create a map of word lengths for words longer than 3 characters.

Kotlin
val words = listOf("apple", "bat", "carrot", "dog")
val lengths = words.filter { it.length [1] 3 }.associateWith { it.[2] }
Drag options to blanks, or click blank then click option'
A>
Blength
C<
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' causing wrong filtering.
Using count() instead of length property.
5fill in blank
hard

Fill all three blanks to create a map of uppercase keys to values greater than zero.

Kotlin
val data = mapOf("a" to 1, "b" to 0, "c" to 3)
val result = data.filter { it.value [1] 0 }.mapKeys { it.key.[2]() }.mapValues { it.[3] }
Drag options to blanks, or click blank then click option'
A<
Bvalue
C>
Duppercase
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in filter condition.
Using key instead of value in mapValues.