Recall & Review
beginner
What is a
Job in Kotlin coroutines?A
Job represents a cancellable task or coroutine. It controls the coroutine's lifecycle, allowing you to start, cancel, or check its status.Click to reveal answer
intermediate
Name the main states in a Job's lifecycle.
The main states are:
- New: Created but not started.
- Active: Running or suspended.
- Completing: Finishing work.
- Cancelled: Stopped before completion.
- Completed: Finished successfully.
Click to reveal answer
beginner
How do you cancel a Job in Kotlin?
You call the
cancel() function on the Job instance. This requests cancellation, and the coroutine cooperatively stops if it checks for cancellation.Click to reveal answer
intermediate
What happens if a coroutine ignores cancellation?
If a coroutine does not check for cancellation (e.g., by calling
yield() or checking isActive), it may continue running even after cancel() is called, delaying cancellation.Click to reveal answer
intermediate
Explain cooperative cancellation in Kotlin coroutines.
Cooperative cancellation means the coroutine must regularly check if it is cancelled and stop work accordingly. This is done by checking
isActive or using cancellable suspending functions.Click to reveal answer
Which function is used to stop a running Job in Kotlin?
✗ Incorrect
The
cancel() function requests cancellation of a Job.What state is a Job in when it has finished successfully?
✗ Incorrect
A Job is Completed when it finishes its work successfully.
What must a coroutine do to respond to cancellation?
✗ Incorrect
Coroutines must cooperatively check for cancellation to stop execution.
Which of these is NOT a Job lifecycle state?
✗ Incorrect
There is no Paused state in Job lifecycle.
What happens if you call
cancel() on a Job but the coroutine never checks for cancellation?✗ Incorrect
Without cooperative checks, the coroutine keeps running despite cancellation requests.
Describe the lifecycle of a Kotlin Job and how cancellation affects it.
Think about how a Job moves from new to completed or cancelled.
You got /4 concepts.
Explain why cooperative cancellation is important in Kotlin coroutines.
Consider what happens if a coroutine ignores cancellation.
You got /4 concepts.