0
0
iOS Swiftmobile~20 mins

UserDefaults for simple values in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
UserDefaults Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
1:30remaining
What is the output after saving and retrieving a value with UserDefaults?
Consider this Swift code that saves a string to UserDefaults and then retrieves it. What will be printed?
iOS Swift
let defaults = UserDefaults.standard
let key = "username"
defaults.set("Alice", forKey: key)
if let name = defaults.string(forKey: key) {
  print(name)
} else {
  print("No value")
}
AAlice
BNo value
Cnil
DCrash due to missing key
Attempts:
2 left
💡 Hint
UserDefaults stores simple values persistently and can retrieve them by the same key.
📝 Syntax
intermediate
1:30remaining
Which option correctly saves an integer to UserDefaults?
You want to save the integer 42 to UserDefaults under the key "score". Which code snippet does this correctly?
AUserDefaults.standard.setInt(42, forKey: "score")
BUserDefaults.standard["score"] = 42
CUserDefaults.set(42, forKey: "score")
DUserDefaults.standard.set(42, forKey: "score")
Attempts:
2 left
💡 Hint
Use the standard UserDefaults instance and the set(_:forKey:) method.
lifecycle
advanced
2:00remaining
When are UserDefaults values saved to disk?
You save a value to UserDefaults using set(_:forKey:). When is this value guaranteed to be saved to disk?
ANever saved automatically; must call save() explicitly
BWhen the app terminates or goes to background
COnly when synchronize() is called
DImmediately after calling set(_:forKey:)
Attempts:
2 left
💡 Hint
UserDefaults batches writes for efficiency and saves at certain app lifecycle events.
🔧 Debug
advanced
2:00remaining
Why does retrieving a saved Bool from UserDefaults return false unexpectedly?
This code saves a Bool value true but later retrieving it returns false. What is the likely cause? let defaults = UserDefaults.standard defaults.set(true, forKey: "isLoggedIn") let loggedIn = defaults.bool(forKey: "isLoggedIn") print(loggedIn)
AThe key "isLoggedIn" was never saved before retrieval
BThe value was saved but the retrieval key is misspelled
CUserDefaults.bool(forKey:) returns false if the key does not exist
DUserDefaults cannot store Bool values
Attempts:
2 left
💡 Hint
Check if the key exists before relying on bool(forKey:).
🧠 Conceptual
expert
2:30remaining
What is the best practice for storing sensitive data in UserDefaults?
You want to store a user's password securely. Which statement is true about storing it in UserDefaults?
AUserDefaults is not secure; use Keychain for sensitive data instead
BUserDefaults encrypts data automatically, so it's safe for passwords
CStoring passwords in UserDefaults is fine if the app uses HTTPS
DUserDefaults is secure and recommended for storing passwords
Attempts:
2 left
💡 Hint
Think about data encryption and security best practices on iOS.