0
0
Android Kotlinmobile~20 mins

StateFlow for reactive state in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
StateFlow Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What happens when StateFlow value changes?
Consider a StateFlow holding an integer count. When the count updates, what will the UI displaying this count do?
Android Kotlin
val countFlow = MutableStateFlow(0)
// UI collects countFlow and shows count
countFlow.value = 5
AThe UI automatically updates to show 5
BThe UI shows 0 and never updates
CThe UI crashes because StateFlow is immutable
DThe UI updates only if manually refreshed
Attempts:
2 left
💡 Hint
StateFlow emits new values to collectors automatically.
lifecycle
intermediate
2:00remaining
StateFlow collection and lifecycle
In an Android app, what happens if you collect a StateFlow in a ViewModel but the UI lifecycle is destroyed?
Android Kotlin
val stateFlow = MutableStateFlow("Hello")
// UI collects stateFlow in lifecycleScope
// UI is destroyed
AStateFlow stops emitting values
BCollection continues and leaks memory
CApp crashes due to lifecycle mismatch
DCollection stops automatically when UI lifecycle is destroyed
Attempts:
2 left
💡 Hint
Lifecycle-aware scopes stop coroutines when lifecycle ends.
📝 Syntax
advanced
2:00remaining
Correct syntax to expose StateFlow from ViewModel
Which option correctly exposes a read-only StateFlow from a ViewModel?
Android Kotlin
private val _state = MutableStateFlow(0)
// expose state
Aval state = _state as StateFlow<Int>
Bval state = _state.value
Cval state: StateFlow<Int> get() = _state
Dval state: MutableStateFlow<Int> = _state
Attempts:
2 left
💡 Hint
Expose StateFlow as an interface, not mutable type.
🔧 Debug
advanced
2:00remaining
Why does UI not update when StateFlow value changes?
Given this code, why does the UI not update when _count.value changes? class MyViewModel : ViewModel() { private val _count = MutableStateFlow(0) val count: StateFlow = _count fun increment() { _count.value += 1 } } // UI collects count but never updates
AStateFlow requires emit() instead of value assignment
BUI is not collecting count in a coroutine
C_count.value += 1 does not update StateFlow value
DViewModel is not initialized properly
Attempts:
2 left
💡 Hint
StateFlow emits only when collected in a coroutine.
🧠 Conceptual
expert
2:00remaining
StateFlow vs SharedFlow for UI state
Which statement best explains why StateFlow is preferred over SharedFlow for holding UI state?
AStateFlow always holds the latest value and replays it to new collectors, SharedFlow does not by default
BSharedFlow is slower and cannot be collected in UI
CStateFlow supports multiple values at once, SharedFlow supports only one
DSharedFlow requires manual reset of values, StateFlow resets automatically
Attempts:
2 left
💡 Hint
Think about replay behavior and state retention.