0
0
Android Kotlinmobile~20 mins

SharedPreferences / DataStore in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
SharedPreferences / DataStore Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What is the output when reading a missing key from SharedPreferences?
Consider this Kotlin code snippet that reads a value from SharedPreferences:
val prefs = context.getSharedPreferences("settings", Context.MODE_PRIVATE)
val username = prefs.getString("user_name", "Guest")
println(username)

What will be printed if the key "user_name" does not exist in the preferences?
Android Kotlin
val prefs = context.getSharedPreferences("settings", Context.MODE_PRIVATE)
val username = prefs.getString("user_name", "Guest")
println(username)
A"Guest"
B"" (empty string)
CThrows NullPointerException
Dnull
Attempts:
2 left
💡 Hint
Check the default value parameter in getString method.
lifecycle
intermediate
2:00remaining
When is it safe to read from DataStore in an Android app?
You want to read user preferences stored in DataStore in your Android app.
At which lifecycle event of an Activity is it best to start collecting DataStore data to update the UI?
AInside onResume()
BInside onStart()
CInside onCreate()
DInside onDestroy()
Attempts:
2 left
💡 Hint
Consider when the UI is visible but not yet interactive.
📝 Syntax
advanced
2:00remaining
Which Kotlin code correctly saves a Boolean value using DataStore?
Given a DataStore instance named dataStore, which code snippet correctly saves a Boolean value with key "dark_mode" set to true?
Android Kotlin
val DARK_MODE_KEY = booleanPreferencesKey("dark_mode")

// Choose the correct snippet to save true to this key
AdataStore.updateData { preferences -> preferences[DARK_MODE_KEY] = true }
BdataStore.save { preferences -> preferences[DARK_MODE_KEY] = true }
CdataStore.edit { preferences -> preferences.put(DARK_MODE_KEY, true) }
DdataStore.edit { preferences -> preferences[DARK_MODE_KEY] = true }
Attempts:
2 left
💡 Hint
Check the official DataStore API for editing preferences.
🔧 Debug
advanced
2:00remaining
Why does this SharedPreferences commit not save data?
Look at this Kotlin code:
val prefs = context.getSharedPreferences("prefs", Context.MODE_PRIVATE)
val editor = prefs.edit()
editor.putString("token", "abc123")
// Missing editor.commit() or editor.apply()

What is the reason the data is not saved?
AThe editor object is not committed or applied, so changes are not saved.
BThe key "token" is invalid and cannot be saved.
CSharedPreferences requires a call to save() instead of commit() or apply().
DContext.MODE_PRIVATE prevents saving data.
Attempts:
2 left
💡 Hint
Check what methods finalize changes in SharedPreferences.Editor.
🧠 Conceptual
expert
2:00remaining
What is a key advantage of DataStore over SharedPreferences?
Which of the following is a key advantage of using DataStore instead of SharedPreferences in Android development?
ASharedPreferences supports type-safe keys, while DataStore does not.
BDataStore stores data in XML files, making it easier to read manually.
CDataStore supports asynchronous, coroutine-based APIs for safer and more efficient data access.
DDataStore requires no context to access, unlike SharedPreferences.
Attempts:
2 left
💡 Hint
Think about modern Kotlin features and thread safety.