Android Development Android Development 3 — Questions and Answers
Question 1: An app crashes with a NetworkOnMainThreadException. What is the correct fix?
- Add the INTERNET permission
- Increase the main thread's priority
- Move the network call to a background thread or coroutine (Correct answer)
- Wrap the call in a try-catch block
Correct answer: Move the network call to a background thread or coroutine
Android forbids network I/O on the main thread, so the call must run on a background dispatcher such as Dispatchers.IO.
Question 2: In Kotlin coroutines, which dispatcher is optimized for disk and network operations?
- Dispatchers.IO (Correct answer)
- Dispatchers.Default
- Dispatchers.Main
- Dispatchers.Unconfined
Correct answer: Dispatchers.IO
Dispatchers.IO maintains a larger thread pool tuned for blocking I/O like file and network access.
Question 3: Why does ViewModel survive configuration changes such as screen rotation?
- It runs in a separate process
- The system prevents rotation while a ViewModel exists
- It is retained in a ViewModelStore that outlives the Activity recreation (Correct answer)
- It is serialized to disk automatically
Correct answer: It is retained in a ViewModelStore that outlives the Activity recreation
The ViewModelStore is kept across configuration changes, so the same ViewModel instance is handed to the recreated Activity.
Question 4: Which RecyclerView optimization avoids redrawing the entire list when only some items change?
- Setting the list height to wrap_content
- Calling notifyDataSetChanged() after every change
- Disabling the item animator
- Using DiffUtil (e.g. via ListAdapter) to dispatch minimal updates (Correct answer)
Correct answer: Using DiffUtil (e.g. via ListAdapter) to dispatch minimal updates
DiffUtil computes the difference between old and new lists and dispatches only the necessary item-level updates.
Question 5: Starting with Android 6.0 (API 23), how must dangerous permissions like CAMERA be obtained?
- Requested via Google Play Console settings
- Granted automatically on install
- Declared in the manifest only
- Requested at runtime in addition to being declared in the manifest (Correct answer)
Correct answer: Requested at runtime in addition to being declared in the manifest
Dangerous permissions require both a manifest declaration and an explicit runtime request that the user can deny.
Question 6: What is the role of an Intent filter declared on an Activity?
- It converts explicit intents into implicit ones
- It blocks other apps from launching the Activity
- It advertises which implicit intents the Activity can handle (Correct answer)
- It caches intents for later delivery
Correct answer: It advertises which implicit intents the Activity can handle
Intent filters let the system match implicit intents, such as viewing a URL, to Activities that declare they can handle them.
Question 7: In a Room database, what does the @Query annotation on a DAO method do?
- Runs the SQL on the main thread by default
- Encrypts the underlying database
- Creates a new table for the entity
- Binds a compile-time verified SQL statement to the method (Correct answer)
Correct answer: Binds a compile-time verified SQL statement to the method
@Query attaches an SQL statement that Room validates at compile time against the schema.
An app crashes with a NetworkOnMainThreadException.
What is the correct fix?