Sample questions
Kt Jetpack Compose State RecompositionDifficulty 1
What does marking a function with @Composable actually do?
- aIt lets the function participate in composition and emit UI.✓
- bIt permanently caches the function's return value on disk.
- cIt makes the function run on a background thread automatically.
- dIt converts the function into a UIKit-style delegate callback.
Explanation:@Composable is a compiler annotation/marker: it tells the Compose compiler this function describes part of the UI tree and can only be called from other composable functions (or composition-aware entry points like setContent).
Kt Jetpack Compose State RecompositionDifficulty 1
Why does a plain var count = 0 declared directly inside a @Composable function fail to survive recomposition?
- aWithout
remember, the variable resets to its default each time.✓ - bLocal variables are illegal syntax inside composables.
- cKotlin automatically persists all local
vars across function calls. - dIt survives fine; recomposition never re-runs the function body.
Explanation:A composable function's body can be re-invoked on recomposition. A plain local var has no storage outside that single invocation, so it resets on every re-invocation; remember is what gives a value a slot that survives across recompositions.
Kt Jetpack Compose State RecompositionDifficulty 1
What is the basic purpose of mutableStateOf(...)?
- aIt creates a value that can only be read once and then becomes immutable.
- bIt creates an observable holder that triggers recomposition on change.✓
- cIt schedules a one-time background network call.
- dIt wraps a value so it is written directly to a database.
Explanation:mutableStateOf returns a MutableState<T>. Compose's snapshot system tracks reads of .value during composition, so when the value changes, any composable that read it gets scheduled for recomposition.
Kt Jetpack Compose State RecompositionDifficulty 1
Why is remember { mutableStateOf(0) } the common pattern instead of just mutableStateOf(0) alone?
- a
mutableStateOf alone does not compile inside a composable. - b
remember converts the state into a database-backed value. - c
remember keeps the same instance across recompositions.✓ - d
remember makes the state readable from other Activities.
Explanation:Without remember, each recomposition would call mutableStateOf(0) again, creating a brand-new MutableState reset to 0. remember caches the object in a slot in the composition tree so the same instance persists across recompositions.
Kt Jetpack Compose State RecompositionDifficulty 2
A Text(count.toString()) composable reads count from a remember { mutableStateOf(0) }. A button's onClick does count++. What happens right after the click?
- aNothing updates until the whole screen is manually restarted.
- bThe entire app process is killed and relaunched.
- cCompose recomposes readers of
count, so Text shows the new value.✓ - dThe
Text composable is deleted and a brand-new one is created from scratch.
Explanation:Mutating a MutableState's .value (which count++ does under the hood via the delegate) notifies the snapshot system; Compose then recomposes exactly the composables that read that state during the last composition.
Kt Jetpack Compose State RecompositionDifficulty 2
What is the general purpose of LaunchedEffect(key) inside a composable?
- aTo declare a new
@Composable function inline. - bTo permanently block the UI thread until the effect finishes.
- cTo run a suspend side effect tied to composition's lifecycle.✓
- dTo replace
remember for storing simple integers.
Explanation:LaunchedEffect launches a coroutine scoped to the composition. It runs once when the composable enters composition (and the coroutine is cancelled/relaunched whenever the key argument changes), making it the standard way to call suspend functions from composition.