0
0
Android Kotlinmobile~20 mins

State in ViewModel in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ViewModel State Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What is the output of this ViewModel state update?
Consider this ViewModel code that holds a counter state. What will be the value of counter after calling increment() twice?
Android Kotlin
class CounterViewModel : ViewModel() {
  private val _counter = MutableLiveData(0)
  val counter: LiveData<Int> = _counter

  fun increment() {
    _counter.value = (_counter.value ?: 0) + 1
  }
}

// After calling increment() twice:
Acounter.value == 1
Bcounter.value == 2
Ccounter.value == 0
Dcounter.value == null
Attempts:
2 left
💡 Hint
Each call to increment adds 1 to the current value stored in _counter.
lifecycle
intermediate
1:30remaining
When is ViewModel state preserved?
In Android, when does the ViewModel keep its state intact?
AWhen the user closes the app from recent apps
BWhen the app is killed by the system
CDuring configuration changes like screen rotation
DWhen the device is restarted
Attempts:
2 left
💡 Hint
Think about what happens to Activity and ViewModel during rotation.
📝 Syntax
advanced
2:00remaining
Which option correctly exposes immutable state from ViewModel?
Given a MutableStateFlow in ViewModel, which option correctly exposes it as immutable to the UI?
Android Kotlin
private val _uiState = MutableStateFlow(0)
// Expose immutable state here:
Aval uiState: StateFlow<Int> = _uiState
Bval uiState: MutableStateFlow<Int> = _uiState
Cval uiState: Flow<Int> = _uiState.asStateFlow()
Dval uiState: StateFlow<Int> = _uiState.asStateFlow()
Attempts:
2 left
💡 Hint
Use the correct method to expose a read-only StateFlow.
🔧 Debug
advanced
2:30remaining
Why does UI not update after ViewModel state change?
This ViewModel updates a LiveData value, but the UI does not reflect the change. What is the likely cause?
Android Kotlin
class MyViewModel : ViewModel() {
  val data = MutableLiveData("Hello")

  fun update() {
    data.value = "World"
  }
}

// UI observes data but does not update after update() call.
AMutableLiveData.value was updated from a background thread
BUI is not observing LiveData on the main thread
CLiveData observer was not registered before update() call
DLiveData was declared as val instead of var
Attempts:
2 left
💡 Hint
LiveData.value must be updated on the main thread.
🧠 Conceptual
expert
3:00remaining
What is the main benefit of using StateFlow in ViewModel over LiveData?
Choose the best explanation for why StateFlow might be preferred over LiveData for state management in ViewModel.
AStateFlow supports coroutines and provides a consistent API for reactive streams
BLiveData cannot be observed from fragments
CStateFlow automatically saves state across process death
DLiveData requires manual lifecycle management
Attempts:
2 left
💡 Hint
Think about Kotlin coroutines integration and reactive programming.