Challenge - 5 Problems
SupervisorJob Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2:00remaining
What is the output of this Kotlin coroutine code using SupervisorJob?
Consider the following Kotlin code snippet using
SupervisorJob. What will be printed when it runs?Kotlin
import kotlinx.coroutines.* fun main() = runBlocking { val supervisor = SupervisorJob() val scope = CoroutineScope(coroutineContext + supervisor) val child1 = scope.launch { delay(100) println("Child 1 completed") } val child2 = scope.launch { delay(50) throw RuntimeException("Child 2 failed") } child1.join() try { child2.join() } catch (e: Exception) { // Child 2 failure ignored } println("Scope is active: ${scope.isActive}") }
Attempts:
2 left
π‘ Hint
SupervisorJob allows child coroutines to fail independently without cancelling siblings.
β Incorrect
SupervisorJob lets child coroutines fail without cancelling other children or the parent scope. So, Child 2's failure does not cancel Child 1, which completes successfully. The scope remains active.
π§ Conceptual
intermediate1:30remaining
Why use SupervisorJob in Kotlin coroutines?
Which of the following best explains the main purpose of using
SupervisorJob in Kotlin coroutines?Attempts:
2 left
π‘ Hint
Think about failure isolation between child coroutines.
β Incorrect
SupervisorJob is designed so that failure in one child coroutine does not affect others or the parent scope, allowing independent failure handling.
π§ Debug
advanced2:30remaining
Identify the error in this coroutine code using SupervisorJob
This Kotlin code intends to use SupervisorJob to isolate child coroutine failures. What is the error causing the program to not behave as expected?
Kotlin
import kotlinx.coroutines.* fun main() = runBlocking { val supervisor = SupervisorJob() val scope = CoroutineScope(coroutineContext + supervisor) val child1 = scope.launch { delay(100) println("Child 1 done") } val child2 = scope.launch { delay(50) throw Exception("Child 2 error") } child1.join() child2.join() println("Done") }
Attempts:
2 left
π‘ Hint
Check how CoroutineScope is created with context elements.
β Incorrect
CoroutineScope should combine SupervisorJob with the existing coroutineContext (like Dispatchers) to work properly. Using only SupervisorJob loses dispatcher context, causing unexpected behavior.
β Predict Output
advanced2:30remaining
What happens to the parent scope when a child coroutine fails with SupervisorJob?
Given this Kotlin code, what will be the output and state of the parent scope after child2 fails?
Kotlin
import kotlinx.coroutines.* fun main() = runBlocking { val supervisor = SupervisorJob() val scope = CoroutineScope(coroutineContext + supervisor) val child1 = scope.launch { delay(100) println("Child 1 finished") } val child2 = scope.launch { delay(50) throw IllegalStateException("Child 2 failed") } try { child2.join() } catch (e: Exception) { println("Caught exception: ${e.message}") } println("Parent scope active: ${scope.isActive}") child1.join() }
Attempts:
2 left
π‘ Hint
SupervisorJob prevents parent cancellation on child failure.
β Incorrect
The exception from child2 is caught, the parent scope remains active, and child1 completes successfully.
π§ Conceptual
expert3:00remaining
How does SupervisorJob affect exception propagation in Kotlin coroutines?
Which statement best describes how
SupervisorJob changes exception propagation compared to a regular Job in Kotlin coroutines?Attempts:
2 left
π‘ Hint
Think about failure isolation and exception visibility.
β Incorrect
SupervisorJob isolates failures so siblings and parent are not cancelled, but exceptions still propagate to the parentβs CoroutineExceptionHandler if unhandled.