Android Development Android Development 4 — Questions and Answers
Question 1: A user reports that your app's UI freezes for two seconds when opening a screen that parses a large JSON file. What is the best remedy?
- Parse the JSON in a coroutine on a background dispatcher and post results to the UI (Correct answer)
- Show a Toast asking the user to wait
- Parse the JSON inside onDraw()
- Increase the ANR timeout in the manifest
Correct answer: Parse the JSON in a coroutine on a background dispatcher and post results to the UI
Heavy parsing must be moved off the main thread; coroutines with Dispatchers.Default or IO keep the UI responsive.
Question 2: What is the difference between 'minSdkVersion' and 'targetSdkVersion' in Gradle?
- targetSdk is the lowest version; minSdk is the newest
- minSdk is the lowest OS version supported; targetSdk declares which version's behavior changes the app is tested against (Correct answer)
- They must always be equal
- minSdk sets the compile version; targetSdk sets the install version
Correct answer: minSdk is the lowest OS version supported; targetSdk declares which version's behavior changes the app is tested against
minSdkVersion gates installation on old devices, while targetSdkVersion opts the app into newer platform behaviors.
Question 3: Which scenario correctly describes a memory leak in an Android app?
- A ViewModel is cleared when its Activity finishes
- A bitmap is recycled after use
- A coroutine is cancelled in onCleared()
- A static field holds a reference to a destroyed Activity, preventing garbage collection (Correct answer)
Correct answer: A static field holds a reference to a destroyed Activity, preventing garbage collection
Long-lived references, such as statics or singletons holding an Activity, keep destroyed components in memory.
Question 4: What is the purpose of a Foreground Service on modern Android versions?
- Running hidden tasks without any user awareness
- Keeping an Activity alive after the user leaves
- Running user-visible ongoing work, like music playback, with a persistent notification (Correct answer)
- Replacing WorkManager for all background jobs
Correct answer: Running user-visible ongoing work, like music playback, with a persistent notification
Foreground services perform noticeable ongoing work and must display a notification so the user knows they are running.
Question 5: In Jetpack Compose, what does the 'remember' function do?
- Stores a value in SharedPreferences
- Saves state during configuration changes only
- Persists a value across process death
- Caches a value across recompositions of the same composable (Correct answer)
Correct answer: Caches a value across recompositions of the same composable
remember keeps a value in the composition so it is not recalculated on every recomposition, though it is lost on configuration change unless rememberSaveable is used.
Question 6: Which Gradle build feature lets you produce free and paid versions of an app from one codebase?
- Product flavors (Correct answer)
- Build caching
- Lint baselines
- Instant Run
Correct answer: Product flavors
Product flavors define app variants, such as free and paid, that can differ in code, resources, and application ID.
Question 7: What does the LiveData class guarantee that a plain observable does not?
- Lifecycle-aware delivery, so observers only receive updates while active (Correct answer)
- Values are persisted across reboots
- Observers are notified even after onDestroy()
- Updates are delivered on a background thread
Correct answer: Lifecycle-aware delivery, so observers only receive updates while active
LiveData respects the observer's lifecycle, delivering data only to started or resumed components and auto-removing destroyed ones.
A user reports that your app's UI freezes for two seconds when opening a screen that parses a large JSON file.
What is the best remedy?