Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The launch function takes a lambda block denoted by curly braces {} to define the coroutine's code.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
The delay function suspends the coroutine without blocking the thread.
3fill in blank
hardFix 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'
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.
✗ Incorrect
launch requires a lambda block with curly braces {} to define the coroutine's code.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using it.size which is not valid for String.
Using < instead of > for filtering longer words.
✗ Incorrect
Use it.length to get word length and > 3 to filter longer words.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using deprecated it.toUpperCase() instead of it.uppercase().
Using < instead of > for filtering.
Mixing keySelector and valueTransform parameters.
✗ Incorrect
Use it.uppercase() for uppercase keys, it.length for values, and > 4 to filter.