Challenge - 5 Problems
Flow Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What is the output of this Flow collection?
Consider this Kotlin Flow code that emits numbers and collects them. What will be printed?
Android Kotlin
import kotlinx.coroutines.flow.* import kotlinx.coroutines.runBlocking fun main() = runBlocking { val flow = flow { emit(1) emit(2) emit(3) } flow.collect { value -> println(value) } }
Attempts:
2 left
💡 Hint
Remember that Flow emits values in the order they are emitted inside the flow builder.
✗ Incorrect
The flow emits values 1, 2, and 3 sequentially. The collect function prints each value on its own line.
🧠 Conceptual
intermediate1:30remaining
What does the 'flowOn' operator do in Kotlin Flow?
Choose the correct description of the 'flowOn' operator in Kotlin Flow.
Attempts:
2 left
💡 Hint
Think about where the code inside the flow builder runs.
✗ Incorrect
'flowOn' changes the coroutine context where the upstream flow is executed (emission happens). Collection context remains unchanged.
❓ lifecycle
advanced2:00remaining
What happens if you collect a Flow multiple times?
Given a cold Flow in Kotlin, what is the behavior when you collect it multiple times?
Attempts:
2 left
💡 Hint
Cold flows start fresh for each collector.
✗ Incorrect
Cold flows start their emission anew for each collector. They do not share emissions unless converted to hot flows.
📝 Syntax
advanced2:00remaining
What error does this Flow code produce?
Analyze this Kotlin Flow code snippet. What error will it cause?
Android Kotlin
import kotlinx.coroutines.flow.* import kotlinx.coroutines.runBlocking fun main() = runBlocking { val flow = flow { emit(1) emit(2) emit(3) } flow.collect { value -> if (value == 2) throw Exception("Error at 2") println(value) } }
Attempts:
2 left
💡 Hint
Throwing inside collect will propagate the exception.
✗ Incorrect
The exception is thrown when value equals 2 during collection, stopping the flow and propagating the error.
expert
2:30remaining
How to cancel a Flow collection properly in Android?
In an Android app using Kotlin Flow, which approach correctly cancels the flow collection when the lifecycle owner is destroyed?
Attempts:
2 left
💡 Hint
Think about lifecycle-aware coroutine scopes in Android.
✗ Incorrect
Using lifecycleScope.launch ties the coroutine to the lifecycle owner, automatically cancelling collection on destruction.