Bird
Raised Fist0
Kotlinprogramming~10 mins

Coroutine scope and structured concurrency in Kotlin - Interactive Code Practice

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

Complete the code to launch a coroutine inside the given scope.

Kotlin
fun main() = runBlocking {
    launch [1]
        println("Hello from coroutine")
}
Drag options to blanks, or click blank then click option'
A{}
B() -> Unit
C()
D[]
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses () instead of curly braces {} after launch
Using square brackets [] which are invalid here
2fill in blank
medium

Complete the code to create a new CoroutineScope with a Job.

Kotlin
val scope = CoroutineScope([1] + Dispatchers.Default)
Drag options to blanks, or click blank then click option'
ASupervisorJob()
BJob()
Claunch()
DrunBlocking()
Attempts:
3 left
💡 Hint
Common Mistakes
Using launch() which is a coroutine builder, not a job
Using runBlocking() which blocks the thread
3fill in blank
hard

Fix the error in the coroutine builder to properly wait for completion.

Kotlin
fun main() = runBlocking {
    val job = launch {
        delay(1000L)
        println("Done")
    }
    [1]
}
Drag options to blanks, or click blank then click option'
Ajob.join()
Bjob.cancel()
Cjob.start()
Djob.wait()
Attempts:
3 left
💡 Hint
Common Mistakes
Using cancel() which stops the coroutine prematurely
Using wait() which is not a coroutine function
4fill in blank
hard

Fill both blanks to create a coroutine scope and launch a child coroutine.

Kotlin
fun main() = runBlocking {
    val scope = CoroutineScope([1] + Dispatchers.IO)
    scope.launch [2]
        println("Child coroutine running")
}
Drag options to blanks, or click blank then click option'
AJob()
B{}
C()
DSupervisorJob()
Attempts:
3 left
💡 Hint
Common Mistakes
Using SupervisorJob() instead of Job() for basic scope
Using parentheses () instead of curly braces {} after launch
5fill in blank
hard

Fill all three blanks to create a map of coroutine results filtering only successful ones.

Kotlin
val results = mapOf(
    "task1" to 10,
    "task2" to 0,
    "task3" to 5
)

val filtered = results.filter { ([1], [2]) -> [3] > 0 }
Drag options to blanks, or click blank then click option'
Akey
Bvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using key instead of value in the condition
Mixing up the order of destructured variables

Practice

(1/5)
1. What is the main purpose of a CoroutineScope in Kotlin?
easy
A. To block the main thread until coroutines finish
B. To create threads manually for parallel execution
C. To group coroutines and manage their lifecycle together
D. To convert coroutines into synchronous functions

Solution

  1. Step 1: Understand CoroutineScope role

    A CoroutineScope groups coroutines so they can be managed as one unit.
  2. Step 2: Compare other options

    The other options describe unrelated or incorrect behaviors for CoroutineScope.
  3. Final Answer:

    To group coroutines and manage their lifecycle together -> Option C
  4. Quick Check:

    CoroutineScope groups coroutines [OK]
Hint: CoroutineScope manages coroutines lifecycle as a group [OK]
Common Mistakes:
  • Thinking CoroutineScope creates threads
  • Confusing CoroutineScope with blocking calls
  • Assuming CoroutineScope converts coroutines to sync
2. Which of the following is the correct way to launch a coroutine within a CoroutineScope?
easy
A. scope.launch { /* coroutine code */ }
B. launch.scope { /* coroutine code */ }
C. CoroutineScope.launch { /* coroutine code */ }
D. scope.run { /* coroutine code */ }

Solution

  1. Step 1: Recall coroutine launch syntax

    Within a CoroutineScope, you call launch as a function on the scope instance.
  2. Step 2: Check options for syntax correctness

    scope.launch { /* coroutine code */ } uses scope.launch {} which is correct. Others misuse syntax or functions.
  3. Final Answer:

    scope.launch { /* coroutine code */ } -> Option A
  4. Quick Check:

    launch is called on CoroutineScope instance [OK]
Hint: Use scope.launch { } to start coroutine inside scope [OK]
Common Mistakes:
  • Swapping order like launch.scope
  • Using run instead of launch
  • Calling launch as a static method
3. What will be the output of this Kotlin code snippet?
import kotlinx.coroutines.*

fun main() = runBlocking {
    val job = launch {
        delay(100)
        println("Task done")
    }
    println("Waiting")
    job.join()
    println("Finished")
}
medium
A. Task done\nWaiting\nFinished
B. Waiting\nTask done\nFinished
C. Waiting\nFinished\nTask done
D. Finished\nWaiting\nTask done

Solution

  1. Step 1: Analyze coroutine launch and delay

    The coroutine launched delays 100ms before printing "Task done". Meanwhile, "Waiting" prints immediately.
  2. Step 2: Understand job.join() effect

    The main coroutine waits for the launched job to finish before printing "Finished".
  3. Final Answer:

    Waiting\nTask done\nFinished -> Option B
  4. Quick Check:

    Output order respects delay and join [OK]
Hint: join() waits for coroutine to finish before continuing [OK]
Common Mistakes:
  • Assuming launched coroutine runs instantly
  • Ignoring delay effect
  • Printing Finished before job completes
4. Identify the problem in this Kotlin coroutine code and choose the correct fix:
fun main() = runBlocking {
    val job = GlobalScope.launch {
        delay(1000)
        println("Done")
    }
    println("Waiting")
}
medium
A. Replace delay(1000) with Thread.sleep(1000)
B. Use CoroutineScope().launch {} instead of GlobalScope.launch
C. Remove runBlocking and use GlobalScope.launch only
D. Add job.join() after println("Waiting")

Solution

  1. Step 1: Identify lifecycle issue with GlobalScope

    Using GlobalScope.launch starts coroutine that may outlive runBlocking, so "Done" may never print.
  2. Step 2: Fix by waiting for job completion

    Calling job.join() ensures main coroutine waits for launched coroutine to finish before exiting.
  3. Final Answer:

    Add job.join() after println("Waiting") -> Option D
  4. Quick Check:

    join() waits for coroutine to complete [OK]
Hint: Always join launched jobs to wait for completion [OK]
Common Mistakes:
  • Ignoring job.join() causing premature exit
  • Replacing delay with blocking sleep
  • Removing runBlocking causing main to exit early
5. Given this Kotlin code using structured concurrency, what will be printed?
import kotlinx.coroutines.*

fun main() = runBlocking {
    try {
        coroutineScope {
            launch {
                delay(100)
                println("Child 1 done")
            }
            launch {
                delay(50)
                throw RuntimeException("Error in Child 2")
            }
        }
    } catch (e: Exception) {
        println("Caught: " + e.message)
    }
    println("End of main")
}
hard
A. Caught: Error in Child 2\nEnd of main
B. Child 1 done\nCaught: Error in Child 2\nEnd of main
C. Caught: Error in Child 2
D. Child 1 done\nEnd of main

Solution

  1. Step 1: Understand coroutineScope behavior on exceptions

    If any child coroutine throws an exception, coroutineScope cancels all children and throws the exception.
  2. Step 2: Analyze timing and cancellation

    Child 2 throws after 50ms, cancelling Child 1 before it prints. The exception is caught and message printed.
  3. Final Answer:

    Caught: Error in Child 2\nEnd of main -> Option A
  4. Quick Check:

    Exception cancels siblings, caught prints message [OK]
Hint: Exception cancels sibling coroutines in coroutineScope [OK]
Common Mistakes:
  • Assuming Child 1 prints despite cancellation
  • Missing try-catch around coroutineScope
  • Thinking exceptions are ignored in coroutineScope
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard