Sample questions
Ios Swift Type System OptionalsDifficulty 1
A struct instance is assigned to a new variable, then the copy's property is mutated. What happens to the original?
- aThe original is unaffected✓
- bThe original changes too, since structs are references.
- cBoth variables now point to the same crashed instance
- dThe assignment itself fails to compile.
Explanation:Structs are value types: assignment copies the value, so mutating the copy leaves the original untouched, unlike a class instance which would share the same reference.
Ios Swift Type System OptionalsDifficulty 1
A class instance is assigned to a new variable, then a property is mutated through that new variable. What happens to the original?
- aThe original is unaffected, classes always copy on assignment.
- bThe program crashes immediately on assignment
- cThe original also reflects the change✓
- dOnly the deinitializer runs early.
Explanation:Classes are reference types: assignment copies the reference, not the underlying object, so mutating through either variable is visible through the other.
Ios Swift Type System OptionalsDifficulty 2
Two Array<Int> value bindings share the same storage until one is mutated ("copy-on-write"). What triggers the actual copy?
- aSimply reading either array's contents.
- bAttempting to mutate one of them while more than one reference to the storage exists✓
- cDeclaring the second variable, even before any mutation.
- dPassing either array to a function as a parameter
Explanation:Copy-on-write means shared storage is only duplicated at the point of mutation when the storage's reference count indicates it's shared; plain reads or extra bindings alone don't trigger a copy.
Ios Swift Type System OptionalsDifficulty 1
What does declaring a property as var name: String? mean?
- aThe property can hold either a String value or be absent (nil)✓
- bThe property is guaranteed to always hold a non-empty String.
- cThe property can only be set once and never changed again
- dThe
? makes the property thread-safe automatically.
Explanation:The ? marks the type as Optional<String>, meaning the property can either hold a wrapped String value or be nil, representing the absence of a value.
Ios Swift Type System OptionalsDifficulty 2
Given let name: String? = nil, what does name?.uppercased() evaluate to?
- aA runtime crash, since uppercased() is called on nil
- b
nil, because optional chaining short-circuits when the optional is nil✓ - cAn empty string "".
- dA compile-time error since chaining requires force-unwrap.
Explanation:Optional chaining stops safely and evaluates to nil for the whole expression when the receiver is nil, instead of calling the method or crashing.
Ios Swift Type System OptionalsDifficulty 1
What does the nil-coalescing operator ?? do in let value = maybeName ?? "default"?
- aForces
maybeName to unwrap and crashes if it's nil. - bAlways returns "default" regardless of maybeName
- cConverts maybeName to a Bool for an if-check.
- dUses
maybeName's wrapped value if present, otherwise falls back to "default"✓
Explanation:?? provides a fallback: if the left-hand optional holds a value it's used, otherwise the right-hand default expression is evaluated and used instead, without ever crashing.