Bird
Raised Fist0
Kotlinprogramming~20 mins

Dispatchers.Main, IO, Default behavior in Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Coroutine Dispatcher 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 dispatcher code?

Consider this Kotlin coroutine code snippet. What will it print?

Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    launch(Dispatchers.Main) {
        println("Running on Main: ${Thread.currentThread().name}")
    }
    launch(Dispatchers.IO) {
        println("Running on IO: ${Thread.currentThread().name}")
    }
    launch(Dispatchers.Default) {
        println("Running on Default: ${Thread.currentThread().name}")
    }
}
A
Running on Main: DefaultDispatcher-worker-1
Running on IO: main
Running on Default: main
B
Running on Main: DefaultDispatcher-worker-1
Running on IO: DefaultDispatcher-worker-2
Running on Default: DefaultDispatcher-worker-3
C
Running on Main: main
Running on IO: DefaultDispatcher-worker-1
Running on Default: DefaultDispatcher-worker-2
D
Running on Main: main
Running on IO: main
Running on Default: main
Attempts:
2 left
💡 Hint

Remember that Dispatchers.Main runs on the main thread, while Dispatchers.IO and Dispatchers.Default use background threads.

❓ Predict Output
intermediate
2:00remaining
What happens if you launch a coroutine with Dispatchers.Main in a non-Android JVM?

What will happen when running this Kotlin coroutine code on a plain JVM (no Android)?

Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    launch(Dispatchers.Main) {
        println("Hello from Main")
    }
}
AThrows an IllegalStateException because Dispatchers.Main is not available.
BRuns on a background thread named DefaultDispatcher-worker-1.
CPrints "Hello from Main" successfully on the main thread.
DDeadlocks and never prints anything.
Attempts:
2 left
💡 Hint

Think about whether Dispatchers.Main is supported outside Android or JavaFX environments.

❓ Predict Output
advanced
2:00remaining
What is the output of this mixed dispatcher coroutine code?

Analyze the output of this Kotlin coroutine code snippet:

Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    val job = launch(Dispatchers.Default) {
        println("Default start: ${Thread.currentThread().name}")
        withContext(Dispatchers.IO) {
            println("Inside IO: ${Thread.currentThread().name}")
        }
        println("Back to Default: ${Thread.currentThread().name}")
    }
    job.join()
}
A
Default start: DefaultDispatcher-worker-1
Inside IO: DefaultDispatcher-worker-1
Back to Default: DefaultDispatcher-worker-1
B
Default start: DefaultDispatcher-worker-1
Inside IO: main
Back to Default: DefaultDispatcher-worker-1
C
Default start: main
Inside IO: DefaultDispatcher-worker-1
Back to Default: main
D
Default start: DefaultDispatcher-worker-1
Inside IO: DefaultDispatcher-worker-2
Back to Default: DefaultDispatcher-worker-1
Attempts:
2 left
💡 Hint

Remember that withContext switches the coroutine context temporarily.

❓ Predict Output
advanced
2:00remaining
What error does this code raise when using Dispatchers.Main improperly?

What error will this Kotlin code produce?

Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    launch(Dispatchers.Main.immediate) {
        println("Running immediate on Main")
    }
}
AIllegalStateException: Module with the Main dispatcher is missing.
BNo error, prints "Running immediate on Main" on main thread.
CRuntimeException: Immediate dispatcher not supported on this platform.
DDeadlock because immediate dispatcher tries to run on main thread synchronously.
Attempts:
2 left
💡 Hint

Consider if the Main dispatcher module is included in your project.

🧠 Conceptual
expert
2:00remaining
Which dispatcher is best suited for CPU-intensive tasks and why?

Choose the best dispatcher for CPU-heavy computations and explain why.

ADispatchers.IO because it uses a large thread pool optimized for blocking IO tasks.
BDispatchers.Default because it uses a shared pool of threads optimized for CPU-intensive work.
CDispatchers.Main because it runs on the main thread and ensures UI responsiveness.
DA new single-threaded dispatcher to avoid thread switching overhead.
Attempts:
2 left
💡 Hint

Think about thread pool size and task type for CPU-bound work.

Practice

(1/5)
1. Which Dispatcher should you use in Kotlin when updating the user interface (UI)?
easy
A. Dispatchers.Main
B. Dispatchers.IO
C. Dispatchers.Default
D. Dispatchers.Unconfined

Solution

  1. Step 1: Understand UI thread requirements

    UI updates must happen on the main thread to avoid crashes or freezes.
  2. Step 2: Match Dispatcher to UI work

    Dispatchers.Main is designed for UI tasks on the main thread.
  3. Final Answer:

    Dispatchers.Main -> Option A
  4. Quick Check:

    UI work = Dispatchers.Main [OK]
Hint: UI updates always use Dispatchers.Main [OK]
Common Mistakes:
  • Using Dispatchers.IO for UI updates
  • Using Dispatchers.Default for UI work
  • Confusing Dispatchers.Unconfined with Main
2. Which of the following is the correct way to launch a coroutine on the IO dispatcher in Kotlin?
easy
A. CoroutineScope.launch(Dispatchers.IO) { /* IO work */ }
B. CoroutineScope(Dispatchers.Main).launch { /* IO work */ }
C. CoroutineScope(Dispatchers.Default).launch { /* IO work */ }
D. CoroutineScope(Dispatchers.IO).launch { /* IO work */ }

Solution

  1. Step 1: Identify correct CoroutineScope usage

    To launch a coroutine on a specific dispatcher, create CoroutineScope with that dispatcher.
  2. Step 2: Check syntax correctness

    CoroutineScope(Dispatchers.IO).launch { ... } is correct syntax for IO dispatcher.
  3. Final Answer:

    CoroutineScope(Dispatchers.IO).launch { /* IO work */ } -> Option D
  4. Quick Check:

    IO dispatcher launch = CoroutineScope(Dispatchers.IO).launch { /* IO work */ } [OK]
Hint: Use CoroutineScope(Dispatchers.IO).launch for IO tasks [OK]
Common Mistakes:
  • Using Dispatchers.Main for IO tasks
  • Incorrect CoroutineScope.launch syntax
  • Using Dispatchers.Default for IO tasks
3. What will be the output of the following Kotlin code snippet?
import kotlinx.coroutines.*

fun main() = runBlocking {
    launch(Dispatchers.Default) {
        println("Running on: " + Thread.currentThread().name)
    }.join()
}
medium
A. Running on: DefaultDispatcher-worker-1
B. Running on: main
C. Running on: IO-thread-1
D. Running on: Unconfined

Solution

  1. Step 1: Identify the dispatcher used

    The coroutine is launched with Dispatchers.Default, which uses a shared background thread pool.
  2. Step 2: Understand thread naming

    Threads in Default dispatcher are named like "DefaultDispatcher-worker-1".
  3. Final Answer:

    Running on: DefaultDispatcher-worker-1 -> Option A
  4. Quick Check:

    Dispatchers.Default runs on DefaultDispatcher-worker threads [OK]
Hint: Default dispatcher uses background worker threads [OK]
Common Mistakes:
  • Assuming it runs on main thread
  • Confusing IO thread names with Default
  • Expecting Unconfined dispatcher output
4. Consider this Kotlin coroutine code snippet:
CoroutineScope(Dispatchers.Main).launch {
    withContext(Dispatchers.IO) {
        // Perform file read
    }
    println("File read complete")
}
What is the main issue if this code is run in an Android app?
medium
A. Using Dispatchers.Main inside withContext causes a crash
B. File read should be done on Dispatchers.Default, not IO
C. No issue; this is the correct way to switch dispatchers
D. println runs on IO dispatcher causing UI freeze

Solution

  1. Step 1: Analyze dispatcher switching

    The code launches on Main dispatcher, then switches to IO for file reading.
  2. Step 2: Confirm correct usage

    Switching to Dispatchers.IO for file I/O and back to Main for UI is correct practice.
  3. Final Answer:

    No issue; this is the correct way to switch dispatchers -> Option C
  4. Quick Check:

    Dispatcher switching Main ↔ IO is correct [OK]
Hint: Switch to IO for I/O, back to Main for UI [OK]
Common Mistakes:
  • Thinking Dispatchers.Default is for I/O
  • Believing println runs on IO dispatcher
  • Assuming dispatcher switch causes crash
5. You want to perform a CPU-intensive calculation without blocking the UI in a Kotlin Android app. Which dispatcher should you use and why?

Select the best option.

hard
A. Dispatchers.Main, because it runs on the main thread
B. Dispatchers.Default, because it is optimized for CPU-heavy work
C. Dispatchers.Unconfined, because it runs immediately
D. Dispatchers.IO, because it handles background tasks

Solution

  1. Step 1: Identify task type

    CPU-intensive tasks need background threads to avoid freezing UI.
  2. Step 2: Match dispatcher to CPU work

    Dispatchers.Default is designed for CPU-heavy tasks using a shared thread pool.
  3. Final Answer:

    Dispatchers.Default, because it is optimized for CPU-heavy work -> Option B
  4. Quick Check:

    CPU tasks = Dispatchers.Default [OK]
Hint: Use Default dispatcher for CPU-heavy tasks [OK]
Common Mistakes:
  • Using Main dispatcher for CPU work
  • Confusing IO dispatcher with CPU dispatcher
  • Using Unconfined dispatcher for heavy tasks
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