0
0
Android Kotlinmobile~20 mins

MVVM pattern in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MVVM Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding the role of ViewModel in MVVM
In the MVVM pattern, what is the primary responsibility of the ViewModel?
ATo hold and manage UI-related data and handle business logic without referencing the View directly
BTo directly update the UI components and handle user input events
CTo manage database operations and network calls exclusively
DTo act as a data model representing the app's data structure
Attempts:
2 left
💡 Hint
Think about which component acts as a bridge between UI and data without knowing UI details.
ui_behavior
intermediate
2:00remaining
LiveData observation in MVVM
Given a ViewModel exposing LiveData called 'status', what happens in the View when the LiveData value changes?
Android Kotlin
viewModel.status.observe(viewLifecycleOwner) { newStatus ->
  textView.text = newStatus
}
AThe textView's text updates only if manually refreshed by the user
BThe textView's text updates only once when the View is created
CThe textView's text never updates because LiveData does not notify observers
DThe textView's text updates automatically to the new status value whenever it changes
Attempts:
2 left
💡 Hint
LiveData notifies observers when data changes.
lifecycle
advanced
2:00remaining
ViewModel lifecycle and data retention
What happens to the ViewModel instance when the associated Activity is recreated due to a configuration change (e.g., device rotation)?
AThe ViewModel is destroyed immediately and recreated with the Activity
BA new ViewModel instance is created, losing previous data
CThe same ViewModel instance is retained and reused, preserving its data
DThe ViewModel instance is shared across all Activities in the app
Attempts:
2 left
💡 Hint
ViewModel is designed to survive configuration changes.
navigation
advanced
2:00remaining
Sharing data between fragments using ViewModel
How can two fragments in the same Activity share data using MVVM pattern?
AEach fragment creates its own ViewModel instance and communicates via interfaces
BBoth fragments use the same Activity-scoped ViewModel instance to share LiveData
CFragments directly access each other's views to share data
DFragments use static variables to share data globally
Attempts:
2 left
💡 Hint
Think about ViewModel scope and shared lifecycle owners.
🔧 Debug
expert
3:00remaining
Diagnosing a memory leak caused by ViewModel misuse
A developer stores a reference to an Activity's Context inside a ViewModel. What is the likely consequence?
AMemory leak occurs because ViewModel outlives Activity and holds a strong reference to its Context
BNo problem occurs because ViewModel automatically clears references on Activity destruction
CApp crashes immediately due to invalid Context usage
DThe Context reference is weak and does not affect memory
Attempts:
2 left
💡 Hint
Consider how long ViewModel lives compared to Activity and what holding Context means.