MCQs / Computer Science / Lab: SharedPreferences

Lab: SharedPreferences MCQs — Computer Science

11 multiple-choice questions on Lab: SharedPreferences 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. Which line saves the username in the background without blocking the UI?

  1. prefs.edit().putString("username", n).commit()
  2. prefs.edit().putString("username", n)
  3. prefs.putString("username", n)
  4. prefs.edit().putString("username", n).apply()

Answer: D) prefs.edit().putString("username", n).apply()

Explanation: apply() writes asynchronously; without apply/commit nothing is saved.

Discuss this question

2. Which call gets a SharedPreferences object named userprefs?

  1. openFileInput("userprefs")
  2. getSharedPreferences("userprefs", MODEPRIVATE)
  3. SharedPreferences("userprefs")
  4. getPreferences("userprefs")

Answer: B) getSharedPreferences("userprefs", MODEPRIVATE)

Explanation: getSharedPreferences(name, mode) opens or creates the preferences file.

Discuss this question

3. Which Editor method stores a true/false value?

  1. putBoolean()
  2. putFlag()
  3. setBoolean()
  4. putBit()

Answer: A) putBoolean()

Explanation: putBoolean(key, value) stores a Boolean.

Discuss this question

4. To write a value you first call:

  1. prefs.write()
  2. prefs.open()
  3. prefs.save()
  4. prefs.edit() to get an Editor

Answer: D) prefs.edit() to get an Editor

Explanation: Changes are made through SharedPreferences.Editor.

Discuss this question

5. prefs.edit().clear().apply() will:

  1. Remove only the username
  2. Encrypt the file
  3. Remove all keys in that preferences file
  4. Delete the app

Answer: C) Remove all keys in that preferences file

Explanation: clear() removes all entries.

Discuss this question

6. In the lab, why does the saved username still appear after the app is closed and reopened?

  1. SharedPreferences are written to a file in the app's private storage
  2. It is stored in RAM
  3. Android remembers EditText text forever
  4. The emulator caches screens

Answer: A) SharedPreferences are written to a file in the app's private storage

Explanation: The XML preferences file persists across app restarts.

Discuss this question

7. prefs.getString("username", null) returns null when:

  1. No value has been saved for that key
  2. The value is longer than 10 characters
  3. The phone is offline
  4. The app is in landscape

Answer: A) No value has been saved for that key

Explanation: The default value is returned if the key does not exist.

Discuss this question

8. Which call deletes only the username key?

  1. prefs.edit().clear().apply()
  2. prefs.edit().remove("username").apply()
  3. prefs.delete("username")
  4. prefs.remove()

Answer: B) prefs.edit().remove("username").apply()

Explanation: remove(key) deletes a single entry.

Discuss this question

9. The launch counter in the lab is updated by:

  1. Reading the value only
  2. Using commit() twice
  3. Creating a new file each time
  4. Reading the old value, adding 1 and writing it back

Answer: D) Reading the old value, adding 1 and writing it back

Explanation: Read–modify–write updates the stored number.

Discuss this question

10. Which Android Studio tool window lets you browse the device's files?

  1. Layout Inspector
  2. Device Explorer
  3. Build Analyzer
  4. Logcat

Answer: B) Device Explorer

Explanation: Device Explorer shows the file system of the connected device/emulator.

Discuss this question

11. The preferences XML file of an app is located in:

  1. /data/data/package-name/sharedprefs/
  2. /sdcard/Download
  3. res/values
  4. assets/prefs

Answer: A) /data/data/package-name/sharedprefs/

Explanation: Each app's SharedPreferences live in its private data folder.

Discuss this question

Practise Lab: SharedPreferences MCQs

More Computer Science chapters