0
0
Android Kotlinmobile~20 mins

LiveData basics in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
LiveData Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
LiveData Observer Update Behavior
Given this LiveData and observer setup in an Android app, what will be the output on the UI after calling liveData.value = "Hello"?
Android Kotlin
val liveData = MutableLiveData<String>()
liveData.observe(this) { value ->
  textView.text = value
}
liveData.value = "Hello"
AThe TextView will display "Hello" immediately.
BThe TextView will remain empty until the next UI refresh.
CThe app will crash because LiveData cannot be updated directly.
DThe TextView will display "null" because the initial value is missing.
Attempts:
2 left
💡 Hint
LiveData notifies observers immediately when the value changes.
lifecycle
intermediate
2:00remaining
LiveData and Lifecycle Owner
What happens if you observe LiveData with a lifecycle owner that is already in the DESTROYED state?
Android Kotlin
liveData.observe(lifecycleOwner) { value ->
  // update UI
}
AThe observer will receive updates until the lifecycle is destroyed.
BThe observer will never receive updates because the lifecycle is destroyed.
CThe app will throw an IllegalStateException immediately.
DThe observer will receive updates even after the lifecycle is destroyed.
Attempts:
2 left
💡 Hint
LiveData respects the lifecycle state of its observers.
📝 Syntax
advanced
2:00remaining
Correct LiveData Declaration
Which of the following Kotlin code snippets correctly declares a MutableLiveData of integers and sets its initial value to 10?
Aval liveData = MutableLiveData<Int>(10)
Bval liveData = MutableLiveData<Int>.value(10)
Cval liveData = MutableLiveData<Int> { 10 }
Dval liveData = MutableLiveData<Int>().apply { value = 10 }
Attempts:
2 left
💡 Hint
MutableLiveData initial value is set via the value property, not constructor.
🔧 Debug
advanced
2:00remaining
LiveData Observer Not Updating UI
Why does this LiveData observer not update the TextView when the value changes?
Android Kotlin
val liveData = MutableLiveData<String>()
liveData.observe(this) { value ->
  textView.text = value
}
// Later in code
liveData.postValue("Update")
ApostValue does not notify observers; only setValue does.
BThe observer is not active because the lifecycle owner is paused.
CpostValue updates happen on background thread; UI update must be on main thread.
DThe TextView is null, so update fails silently.
Attempts:
2 left
💡 Hint
postValue schedules update on main thread asynchronously.
🧠 Conceptual
expert
3:00remaining
LiveData Behavior on Configuration Change
After a device rotation (configuration change), what happens to the LiveData observers and their data in a typical Android app using ViewModel and LiveData?
AThe ViewModel survives, LiveData keeps its data, and new observers receive the last value immediately.
BThe ViewModel and LiveData are recreated, so data is lost and observers start empty.
CLiveData data is saved but observers are not recreated automatically after rotation.
DObservers remain the same instance and continue receiving updates without interruption.
Attempts:
2 left
💡 Hint
ViewModel survives configuration changes to keep data.