0
0
Kotlinprogramming~20 mins

WithContext for dispatcher switching in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
WithContext Mastery
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 code using withContext?

Consider the following Kotlin coroutine code snippet. What will be printed to the console?

Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    println("Start: ${Thread.currentThread().name}")
    withContext(Dispatchers.IO) {
        println("Inside IO: ${Thread.currentThread().name}")
    }
    println("End: ${Thread.currentThread().name}")
}
A
Start: main
Inside IO: DefaultDispatcher-worker-1
End: main
B
Start: main
Inside IO: main
End: main
C
Start: main
Inside IO: IO-thread
End: main
D
Start: main
Inside IO: DefaultDispatcher-worker-1
End: DefaultDispatcher-worker-1
Attempts:
2 left
💡 Hint

Remember that withContext(Dispatchers.IO) switches the coroutine context to a background thread pool.

Predict Output
intermediate
2:00remaining
What happens if you use withContext with the same dispatcher?

What will be the output of this Kotlin coroutine code?

Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    println("Before withContext: ${Thread.currentThread().name}")
    withContext(Dispatchers.Default) {
        println("Inside withContext: ${Thread.currentThread().name}")
    }
    println("After withContext: ${Thread.currentThread().name}")
}
A
Before withContext: main
Inside withContext: DefaultDispatcher-worker-1
After withContext: main
B
Before withContext: main
Inside withContext: main
After withContext: main
C
Before withContext: DefaultDispatcher-worker-1
Inside withContext: DefaultDispatcher-worker-1
After withContext: DefaultDispatcher-worker-1
D
Before withContext: main
Inside withContext: DefaultDispatcher-worker-1
After withContext: DefaultDispatcher-worker-1
Attempts:
2 left
💡 Hint

Think about how runBlocking and withContext interact with dispatchers.

🔧 Debug
advanced
2:00remaining
Identify the error in this withContext usage

What error will this Kotlin code produce when run?

Kotlin
import kotlinx.coroutines.*

fun main() {
    withContext(Dispatchers.IO) {
        println("Running in IO dispatcher")
    }
}
AUnresolved reference: withContext
BIllegalStateException: This job has not been started
CSuspend function 'withContext' should be called only from a coroutine or another suspend function
DNo error, prints 'Running in IO dispatcher'
Attempts:
2 left
💡 Hint

Check if withContext is called inside a coroutine or suspend function.

Predict Output
advanced
2:00remaining
What is the output of nested withContext calls with different dispatchers?

Analyze the output of this Kotlin coroutine code with nested withContext calls:

Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    println("Start: ${Thread.currentThread().name}")
    withContext(Dispatchers.Default) {
        println("Default: ${Thread.currentThread().name}")
        withContext(Dispatchers.IO) {
            println("IO: ${Thread.currentThread().name}")
        }
        println("Back to Default: ${Thread.currentThread().name}")
    }
    println("End: ${Thread.currentThread().name}")
}
A
Start: main
Default: DefaultDispatcher-worker-1
IO: IO-thread
Back to Default: DefaultDispatcher-worker-1
End: main
B
Start: main
Default: DefaultDispatcher-worker-1
IO: DefaultDispatcher-worker-1
Back to Default: DefaultDispatcher-worker-1
End: main
C
Start: main
Default: main
IO: DefaultDispatcher-worker-1
Back to Default: main
End: main
D
Start: main
Default: DefaultDispatcher-worker-1
IO: DefaultDispatcher-worker-2
Back to Default: DefaultDispatcher-worker-1
End: main
Attempts:
2 left
💡 Hint

Each withContext switches to the specified dispatcher thread pool.

🧠 Conceptual
expert
3:00remaining
Why use withContext instead of launch for dispatcher switching?

Which statement best explains why withContext is preferred over launch when switching dispatchers for a block of code?

A<code>launch</code> switches the coroutine context and waits for the block to complete, while <code>withContext</code> starts a new coroutine that runs concurrently.
B<code>withContext</code> switches the coroutine context and waits for the block to complete, preserving sequential execution, while <code>launch</code> starts a new coroutine that runs concurrently.
C<code>withContext</code> creates a new thread, but <code>launch</code> reuses the current thread without switching context.
D<code>launch</code> is used only for UI threads, while <code>withContext</code> is for background threads.
Attempts:
2 left
💡 Hint

Think about blocking vs non-blocking behavior and sequential code flow.