0
0
Kotlinprogramming~20 mins

FlowOn for changing dispatcher in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FlowOn 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 Kotlin Flow code with flowOn?

Consider the following Kotlin code using flowOn. What will be printed?

Kotlin
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*

fun main() = runBlocking {
    flow {
        println("Emitting on thread: ${Thread.currentThread().name}")
        emit(1)
    }
    .flowOn(Dispatchers.Default)
    .collect {
        println("Collected $it on thread: ${Thread.currentThread().name}")
    }
}
A
Emitting on thread: DefaultDispatcher-worker-1
Collected 1 on thread: DefaultDispatcher-worker-1
B
Emitting on thread: main
Collected 1 on thread: DefaultDispatcher-worker-1
C
Emitting on thread: main
Collected 1 on thread: main
D
Emitting on thread: DefaultDispatcher-worker-1
Collected 1 on thread: main
Attempts:
2 left
💡 Hint

Remember that flowOn changes the dispatcher of the upstream flow emissions.

🧠 Conceptual
intermediate
1:30remaining
Which statement about flowOn is correct?

Choose the correct statement about flowOn in Kotlin Flow.

A<code>flowOn</code> delays the flow emissions by switching dispatcher after collection.
B<code>flowOn</code> cancels the flow if the dispatcher is not specified.
C<code>flowOn</code> changes the dispatcher only for the upstream flow emissions.
D<code>flowOn</code> changes the dispatcher of the entire flow including collection.
Attempts:
2 left
💡 Hint

Think about which part of the flow flowOn affects.

🔧 Debug
advanced
2:00remaining
Why does this flowOn usage cause a runtime error?

Examine the code below. Why does it throw an exception at runtime?

Kotlin
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*

fun main() = runBlocking {
    flow {
        emit(1)
    }
    .flowOn(Dispatchers.IO)
    .collect {
        delay(1000)
        println(it)
    }
}
ABecause delay cannot be called inside collect without a suspend function.
BBecause delay inside collect is allowed, no runtime error occurs.
CBecause flowOn cannot be used with Dispatchers.IO.
DBecause collect is blocking and delays the flow emission causing deadlock.
Attempts:
2 left
💡 Hint

Recall that collect is a suspend function and can call delay.

Predict Output
advanced
2:30remaining
What is the output of this flow with multiple flowOn calls?

What will this Kotlin program print?

Kotlin
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*

fun main() = runBlocking {
    flow {
        println("Start emitting on: ${Thread.currentThread().name}")
        emit(1)
    }
    .flowOn(Dispatchers.IO)
    .map {
        println("Mapping $it on: ${Thread.currentThread().name}")
        it * 2
    }
    .flowOn(Dispatchers.Default)
    .collect {
        println("Collected $it on: ${Thread.currentThread().name}")
    }
}
A
Start emitting on: IO-thread
Mapping 1 on: DefaultDispatcher-worker-1
Collected 2 on: main
B
Start emitting on: DefaultDispatcher-worker-1
Mapping 1 on: DefaultDispatcher-worker-2
Collected 2 on: main
C
Start emitting on: main
Mapping 1 on: IO-thread
Collected 2 on: DefaultDispatcher-worker-1
D
Start emitting on: IO-thread
Mapping 1 on: IO-thread
Collected 2 on: main
Attempts:
2 left
💡 Hint

Remember each flowOn affects upstream operators only.

🧠 Conceptual
expert
2:00remaining
How does flowOn affect upstream and downstream operators in a flow chain?

Given a flow chain with multiple operators and flowOn calls, which statement is true?

A<code>flowOn</code> changes the dispatcher of all operators upstream from its position.
B<code>flowOn</code> changes the dispatcher of all operators downstream from its position.
C<code>flowOn</code> changes the dispatcher of the operator immediately before it only.
D<code>flowOn</code> changes the dispatcher of the entire flow regardless of position.
Attempts:
2 left
💡 Hint

Think about the direction of data flow and which operators flowOn affects.