Challenge - 5 Problems
ViewModel Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate2:00remaining
Correct ViewModel class declaration
Which option correctly declares a simple ViewModel class in Kotlin for Android?
Attempts:
2 left
💡 Hint
Remember Kotlin uses ':' for inheritance and parentheses for constructor calls.
✗ Incorrect
In Kotlin, to create a ViewModel class, you extend ViewModel with a colon and call its constructor with parentheses. Option A follows this syntax correctly.
❓ ui_behavior
intermediate2:00remaining
ViewModel lifecycle behavior
What happens to a ViewModel instance when the associated Activity is rotated (configuration change)?
Attempts:
2 left
💡 Hint
Think about how ViewModel helps keep UI data during screen rotations.
✗ Incorrect
ViewModel survives configuration changes like rotation by being retained and reused by the new Activity instance, preventing data loss.
🧠 Conceptual
advanced2:00remaining
Purpose of ViewModel in MVVM architecture
What is the main role of a ViewModel in the MVVM pattern on Android?
Attempts:
2 left
💡 Hint
ViewModel acts as a bridge between UI and data layers.
✗ Incorrect
ViewModel holds UI data and business logic, keeping it separate from UI controllers like Activities or Fragments, enabling cleaner code and lifecycle awareness.
🔧 Debug
advanced2:00remaining
Identify the error in ViewModel usage
What error will occur with this code snippet?
val viewModel = ViewModelProvider(this).get(MyViewModel::class.java)
class MyViewModel {}
Android Kotlin
val viewModel = ViewModelProvider(this).get(MyViewModel::class.java) class MyViewModel {}
Attempts:
2 left
💡 Hint
Check if MyViewModel inherits from the correct base class.
✗ Incorrect
MyViewModel does not extend ViewModel, so ViewModelProvider cannot create its instance, causing a compile error.
expert
2:00remaining
ViewModel sharing between fragments
How can two fragments in the same Activity share the same ViewModel instance?
Attempts:
2 left
💡 Hint
Think about the lifecycle owner scope when requesting ViewModel.
✗ Incorrect
Using the Activity as the ViewModelStoreOwner allows fragments to share the same ViewModel instance tied to the Activity lifecycle.