0
0
Kotlinprogramming~20 mins

Why structured concurrency prevents leaks in Kotlin - Challenge Your Understanding

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

fun main() = runBlocking {
    launch {
        delay(100)
        println("Task 1 done")
    }
    launch {
        delay(50)
        println("Task 2 done")
    }
    println("Main done")
}
AMain done\nTask 2 done\nTask 1 done
BMain done
CTask 1 done\nTask 2 done\nMain done
DTask 2 done\nTask 1 done
Attempts:
2 left
💡 Hint
Remember that runBlocking waits for all launched coroutines inside it to complete before finishing.
🧠 Conceptual
intermediate
1:30remaining
Why does structured concurrency prevent coroutine leaks?
Which statement best explains why structured concurrency prevents coroutine leaks in Kotlin?
ABecause Kotlin coroutines use garbage collection to clean up leaked coroutines.
BBecause coroutines run on a single thread and cannot leak resources.
CBecause coroutines automatically restart if they fail, preventing leaks.
DBecause all child coroutines are tied to a parent scope and must complete or cancel before the parent finishes.
Attempts:
2 left
💡 Hint
Think about how coroutine lifecycles are managed in structured concurrency.
🔧 Debug
advanced
2:00remaining
Identify the leak caused by missing coroutine scope
What error or problem occurs when launching a coroutine without a proper parent scope in Kotlin?
Kotlin
import kotlinx.coroutines.*

fun main() {
    GlobalScope.launch {
        delay(1000)
        println("Leaked coroutine finished")
    }
    println("Main finished")
}
AThe coroutine will throw a CancellationException immediately.
BThe program will not compile due to missing coroutine scope.
CThe coroutine may continue running after main ends, causing a resource leak.
DThe coroutine will block the main thread until it finishes.
Attempts:
2 left
💡 Hint
Consider what happens when coroutines are launched in GlobalScope.
📝 Syntax
advanced
2:00remaining
Correct syntax for structured concurrency with coroutineScope
Which option correctly uses structured concurrency to launch two child coroutines and waits for both to finish?
Kotlin
import kotlinx.coroutines.*

suspend fun doWork() = coroutineScope {
    // launch two child coroutines here
}
ArunBlocking { launch { println("Child 1") } }
B
launch { delay(100); println("Child 1") }
launch { delay(200); println("Child 2") }
C
async { println("Child 1") }
async { println("Child 2") }
D
GlobalScope.launch { println("Child 1") }
GlobalScope.launch { println("Child 2") }
Attempts:
2 left
💡 Hint
Inside coroutineScope, use launch to start child coroutines tied to the scope.
🚀 Application
expert
2:30remaining
How structured concurrency handles exceptions to prevent leaks
Given this code, what happens when one child coroutine throws an exception inside coroutineScope?
Kotlin
import kotlinx.coroutines.*

suspend fun main() = coroutineScope {
    launch {
        delay(100)
        throw RuntimeException("Error in child 1")
    }
    launch {
        delay(200)
        println("Child 2 finished")
    }
}
AThe exception cancels the entire coroutineScope, so Child 2 is cancelled and "Child 2 finished" is not printed.
BThe exception is ignored, and Child 2 runs to completion printing "Child 2 finished".
CThe exception crashes the program immediately without cancelling Child 2.
DThe exception is caught automatically and printed, but both children complete.
Attempts:
2 left
💡 Hint
Think about how coroutineScope handles exceptions from child coroutines.