A. count is not a state variable, so UI won't update
B. Button onClick syntax is incorrect
C. Text composable cannot display variables
D. count should be a val, not var
Solution
Step 1: Identify state usage in code
Variable count is a plain var, not wrapped in state, so Compose won't track changes.
Step 2: Understand recomposition trigger
Without state, UI does not recompose on count change, so text stays the same.
Final Answer:
count is not a state variable, so UI won't update -> Option A
Quick Check:
State needed for recomposition [OK]
Hint: Use state variables to trigger UI updates [OK]
Common Mistakes:
Thinking var alone triggers recomposition
Believing onClick syntax is wrong
Assuming Text can't show variables
5. You want to create a counter that resets to zero when it reaches 5, updating the UI automatically. Which code snippet correctly uses recomposition to achieve this?
@Composable
fun Counter() {
var count by remember { mutableStateOf(0) }
Button(onClick = {
count = if (count == 5) 0 else count + 1
}) {
Text("Count: $count")
}
}
hard
A. Correct: count resets at 5 and UI updates
B. Incorrect: count never resets to zero
C. Incorrect: count is not a state variable
D. Incorrect: Button onClick syntax is invalid
Solution
Step 1: Analyze state and logic in code
Variable count is a state variable with remember and mutableStateOf, so UI updates on change.
Step 2: Check reset logic correctness
When count reaches 5, it resets to 0; otherwise, it increments by 1. This logic is correct.
Final Answer:
Correct: count resets at 5 and UI updates -> Option A
Quick Check:
State + reset logic triggers recomposition [OK]
Hint: Use state and conditional update for reset [OK]
Common Mistakes:
Not using state variable for count
Missing reset condition
Incorrect Button onClick syntax
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.