Which is the correct way to read a string preference with a default value?

  1. prefs.getString("username", "Guest")
  2. prefs.read("username")
  3. prefs.username
  4. prefs.getString("username")
Answer

A) prefs.getString("username", "Guest")

The second parameter is returned if the key is missing.

Computer Science MCQ · More UI Events, Views, Preferences and Data Storage MCQs · Practise