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? prefs.edit().putString("username", n).commit() prefs.edit().putString("username", n) prefs.putString("username", n) 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? openFileInput("userprefs") getSharedPreferences("userprefs", MODEPRIVATE) SharedPreferences("userprefs") 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? putBoolean() putFlag() setBoolean() putBit() Answer: A) putBoolean()
Explanation: putBoolean(key, value) stores a Boolean.
Discuss this question
4. To write a value you first call: prefs.write() prefs.open() prefs.save() 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: Remove only the username Encrypt the file Remove all keys in that preferences file 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? SharedPreferences are written to a file in the app's private storage It is stored in RAM Android remembers EditText text forever 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: No value has been saved for that key The value is longer than 10 characters The phone is offline 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? prefs.edit().clear().apply() prefs.edit().remove("username").apply() prefs.delete("username") 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: Reading the value only Using commit() twice Creating a new file each time 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? Layout Inspector Device Explorer Build Analyzer 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: /data/data/package-name/sharedprefs/ /sdcard/Download res/values 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