Bird
Raised Fist0
Kotlinprogramming~20 mins

RunBlocking for bridging in Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of runBlocking in Kotlin coroutines?
easy
A. To create a new thread for running coroutines
B. To launch a coroutine without blocking the current thread
C. To cancel running coroutines immediately
D. To bridge blocking code and coroutine code by blocking the current thread until coroutine completes

Solution

  1. Step 1: Understand the role of runBlocking

    runBlocking is designed to bridge blocking and coroutine code by blocking the current thread until the coroutine inside it finishes.
  2. Step 2: Compare with other coroutine builders

    Unlike launch or async, runBlocking blocks the thread, making it suitable for main functions or tests but not for UI or performance-critical code.
  3. Final Answer:

    To bridge blocking code and coroutine code by blocking the current thread until coroutine completes -> Option D
  4. Quick Check:

    runBlocking blocks thread until coroutine finishes = C [OK]
Hint: runBlocking blocks thread until coroutine finishes [OK]
Common Mistakes:
  • Thinking runBlocking launches coroutine without blocking
  • Confusing runBlocking with launch or async
  • Assuming runBlocking creates a new thread
2. Which of the following is the correct syntax to use runBlocking in Kotlin?
easy
A. runBlocking[ println("Hello") ]
B. runBlocking() -> println("Hello")
C. runBlocking { println("Hello") }
D. runBlocking => { println("Hello") }

Solution

  1. Step 1: Recall runBlocking syntax

    runBlocking is a function that takes a lambda block inside curly braces to run coroutine code.
  2. Step 2: Identify correct syntax

    runBlocking { println("Hello") } uses curly braces correctly with a lambda block. Other options use invalid syntax like parentheses with arrow or square brackets.
  3. Final Answer:

    runBlocking { println("Hello") } -> Option C
  4. Quick Check:

    runBlocking uses curly braces for lambda = A [OK]
Hint: runBlocking uses curly braces for coroutine block [OK]
Common Mistakes:
  • Using parentheses instead of curly braces
  • Using arrow or square brackets incorrectly
  • Missing lambda block inside runBlocking
3. What will be the output of the following Kotlin code?
import kotlinx.coroutines.*
fun main() = runBlocking {
    launch {
        delay(100)
        println("World")
    }
    println("Hello")
}
medium
A. Hello\nWorld
B. World\nHello
C. Hello
D. World

Solution

  1. Step 1: Understand runBlocking and launch behavior

    runBlocking blocks the main thread until all coroutines inside complete. The launch coroutine delays 100ms before printing "World".
  2. Step 2: Trace the print statements

    "Hello" is printed immediately in runBlocking, then after 100ms delay, "World" is printed by the launched coroutine.
  3. Final Answer:

    Hello World -> Option A
  4. Quick Check:

    runBlocking waits; launch delays; prints Hello then World = D [OK]
Hint: runBlocking waits; launch delays; prints Hello then World [OK]
Common Mistakes:
  • Assuming launch runs immediately before println("Hello")
  • Ignoring delay effect
  • Thinking runBlocking does not wait for launch
4. Identify the error in this Kotlin code using runBlocking:
fun main() {
    runBlocking {
        delay(1000)
        println("Done")
    }
}
medium
A. runBlocking cannot be used inside main function
B. Missing import for delay function
C. delay cannot be called inside runBlocking
D. println should be outside runBlocking

Solution

  1. Step 1: Check for necessary imports

    The delay function is part of kotlinx.coroutines package and requires import kotlinx.coroutines.delay.
  2. Step 2: Verify usage of runBlocking and delay

    runBlocking can be used inside main, and delay is allowed inside runBlocking coroutine scope, so no error there.
  3. Final Answer:

    Missing import for delay function -> Option B
  4. Quick Check:

    delay needs import kotlinx.coroutines.delay = A [OK]
Hint: Import kotlinx.coroutines.delay to use delay inside runBlocking [OK]
Common Mistakes:
  • Thinking runBlocking can't be used in main
  • Assuming delay is a blocking call
  • Placing println outside runBlocking unnecessarily
5. You want to write a Kotlin main function that prints numbers 1 to 3 with a 500ms delay between each, using runBlocking. Which code correctly achieves this?
hard
A. fun main() = runBlocking { for (i in 1..3) { delay(500) println(i) } }
B. fun main() { for (i in 1..3) { delay(500) println(i) } }
C. fun main() = runBlocking { (1..3).forEach { launch { delay(500); println(it) } } }
D. fun main() { (1..3).forEach { Thread.sleep(500) println(it) } }

Solution

  1. Step 1: Understand runBlocking usage for sequential delay

    fun main() = runBlocking { for (i in 1..3) { delay(500) println(i) } } uses runBlocking once and delays inside a for loop, printing numbers sequentially with 500ms delay.
  2. Step 2: Analyze other options

    fun main() { for (i in 1..3) { delay(500) println(i) } } lacks a coroutine scope, so delay() will not compile. fun main() = runBlocking { (1..3).forEach { launch { delay(500); println(it) } } } launches coroutines concurrently, printing numbers out of order. fun main() { (1..3).forEach { Thread.sleep(500) println(it) } } uses Thread.sleep, which blocks thread but not coroutine style.
  3. Final Answer:

    fun main() = runBlocking { for (i in 1..3) { delay(500) println(i) } } -> Option A
  4. Quick Check:

    runBlocking with sequential delay in loop = B [OK]
Hint: Use runBlocking once with delay inside loop for sequential prints [OK]
Common Mistakes:
  • Using runBlocking inside loop repeatedly
  • Launching coroutines without waiting
  • Using Thread.sleep instead of delay in coroutines
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