Hint: Use square brackets and commas for arrays [OK]
Common Mistakes:
Using curly braces instead of square brackets
Separating elements with semicolons
Trying to call Array like a function with multiple arguments
3. What will be the output of this Swift code?
var original = [1, 2, 3]
var copy = original
copy.append(4)
print(original)
medium
A. [1, 2, 3, 4]
B. [4]
C. Error: Cannot append to copy
D. [1, 2, 3]
Solution
Step 1: Understand value type copy behavior
Assigning original to copy creates a new independent array copy.
Step 2: Append to copy and check original
Appending 4 to copy does not affect original, so original remains [1, 2, 3].
Final Answer:
[1, 2, 3] -> Option D
Quick Check:
Value type copy = original unchanged [OK]
Hint: Appending copy doesn't change original array [OK]
Common Mistakes:
Assuming original changes when copy is modified
Expecting a runtime error on append
Confusing reference and value types
4. Identify the error in this Swift code related to collections as value types:
var list1 = ["a", "b"]
var list2 = list1
list2.append("c")
print(list1)
medium
A. No error; output is ["a", "b"]
B. Appending to list2 causes a compile error
C. list1 changes unexpectedly after modifying list2
D. list1 and list2 share the same reference
Solution
Step 1: Analyze value type behavior in code
list2 is a copy of list1; modifying list2 does not affect list1.
Step 2: Check output of print(list1)
list1 remains ["a", "b"] because collections are value types.
Final Answer:
No error; output is ["a", "b"] -> Option A
Quick Check:
Value types keep original unchanged [OK]
Hint: Modifying copy doesn't affect original collection [OK]
Common Mistakes:
Thinking list1 changes after list2 append
Expecting a compile or runtime error
Believing collections are reference types by default
5. You want to create a function that modifies a dictionary passed as a parameter without changing the original dictionary outside the function. Why does Swift's value type behavior for collections help here?
hard
A. Because dictionaries are reference types and share data automatically
B. Because the dictionary is copied when passed, so original stays unchanged
C. Because Swift prevents any modification inside functions
D. Because the function must return a new dictionary explicitly
Solution
Step 1: Understand parameter passing for value types
When a dictionary is passed to a function, Swift copies it, so the function works on a separate copy.
Step 2: Effect on original dictionary
Modifications inside the function do not affect the original dictionary outside because of this copy.
Final Answer:
Because the dictionary is copied when passed, so original stays unchanged -> Option B
Quick Check:
Value type copy on function call = Because the dictionary is copied when passed, so original stays unchanged [OK]
Hint: Function gets copy; original dictionary safe [OK]
Common Mistakes:
Assuming dictionaries are reference types
Thinking original changes without explicit return
Believing Swift forbids modification inside functions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.