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?

  1. Creates the RecyclerView
  2. Loads images
  3. Encrypts traffic
  4. 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?

  1. Retrofit switches to SMS
  2. An exception is caught and the error box with Retry is shown
  3. The app crashes
  4. 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?

  1. App Inspection → Network Inspector
  2. Logcat filters only
  3. Device Manager
  4. 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:

  1. Show a Toast
  2. Delete the adapter
  3. Download the data
  4. 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:

  1. A boolean
  2. A number
  3. An array
  4. 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?

  1. ACCESSNETWORKSTATE only
  2. BINDJOBSERVICE
  3. READEXTERNALSTORAGE
  4. 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?

  1. countRows()
  2. size()
  3. onBindViewHolder()
  4. getItemCount()

Answer: D) getItemCount()

Explanation: getItemCount tells RecyclerView how many items exist.

Discuss this question

8. Why does BASEURL end with a slash?

  1. Retrofit requires the base URL to end with /
  2. To enable caching
  3. JSONPlaceholder needs it for HTTPS
  4. 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?

  1. To cache data
  2. To run it twice
  3. So it can be called from a coroutine and run without blocking the main thread
  4. 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:

  1. execute() on the main thread
  2. call.enqueue(callback)
  3. startService()
  4. 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:

  1. It needs no internet
  2. The coroutine is cancelled automatically when the activity is destroyed
  3. It replaces the adapter
  4. 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