0
0
Android Kotlinmobile~10 mins

Why local storage enables offline access in Android Kotlin - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to save a string value in SharedPreferences.

Android Kotlin
val sharedPref = getSharedPreferences("MyPrefs", MODE_PRIVATE)
val editor = sharedPref.edit()
editor.putString("username", [1])
editor.apply()
Drag options to blanks, or click blank then click option'
Ausername
B"JohnDoe"
CgetString()
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the string value.
Using a variable name instead of a string literal.
2fill in blank
medium

Complete the code to read a string value from SharedPreferences with a default fallback.

Android Kotlin
val sharedPref = getSharedPreferences("MyPrefs", MODE_PRIVATE)
val username = sharedPref.getString("username", [1])
Drag options to blanks, or click blank then click option'
A0
B""
C"Guest"
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Using null without quotes.
Using a number instead of a string.
3fill in blank
hard

Fix the error in the code to correctly save an integer in SharedPreferences.

Android Kotlin
val sharedPref = getSharedPreferences("MyPrefs", MODE_PRIVATE)
val editor = sharedPref.edit()
editor.[1]("userAge", 25)
editor.apply()
Drag options to blanks, or click blank then click option'
AputFloat
BputString
CputBoolean
DputInt
Attempts:
3 left
💡 Hint
Common Mistakes
Using putString for integers.
Using putBoolean or putFloat incorrectly.
4fill in blank
hard

Fill both blanks to check if a key exists and then remove it from SharedPreferences.

Android Kotlin
val sharedPref = getSharedPreferences("MyPrefs", MODE_PRIVATE)
if (sharedPref.[1]("userToken")) {
  val editor = sharedPref.edit()
  editor.[2]("userToken")
  editor.apply()
}
Drag options to blanks, or click blank then click option'
Acontains
Bremove
Cclear
DgetBoolean
Attempts:
3 left
💡 Hint
Common Mistakes
Using getBoolean instead of contains.
Using clear which removes all keys.
5fill in blank
hard

Fill all three blanks to save a boolean flag and then read it with a default value.

Android Kotlin
val sharedPref = getSharedPreferences("MyPrefs", MODE_PRIVATE)
val editor = sharedPref.edit()
editor.[1]("isLoggedIn", true)
editor.apply()

val isLoggedIn = sharedPref.[2]("isLoggedIn", [3])
Drag options to blanks, or click blank then click option'
AputBoolean
BgetBoolean
Cfalse
DputString
Attempts:
3 left
💡 Hint
Common Mistakes
Using putString for booleans.
Using getString instead of getBoolean.
Using true as default instead of false.