0
0
Kotlinprogramming~5 mins

SupervisorJob for independent failure in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is SupervisorJob in Kotlin coroutines?

SupervisorJob is a special kind of job that allows child coroutines to fail independently without cancelling their siblings.

Click to reveal answer
beginner
How does SupervisorJob differ from a regular Job in Kotlin?

In a regular Job, if one child coroutine fails, all siblings are cancelled. With SupervisorJob, one child's failure does not affect others.

Click to reveal answer
beginner
Why use SupervisorJob in a coroutine scope?

Use SupervisorJob when you want child coroutines to run independently so that failure in one does not stop others from running.

Click to reveal answer
intermediate
What happens to the parent coroutine when a child fails under SupervisorJob?

The parent coroutine does not get cancelled automatically when a child fails under SupervisorJob. It continues running.

Click to reveal answer
intermediate
Show a simple Kotlin example using SupervisorJob where one child fails but others continue.
val supervisor = SupervisorJob()
val scope = CoroutineScope(Dispatchers.Default + supervisor)

scope.launch {
  delay(100)
  throw Exception("Child 1 failed")
}
scope.launch {
  delay(200)
  println("Child 2 completed")
}

Output:<br>Child 2 completed

Click to reveal answer
What does SupervisorJob allow in Kotlin coroutines?
AChild coroutines fail independently without cancelling siblings
BAll child coroutines cancel together on any failure
CIt cancels the parent coroutine on any child failure
DIt prevents any coroutine from failing
If a child coroutine fails under a regular Job, what happens?
AOnly the failed child is cancelled
BNothing happens
CThe parent coroutine continues unaffected
DAll sibling coroutines are cancelled
Which of these is true about SupervisorJob?
AIt cancels the parent on child failure
BIt allows siblings to continue after a child failure
CIt prevents any coroutine from starting
DIt is the default job for all coroutines
How do you create a coroutine scope with SupervisorJob?
ACoroutineScope(Dispatchers.Default + Job())
BCoroutineScope(Dispatchers.Default)
CCoroutineScope(Dispatchers.Default + SupervisorJob())
DCoroutineScope(SupervisorJob())
What will be printed in this code?<br>
val scope = CoroutineScope(Dispatchers.Default + SupervisorJob())
scope.launch { throw Exception("Fail") }
scope.launch { println("Hello") }
AHello
BNothing, all coroutines cancelled
CException message printed
DHello and Exception message printed
Explain how SupervisorJob helps manage failures in child coroutines.
Think about what happens when one child fails but others keep working.
You got /3 concepts.
    Describe how to create a coroutine scope that uses SupervisorJob and why you would do that.
    Focus on the syntax and the benefit of independent failure.
    You got /3 concepts.