0
0
Kotlinprogramming~20 mins

Coroutine scope and structured concurrency in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Coroutine Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of nested coroutine scopes
What is the output of this Kotlin code using coroutine scopes and structured concurrency?
Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    launch {
        delay(100)
        println("Task 1 done")
    }
    coroutineScope {
        launch {
            delay(50)
            println("Task 2 done")
        }
        println("Inside coroutineScope")
    }
    println("Outside coroutineScope")
}
A
Inside coroutineScope
Task 2 done
Task 1 done
Outside coroutineScope
B
Task 1 done
Task 2 done
Inside coroutineScope
Outside coroutineScope
C
Inside coroutineScope
Task 1 done
Task 2 done
Outside coroutineScope
D
Task 2 done
Inside coroutineScope
Task 1 done
Outside coroutineScope
Attempts:
2 left
💡 Hint
Remember that coroutineScope waits for all its child coroutines to complete before continuing.
Predict Output
intermediate
2:00remaining
Effect of cancelling a parent coroutine scope
What will be printed when this Kotlin code runs?
Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    val parentJob = launch {
        launch {
            delay(100)
            println("Child 1 finished")
        }
        launch {
            delay(200)
            println("Child 2 finished")
        }
    }
    delay(150)
    parentJob.cancel()
    delay(200)
    println("Done")
}
ADone
B
Child 1 finished
Child 2 finished
Done
C
Child 1 finished
Done
D
Child 2 finished
Done
Attempts:
2 left
💡 Hint
Cancelling a parent coroutine cancels all its children immediately.
🔧 Debug
advanced
2:00remaining
Identify the error in coroutine scope usage
This Kotlin code throws an exception. What is the cause?
Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    val job = GlobalScope.launch {
        delay(100)
        println("Hello from GlobalScope")
    }
    job.join()
    println("Done")
}
ANo error; code runs and prints both lines.
BThe use of GlobalScope.launch breaks structured concurrency and may cause unexpected behavior.
Cjob.join() causes a deadlock in runBlocking.
DMissing coroutineScope block causes compilation error.
Attempts:
2 left
💡 Hint
GlobalScope is not tied to the lifecycle of runBlocking.
📝 Syntax
advanced
2:00remaining
Correct syntax for coroutineScope usage
Which option correctly uses coroutineScope to launch two child coroutines and waits for both to finish?
Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    // Fill in the blank
}
A
coroutineScope {
    launch { println("Child 1") }
    launch { println("Child 2") }
}
B
launch {
    coroutineScope {
        launch { println("Child 1") }
        launch { println("Child 2") }
    }
}
C
coroutineScope {
    launch { println("Child 1") }
}
launch { println("Child 2") }
D
runBlocking {
    launch { println("Child 1") }
    launch { println("Child 2") }
}
Attempts:
2 left
💡 Hint
coroutineScope waits for all child coroutines inside it to complete before returning.
🚀 Application
expert
2:00remaining
How many coroutines are active at peak in this code?
Consider this Kotlin code snippet. How many coroutines run concurrently at the peak?
Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    launch {
        repeat(3) { i ->
            launch {
                delay(100L * i)
                println("Inner coroutine $i done")
            }
        }
    }
    launch {
        delay(50)
        println("Another coroutine done")
    }
}
A2
B3
C5
D4
Attempts:
2 left
💡 Hint
Count all coroutines launched that overlap in time at the peak.