SharedPreferences is used to store small amounts of simple data as key-value pairs, like user settings or preferences, that persist across app launches.
Click to reveal answer
intermediate
What is the main advantage of DataStore over SharedPreferences?
DataStore is more modern, supports asynchronous and type-safe data storage, and avoids blocking the main thread, making it safer and more efficient for storing data.
Click to reveal answer
beginner
How do you save a string value using SharedPreferences in Kotlin?
Use edit() to get an editor, then putString(key, value), and finally apply() to save asynchronously.
Click to reveal answer
intermediate
What are the two types of DataStore in Android?
Preferences DataStore (stores key-value pairs without a predefined schema) and Proto DataStore (stores typed objects using Protocol Buffers).
Click to reveal answer
beginner
Why should you avoid using SharedPreferences on the main thread?
Because SharedPreferences can block the main thread during read/write operations, causing the app UI to freeze or lag.
Click to reveal answer
Which method is used to save changes in SharedPreferences asynchronously?
Astore()
Bcommit()
Capply()
Dsave()
✗ Incorrect
apply() saves changes asynchronously without blocking the main thread, while commit() saves synchronously.
What is a key benefit of using DataStore over SharedPreferences?
ANo need to define keys
BOnly stores strings
CBlocks the main thread
DSupports asynchronous operations
✗ Incorrect
DataStore supports asynchronous operations, improving app performance and responsiveness.
Which DataStore type uses Protocol Buffers for data storage?
APreferences DataStore
BProto DataStore
CSharedPreferences
DSQL DataStore
✗ Incorrect
Proto DataStore uses Protocol Buffers to store typed data objects.
How do you read a value from SharedPreferences?
AgetString(key, defaultValue)
BreadString(key)
CfetchString(key)
DloadString(key)
✗ Incorrect
Use getString(key, defaultValue) to read a string value from SharedPreferences.
Which of these is NOT a recommended practice when using SharedPreferences?
AStore large binary files
BUse apply() for saving changes
CAvoid blocking the main thread
DStore simple key-value pairs
✗ Incorrect
SharedPreferences is not designed for storing large binary files; use other storage options instead.
Explain how to save and retrieve a user preference using SharedPreferences in Kotlin.
Think about how you store and read simple settings like a username.
You got /4 concepts.
Describe the differences between Preferences DataStore and Proto DataStore and when to use each.
Consider the complexity and type safety of your data.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of SharedPreferences in Android development?
easy
A. To handle network requests and responses
B. To create complex database tables for large data storage
C. To save small amounts of key-value data persistently on the device
D. To design user interface layouts
Solution
Step 1: Understand SharedPreferences role
SharedPreferences is designed to store small amounts of data as key-value pairs persistently on the device.
Step 2: Compare with other options
Options B, C, and D describe unrelated tasks like databases, networking, and UI design, which are not the purpose of SharedPreferences.
Final Answer:
To save small amounts of key-value data persistently on the device -> Option C
Quick Check:
SharedPreferences = small persistent key-value storage [OK]
2. Which of the following is the correct way to get a SharedPreferences instance in Kotlin inside an Activity?
easy
A. val prefs = SharedPreferences("my_prefs")
B. val prefs = getSharedPreferences("my_prefs", Context.MODE_PRIVATE)
C. val prefs = SharedPreferences.getInstance("my_prefs")
D. val prefs = getPreferences("my_prefs")
Solution
Step 1: Recall correct method to get SharedPreferences
The correct method is to call getSharedPreferences(name, mode) on a Context, passing the name and mode.
Step 2: Analyze options
val prefs = getSharedPreferences("my_prefs", Context.MODE_PRIVATE) uses the correct method. Options A, C, and D are invalid constructors or methods. val prefs = getPreferences("my_prefs") uses getPreferences which does not take a name parameter.
Final Answer:
val prefs = getSharedPreferences("my_prefs", Context.MODE_PRIVATE) -> Option B
Quick Check:
Use getSharedPreferences(name, mode) to get prefs [OK]
Hint: Use getSharedPreferences with name and mode inside Activity [OK]
Common Mistakes:
Trying to instantiate SharedPreferences directly
Using getPreferences with a name parameter
Confusing method names or parameters
3. What will be the output of this Kotlin code snippet using SharedPreferences?
val prefs = getSharedPreferences("settings", Context.MODE_PRIVATE)
val editor = prefs.edit()
editor.putString("username", "Alice")
editor.apply()
val name = prefs.getString("username", "Unknown")
println(name)
medium
A. null
B. Unknown
C. Compilation error
D. Alice
Solution
Step 1: Store value in SharedPreferences
The code puts the string "Alice" with key "username" and applies the changes asynchronously.
Step 2: Retrieve stored value
Then it retrieves the value for "username"; since it was stored, it returns "Alice" instead of the default "Unknown".
4. Identify the error in this Kotlin code using DataStore to save a boolean value:
val dataStore = context.createDataStore(name = "settings")
fun saveFlag(flag: Boolean) {
dataStore.edit { preferences ->
preferences[booleanPreferencesKey("flag")] = flag
}
}
medium
A. Missing 'suspend' modifier on the saveFlag function
B. Missing commit() call after edit block
C. booleanPreferencesKey should be replaced with stringPreferencesKey
D. DataStore cannot store boolean values
Solution
Step 1: Check DataStore function modifiers
The function saveFlag must be marked suspend because it calls dataStore.edit(), which is a suspend function.
Step 2: Verify other code parts
The createDataStore call is correct. The edit block commits changes automatically. Using booleanPreferencesKey is correct for boolean values. DataStore supports boolean values.
Final Answer:
Missing 'suspend' modifier on the saveFlag function -> Option A
5. You want to migrate existing user settings from SharedPreferences to DataStore in a Kotlin app. Which approach correctly handles this migration?
hard
A. Use SharedPreferencesMigration with DataStore to copy data automatically on first access
B. Manually read all SharedPreferences keys and write them to DataStore every app start
C. Delete SharedPreferences and start fresh with DataStore without migration
D. Use SharedPreferences.edit() inside DataStore's edit block
Solution
Step 1: Understand migration support in DataStore
DataStore provides a built-in SharedPreferencesMigration class to migrate data automatically from SharedPreferences on first DataStore access.
Step 2: Evaluate other options
Manually read all SharedPreferences keys and write them to DataStore every app start is inefficient and error-prone. Delete SharedPreferences and start fresh with DataStore without migration loses user data. Use SharedPreferences.edit() inside DataStore's edit block is invalid because SharedPreferences and DataStore APIs are separate and cannot be mixed inside edit blocks.
Final Answer:
Use SharedPreferencesMigration with DataStore to copy data automatically on first access -> Option A
Quick Check:
Use SharedPreferencesMigration for smooth migration [OK]
Hint: Use SharedPreferencesMigration for easy migration [OK]
Common Mistakes:
Trying manual migration every app start
Deleting old preferences losing data
Mixing SharedPreferences API inside DataStore
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.