Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to launch a coroutine inside the given scope.
Kotlin
fun main() = runBlocking {
launch [1]
println("Hello from coroutine")
}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses () instead of curly braces {} after launch
Using square brackets [] which are invalid here
✗ Incorrect
The launch function takes a lambda block enclosed in curly braces {} to define the coroutine body.
2fill in blank
mediumComplete the code to create a new CoroutineScope with a Job.
Kotlin
val scope = CoroutineScope([1] + Dispatchers.Default) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using launch() which is a coroutine builder, not a job
Using runBlocking() which blocks the thread
✗ Incorrect
CoroutineScope requires a Job instance to manage its lifecycle; Job() creates a new job.
3fill in blank
hardFix the error in the coroutine builder to properly wait for completion.
Kotlin
fun main() = runBlocking {
val job = launch {
delay(1000L)
println("Done")
}
[1]
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using cancel() which stops the coroutine prematurely
Using wait() which is not a coroutine function
✗ Incorrect
Calling job.join() suspends until the coroutine completes, ensuring "Done" is printed before main ends.
4fill in blank
hardFill both blanks to create a coroutine scope and launch a child coroutine.
Kotlin
fun main() = runBlocking {
val scope = CoroutineScope([1] + Dispatchers.IO)
scope.launch [2]
println("Child coroutine running")
}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using SupervisorJob() instead of Job() for basic scope
Using parentheses () instead of curly braces {} after launch
✗ Incorrect
Job() creates the scope's job; launch requires a lambda block {} to define the coroutine body.
5fill in blank
hardFill all three blanks to create a map of coroutine results filtering only successful ones.
Kotlin
val results = mapOf(
"task1" to 10,
"task2" to 0,
"task3" to 5
)
val filtered = results.filter { ([1], [2]) -> [3] > 0 } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using key instead of value in the condition
Mixing up the order of destructured variables
✗ Incorrect
The filter destructures the map entries into key and value; filtering keeps entries where value > 0.