0
0
Kotlinprogramming~10 mins

SupervisorJob for independent failure in Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a SupervisorJob instance.

Kotlin
val supervisor = [1]()
Drag options to blanks, or click blank then click option'
AJob
Blaunch
CSupervisorJob
DCoroutineScope
Attempts:
3 left
💡 Hint
Common Mistakes
Using Job() instead of SupervisorJob()
Using CoroutineScope() which is not a Job
Using launch() which is a coroutine builder
2fill in blank
medium

Complete the code to launch a child coroutine with the supervisor job.

Kotlin
val scope = CoroutineScope(Dispatchers.Default + [1]())
scope.launch {
    println("Child coroutine running")
}
Drag options to blanks, or click blank then click option'
ADispatchers.IO
BJob
CCoroutineScope
DSupervisorJob
Attempts:
3 left
💡 Hint
Common Mistakes
Using Job() which cancels all children on failure
Using CoroutineScope() inside CoroutineScope constructor
Using Dispatchers.IO which is a dispatcher, not a job
3fill in blank
hard

Fix the error in the code to ensure child coroutines fail independently.

Kotlin
val supervisor = Job()
val scope = CoroutineScope(Dispatchers.Default + [1])
scope.launch {
    throw Exception("Failure")
}
Drag options to blanks, or click blank then click option'
ASupervisorJob()
BJob()
Csupervisor
DDispatchers.IO
Attempts:
3 left
💡 Hint
Common Mistakes
Using Job() which cancels all children on failure
Using Dispatchers.IO which is a dispatcher, not a job
Using variable name instead of a job instance
4fill in blank
hard

Fill all three blanks to create a supervisor scope and launch two child coroutines where one fails independently.

Kotlin
val supervisor = [1]()
val scope = CoroutineScope(Dispatchers.Default + supervisor)
scope.[2] {
    println("Child 1 running")
}
scope.[3] {
    throw Exception("Child 2 failure")
}
Drag options to blanks, or click blank then click option'
ASupervisorJob
BJob
CCoroutineScope
Dlaunch
Attempts:
3 left
💡 Hint
Common Mistakes
Using Job() instead of SupervisorJob()
Using CoroutineScope instead of launch for starting coroutines
5fill in blank
hard

Fill all three blanks to create a supervisor scope, launch a child coroutine that fails, and another that continues.

Kotlin
val supervisor = [1]()
val scope = CoroutineScope(Dispatchers.Default + supervisor)
scope.[2] {
    throw Exception("Failure in child")
}
scope.[3] {
    println("Independent child running")
}
Drag options to blanks, or click blank then click option'
AJob
Blaunch
CSupervisorJob
Dasync
Attempts:
3 left
💡 Hint
Common Mistakes
Using Job() instead of SupervisorJob()
Using async instead of launch which changes coroutine behavior