Bird
Raised Fist0
Android Kotlinmobile~10 mins

State in ViewModel in Android Kotlin - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a mutable state variable in ViewModel.

Android Kotlin
private val _count = MutableStateFlow([1])
val count: StateFlow<Int> = _count
Drag options to blanks, or click blank then click option'
Afalse
B""
C0
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Using empty string "" instead of 0 for an integer state.
Using null which is not allowed without nullable type.
Using false which is a Boolean, not an Int.
2fill in blank
medium

Complete the code to update the state value inside ViewModel.

Android Kotlin
fun increment() {
  _count.value = _count.value [1] 1
}
Drag options to blanks, or click blank then click option'
A+
B-
C+=
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+=' which is not valid for StateFlow value assignment.
Using '-' or '*' which change the value incorrectly.
3fill in blank
hard

Fix the error in the ViewModel state declaration.

Android Kotlin
class CounterViewModel : ViewModel() {
  private val _count = MutableStateFlow<Int>([1])
  val count: StateFlow<Int> = _count
}
Drag options to blanks, or click blank then click option'
Anull
Bfalse
C"0"
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using null which causes a runtime error.
Using string "0" which is not an Int.
Using false which is a Boolean.
4fill in blank
hard

Fill both blanks to create a function that resets the count to zero.

Android Kotlin
fun reset() {
  _count.[1] = [2]
}
Drag options to blanks, or click blank then click option'
Avalue
B0
Cnull
Demit
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'emit' which is a suspend function and cannot be called directly here.
Assigning null which is not allowed for non-nullable Int state.
5fill in blank
hard

Fill all three blanks to create a ViewModel with a mutable state and a function to decrement the count.

Android Kotlin
class CounterViewModel : ViewModel() {
  private val _count = MutableStateFlow([1])
  val count: StateFlow<Int> = [2]

  fun decrement() {
    _count.value = _count.value [3] 1
  }
}
Drag options to blanks, or click blank then click option'
A0
B_count
C-
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Exposing 'count' instead of '_count' for the public state.
Using '+' instead of '-' for decrement.
Initializing with a wrong type.

Practice

(1/5)
1. What is the main purpose of using ViewModel in Android development?
easy
A. To replace the Activity lifecycle completely
B. To handle user input events directly
C. To store and manage UI-related data safely during configuration changes
D. To perform network requests on the main thread

Solution

  1. Step 1: Understand ViewModel role and compare options

    ViewModel is designed to hold UI data and survive configuration changes like screen rotations. Only To store and manage UI-related data safely during configuration changes correctly describes this purpose; others describe unrelated or incorrect roles.
  2. Final Answer:

    To store and manage UI-related data safely during configuration changes -> Option C
  3. Quick Check:

    ViewModel stores UI data safely [OK]
Hint: ViewModel keeps UI data safe during screen rotations [OK]
Common Mistakes:
  • Thinking ViewModel handles UI events directly
  • Assuming ViewModel replaces Activity lifecycle
  • Using ViewModel for network calls on main thread
2. Which of the following is the correct way to declare a mutable state in a ViewModel using LiveData?
easy
A. var data = MutableLiveData() as LiveData<String>
B. val data = LiveData<String>()
C. var data = LiveData<String>()
D. val data = MutableLiveData<String>()

Solution

  1. Step 1: Identify mutable LiveData declaration and syntax

    MutableLiveData allows changing the value; LiveData alone is read-only. val data = MutableLiveData<String>() correctly declares a mutable LiveData with type String.
  2. Final Answer:

    val data = MutableLiveData<String>() -> Option D
  3. Quick Check:

    MutableLiveData is mutable LiveData [OK]
Hint: MutableLiveData allows data changes; LiveData is read-only [OK]
Common Mistakes:
  • Using LiveData() directly (it's abstract)
  • Declaring mutable LiveData as var without MutableLiveData
  • Casting MutableLiveData to LiveData incorrectly
3. Given this ViewModel code snippet:
class MyViewModel : ViewModel() {
  private val _count = MutableLiveData(0)
  val count: LiveData<Int> = _count

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

What will be the value of count after calling increment() twice starting from initial state?
medium
A. 2
B. 1
C. 0
D. null

Solution

  1. Step 1: Trace execution from initial value

    _count starts at 0 as MutableLiveData(0). Each increment adds 1: first call sets 1, second call sets 2.
  2. Final Answer:

    2 -> Option A
  3. Quick Check:

    0 + 1 + 1 = 2 [OK]
Hint: Each increment adds 1 to current LiveData value [OK]
Common Mistakes:
  • Forgetting to unwrap nullable value with ?: 0
  • Assuming LiveData updates automatically without setting value
  • Confusing private _count with public count
4. Identify the error in this ViewModel code:
class SampleViewModel : ViewModel() {
  val data = MutableLiveData<String>()

  fun updateData(newValue: String) {
    data.value = newValue
  }

  fun getData(): String {
    return data.value
  }
}
medium
A. getData() returns nullable String but declared as non-null
B. MutableLiveData cannot hold String values
C. data.value should be updated using postValue() only
D. ViewModel cannot have functions

Solution

  1. Step 1: Analyze getData() return type mismatch

    data.value is nullable String?, but getData() returns non-null String. Returning nullable as non-null causes compile error or runtime crash.
  2. Final Answer:

    getData() returns nullable String but declared as non-null -> Option A
  3. Quick Check:

    LiveData.value is nullable [OK]
Hint: LiveData.value is nullable; match return types [OK]
Common Mistakes:
  • Ignoring nullable type of LiveData.value
  • Thinking postValue() is mandatory for all updates
  • Believing ViewModel cannot have functions
5. You want to expose a read-only LiveData from your ViewModel while keeping the MutableLiveData private. Which code snippet correctly implements this pattern?
hard
A. private val _state = MutableLiveData<Int>() val state: MutableLiveData<Int> = _state
B. private val _state = MutableLiveData<Int>() val state: LiveData<Int> get() = _state
C. val state = MutableLiveData<Int>()
D. val _state = LiveData<Int>() val state: MutableLiveData<Int> = _state

Solution

  1. Step 1: Identify correct encapsulation pattern

    MutableLiveData should be private; expose only LiveData publicly. private val _state = MutableLiveData<Int>() val state: LiveData<Int> get() = _state declares private MutableLiveData and public LiveData getter correctly.
  2. Final Answer:

    private val _state = MutableLiveData<Int>()\nval state: LiveData<Int> get() = _state -> Option B
  3. Quick Check:

    Expose LiveData, keep MutableLiveData private [OK]
Hint: Expose LiveData getter, keep MutableLiveData private [OK]
Common Mistakes:
  • Exposing MutableLiveData publicly
  • Declaring LiveData as mutable
  • Using LiveData constructor directly (abstract class)
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard