0
0
Kotlinprogramming~20 mins

Job lifecycle and cancellation in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Job Lifecycle Master
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 coroutine job cancellation code?
Consider the following Kotlin code using coroutines. What will be printed when this code runs?
Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    val job = launch {
        repeat(3) { i ->
            println("Working on $i ...")
            delay(100L)
        }
    }
    delay(250L)
    job.cancel()
    job.join()
    println("Done")
}
AWorking on 0 ...\nWorking on 1 ...\nDone
BWorking on 0 ...\nWorking on 1 ...\nWorking on 2 ...\nDone
CWorking on 0 ...\nDone
DDone
Attempts:
2 left
💡 Hint
Think about when the job is cancelled and how many iterations complete before cancellation.
🧠 Conceptual
intermediate
1:30remaining
Which state does a Job enter after calling cancel() but before completion?
In Kotlin coroutines, after you call cancel() on a Job but before it finishes, what is the Job's state?
AActive
BCancelled
CNew
DCompleting
Attempts:
2 left
💡 Hint
Think about the transition phase between active and fully cancelled.
🔧 Debug
advanced
2:30remaining
Why does this coroutine not cancel as expected?
Examine the code below. The job.cancel() is called, but the coroutine keeps running. Why?
Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    val job = launch {
        for (i in 1..5) {
            Thread.sleep(100L)
            println("Working $i")
        }
    }
    delay(250L)
    job.cancel()
    job.join()
    println("Finished")
}
Adelay() inside the coroutine is missing, so cancellation is ignored.
BThread.sleep blocks the thread and prevents cancellation checks inside the coroutine.
CThe coroutine is not cancellable because it uses launch instead of async.
Djob.cancel() was called too late after the coroutine finished.
Attempts:
2 left
💡 Hint
Consider how blocking calls affect coroutine cancellation.
📝 Syntax
advanced
1:30remaining
Which option correctly cancels a Job and waits for its completion?
Select the Kotlin code snippet that cancels a Job and waits until it finishes properly.
A
job.cancel()
job.join()
B
job.cancel()
job.cancelAndJoin()
Cjob.cancelAndJoin()
D
job.join()
job.cancel()
Attempts:
2 left
💡 Hint
Look for the built-in function that combines cancel and join.
🚀 Application
expert
2:30remaining
How many times will 'Task running' print before cancellation?
Given the code below, how many times will 'Task running' be printed before the job is cancelled?
Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    val job = launch {
        repeat(10) { i ->
            if (!isActive) return@launch
            println("Task running $i")
            delay(50L)
        }
    }
    delay(230L)
    job.cancelAndJoin()
    println("Job cancelled")
}
A5
B6
C4
D7
Attempts:
2 left
💡 Hint
Calculate how many 50ms delays fit into 230ms before cancellation.