Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to switch the flow to the IO dispatcher.
Kotlin
flowOf(1, 2, 3).[1](Dispatchers.IO)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using collect instead of flowOn.
Using map or filter which do not change dispatcher.
✗ Incorrect
The flowOn operator changes the dispatcher where the flow is executed.
2fill in blank
mediumComplete the code to collect the flow on the Main dispatcher.
Kotlin
flowOf("A", "B", "C").flowOn(Dispatchers.IO).[1] { value -> println(value) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using map or filter which transform but do not collect.
Using launchIn which requires a CoroutineScope.
✗ Incorrect
collect is used to receive the emitted values from the flow.
3fill in blank
hardFix the error in the code to correctly change the dispatcher.
Kotlin
val flow = flow {
emit(10)
}.[1](Dispatchers.Default)
flow.collect { println(it) } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using collect instead of flowOn to change dispatcher.
Using launchIn without a CoroutineScope.
✗ Incorrect
flowOn must be used to change the dispatcher of the flow emissions.
4fill in blank
hardFill both blanks to create a flow that emits numbers on IO dispatcher and collects on Main dispatcher.
Kotlin
flow {
emit(1)
emit(2)
}.[1](Dispatchers.IO).[2] { value -> println(value) } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using map or filter instead of collect to receive values.
Using collect before flowOn.
✗ Incorrect
flowOn changes the dispatcher for emission, collect receives the values.
5fill in blank
hardFill all three blanks to create a flow that emits strings on Default dispatcher, maps them to uppercase, and collects on IO dispatcher.
Kotlin
flowOf("a", "b", "c").[1](Dispatchers.Default).[2] { it.[3]() }.collect { println(it) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using filter instead of map for transformation.
Using lowercase instead of uppercase.
✗ Incorrect
flowOn changes dispatcher, map transforms each item, uppercase converts string to uppercase.