0
0
Kotlinprogramming~10 mins

Why coroutines matter for async programming in 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 that prints "Hello".

Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    launch [1] {
        println("Hello")
    }
}
Drag options to blanks, or click blank then click option'
A()
B() -> Unit
C{}
DUnit
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses () instead of curly braces {} for the coroutine block.
Passing a function type instead of a lambda block.
2fill in blank
medium

Complete the code to suspend the coroutine for 1000 milliseconds.

Kotlin
import kotlinx.coroutines.*

suspend fun waitOneSecond() {
    [1](1000L)
}
Drag options to blanks, or click blank then click option'
Adelay
Bsleep
Cpause
Dwait
Attempts:
3 left
💡 Hint
Common Mistakes
Using Thread.sleep which blocks the thread instead of suspending the coroutine.
Using wait or pause which are not coroutine functions.
3fill in blank
hard

Fix the error in the coroutine builder to properly start a coroutine.

Kotlin
import kotlinx.coroutines.*

fun main() {
    GlobalScope.launch [1] {
        println("Running")
    }
    Thread.sleep(500L)
}
Drag options to blanks, or click blank then click option'
A{}
B() -> Unit
C()
DUnit
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses () instead of curly braces {} for the coroutine block.
Passing a function type instead of a lambda block.
4fill in blank
hard

Fill both blanks to create a map of words to their lengths, filtering words longer than 3 characters.

Kotlin
val words = listOf("cat", "house", "dog", "elephant")
val lengths = words.associateWith { [1] }
    .filter { it.value [2] 3 }
Drag options to blanks, or click blank then click option'
Ait.length
B>
C<
Dit.size
Attempts:
3 left
💡 Hint
Common Mistakes
Using it.size which is not valid for String.
Using < instead of > for filtering longer words.
5fill in blank
hard

Fill all three blanks to create a map of uppercase words to their lengths, filtering words longer than 4 characters.

Kotlin
val words = listOf("apple", "bat", "carrot", "dog")
val result = words.associateBy(
    keySelector = { [1] },
    valueTransform = { [2] }
).filter { it.value [3] 4 }
Drag options to blanks, or click blank then click option'
Ait.uppercase()
Bit.length
C>
Dit.toUpperCase()
Attempts:
3 left
💡 Hint
Common Mistakes
Using deprecated it.toUpperCase() instead of it.uppercase().
Using < instead of > for filtering.
Mixing keySelector and valueTransform parameters.