0
0
Kotlinprogramming~20 mins

SupervisorJob for independent failure in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
SupervisorJob Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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}")
}
AChild 2 failed exception is ignored, Child 1 is cancelled, then prints: Scope is active: false
BChild 2 failed exception is thrown and Child 1 is cancelled, no further output.
CChild 2 failed exception is thrown, Child 1 completes, then prints: Scope is active: false
DChild 2 failed exception is ignored, Child 1 completes, then prints: Scope is active: true
Attempts:
2 left
πŸ’‘ Hint
SupervisorJob allows child coroutines to fail independently without cancelling siblings.
🧠 Conceptual
intermediate
1:30remaining
Why use SupervisorJob in Kotlin coroutines?
Which of the following best explains the main purpose of using SupervisorJob in Kotlin coroutines?
ATo ensure that if one child coroutine fails, all sibling coroutines are cancelled immediately.
BTo allow child coroutines to fail independently without cancelling their siblings or the parent scope.
CTo automatically restart failed child coroutines without manual intervention.
DTo run all child coroutines sequentially instead of concurrently.
Attempts:
2 left
πŸ’‘ Hint
Think about failure isolation between child coroutines.
πŸ”§ Debug
advanced
2: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")
}
AThe CoroutineScope is missing the coroutineContext, so SupervisorJob is not combined with Dispatchers, causing unexpected behavior.
BSupervisorJob should be replaced with Job to isolate failures properly.
CThe child coroutines are not launched with async, so exceptions are not caught.
DThe delay times cause child1 to finish before child2 throws an exception, so no error is observed.
Attempts:
2 left
πŸ’‘ Hint
Check how CoroutineScope is created with context elements.
❓ Predict Output
advanced
2: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()
}
A
Caught exception: Child 2 failed
Parent scope active: true
Child 1 finished
B
Caught exception: Child 2 failed
Parent scope active: false
Child 1 finished
C
No exception caught
Parent scope active: false
Child 1 finished
D
No exception caught
Parent scope active: true
Child 1 finished
Attempts:
2 left
πŸ’‘ Hint
SupervisorJob prevents parent cancellation on child failure.
🧠 Conceptual
expert
3: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?
ASupervisorJob propagates exceptions from any child coroutine to the parent immediately, cancelling all siblings.
BSupervisorJob suppresses all exceptions from child coroutines, so the parent never sees any failures.
CSupervisorJob allows child coroutines to fail independently; exceptions do not cancel siblings or the parent, but are still propagated to the parent’s handler if unhandled.
DSupervisorJob queues exceptions from child coroutines and throws them only after all children complete.
Attempts:
2 left
πŸ’‘ Hint
Think about failure isolation and exception visibility.