0
0
Kotlinprogramming~10 mins

Coroutine scope and structured concurrency 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 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'
A{}
B() -> Unit
C()
D[]
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses () instead of curly braces {} after launch
Using square brackets [] which are invalid here
2fill in blank
medium

Complete 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'
ASupervisorJob()
BJob()
Claunch()
DrunBlocking()
Attempts:
3 left
💡 Hint
Common Mistakes
Using launch() which is a coroutine builder, not a job
Using runBlocking() which blocks the thread
3fill in blank
hard

Fix 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'
Ajob.join()
Bjob.cancel()
Cjob.start()
Djob.wait()
Attempts:
3 left
💡 Hint
Common Mistakes
Using cancel() which stops the coroutine prematurely
Using wait() which is not a coroutine function
4fill in blank
hard

Fill 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'
AJob()
B{}
C()
DSupervisorJob()
Attempts:
3 left
💡 Hint
Common Mistakes
Using SupervisorJob() instead of Job() for basic scope
Using parentheses () instead of curly braces {} after launch
5fill in blank
hard

Fill 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'
Akey
Bvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using key instead of value in the condition
Mixing up the order of destructured variables