MCQs / Computer Science / Lab: Displaying Data from an API
Lab: Displaying Data from an API MCQs — Computer Science 11 multiple-choice questions on Lab: Displaying Data from an API from Computer Science, each with the correct answer and, where available, a teacher's explanation. Read through them to revise the chapter, then practise the same questions one by one to test yourself.
1. What does GsonConverterFactory do? Creates the RecyclerView Loads images Encrypts traffic Converts JSON responses into Kotlin objects (and objects to JSON) Answer: D) Converts JSON responses into Kotlin objects (and objects to JSON)
Explanation: Gson (de)serialises JSON.
Discuss this question
2. What happens in the lab when the phone is in airplane mode? Retrofit switches to SMS An exception is caught and the error box with Retry is shown The app crashes Old data appears automatically Answer: B) An exception is caught and the error box with Retry is shown
Explanation: The try/catch handles the network failure.
Discuss this question
3. Which tool shows the actual HTTP request and JSON response of the running app? App Inspection → Network Inspector Logcat filters only Device Manager Layout Editor Answer: A) App Inspection → Network Inspector
Explanation: The Network Inspector lists requests, status codes and bodies.
Discuss this question
4. notifyDataSetChanged() is called after updating the list to: Show a Toast Delete the adapter Download the data Tell the RecyclerView to redraw its rows with the new data Answer: D) Tell the RecyclerView to redraw its rows with the new data
Explanation: It signals that the data set changed.
Discuss this question
5. In the model, val address: Address is declared because the JSON field address is: A boolean A number An array A nested JSON object Answer: D) A nested JSON object
Explanation: Nested objects map to nested data classes.
Discuss this question
6. Which permission must be added to load data from an API? ACCESSNETWORKSTATE only BINDJOBSERVICE READEXTERNALSTORAGE INTERNET Answer: D) INTERNET
Explanation: Network access requires android.permission.INTERNET.
Discuss this question
7. Which method of the adapter returns the number of rows? countRows() size() onBindViewHolder() getItemCount() Answer: D) getItemCount()
Explanation: getItemCount tells RecyclerView how many items exist.
Discuss this question
8. Why does BASEURL end with a slash? Retrofit requires the base URL to end with / To enable caching JSONPlaceholder needs it for HTTPS It makes requests faster Answer: A) Retrofit requires the base URL to end with /
Explanation: Retrofit throws an exception if baseUrl does not end with /.
Discuss this question
9. Why is the API method declared with suspend? To cache data To run it twice So it can be called from a coroutine and run without blocking the main thread To make it private Answer: C) So it can be called from a coroutine and run without blocking the main thread
Explanation: Retrofit runs suspend functions off the main thread.
Discuss this question
10. In Java, Retrofit requests are executed asynchronously with: execute() on the main thread call.enqueue(callback) startService() Thread.sleep() Answer: B) call.enqueue(callback)
Explanation: enqueue runs the call in the background and calls back onResponse/onFailure.
Discuss this question
11. lifecycleScope.launch is used because: It needs no internet The coroutine is cancelled automatically when the activity is destroyed It replaces the adapter It runs on a different phone Answer: B) The coroutine is cancelled automatically when the activity is destroyed
Explanation: lifecycleScope is bound to the activity's lifecycle.
Discuss this question
Practise Lab: Displaying Data from an API MCQs
More Computer Science chapters