0
0
Kotlinprogramming~20 mins

RunBlocking for bridging in Kotlin - Practice Problems & Coding Challenges

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

fun main() = runBlocking {
    launch {
        delay(100)
        println("Inside launch")
    }
    println("Before delay")
    delay(200)
    println("After delay")
}
ABefore delay\nAfter delay\nInside launch
BBefore delay\nInside launch\nAfter delay
CInside launch\nBefore delay\nAfter delay
DAfter delay\nBefore delay\nInside launch
Attempts:
2 left
💡 Hint
Remember that runBlocking waits for all child coroutines to complete before finishing.
Predict Output
intermediate
2:00remaining
What error does this Kotlin code produce?
What error will this Kotlin code produce when run?
Kotlin
import kotlinx.coroutines.*

fun main() {
    runBlocking {
        launch {
            delay(100)
            println("Hello")
        }
    }
    println("Done")
}
ANo error, prints Hello then Done
BNo error, prints Done then Hello
CCompilation error: runBlocking must be called from a coroutine or suspend function
DRuntime error: IllegalStateException
Attempts:
2 left
💡 Hint
Check the order of execution inside and outside runBlocking.
🧠 Conceptual
advanced
1:30remaining
Why use runBlocking in Kotlin coroutines?
Which of the following best explains why runBlocking is used in Kotlin coroutines?
ATo bridge regular blocking code and suspend functions by blocking the current thread until coroutine completes
BTo create a coroutine scope that automatically cancels all child coroutines on error
CTo launch multiple coroutines concurrently without waiting for their completion
DTo start a coroutine that runs asynchronously without blocking the current thread
Attempts:
2 left
💡 Hint
Think about how runBlocking helps when calling suspend functions from normal code.
🔧 Debug
advanced
2:00remaining
Identify the problem in this Kotlin coroutine code using runBlocking
What is the main problem with this Kotlin code snippet?
Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    val job = launch {
        delay(1000)
        println("Task done")
    }
    job.cancel()
    println("Cancelled")
}
AThe println("Cancelled") is never executed
BThe code throws a CancellationException at runtime
CThe coroutine is cancelled immediately, so "Task done" is never printed
DThe delay inside launch causes a compilation error
Attempts:
2 left
💡 Hint
Consider what happens when you cancel a coroutine before it finishes.
🚀 Application
expert
2:30remaining
How many lines are printed by this Kotlin runBlocking code?
How many lines will be printed when running this Kotlin code?
Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    val jobs = List(3) { i ->
        launch {
            delay(100L * i)
            println("Coroutine $i done")
        }
    }
    jobs.forEach { it.join() }
    println("All done")
}
A3
B1
C0
D4
Attempts:
2 left
💡 Hint
Count all printed lines including the final one after all coroutines finish.