0
0
iOS Swiftmobile~20 mins

Collections (Array, Dictionary, Set) in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Collections Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What is the output of this Swift code snippet?
Consider this Swift code that uses a dictionary to count occurrences of words. What will be printed?
iOS Swift
let words = ["apple", "banana", "apple", "orange", "banana", "apple"]
var counts = [String: Int]()
for word in words {
  counts[word, default: 0] += 1
}
print(counts["apple"] ?? 0)
A2
B1
C3
D0
Attempts:
2 left
💡 Hint
Count how many times "apple" appears in the array.
📝 Syntax
intermediate
2:00remaining
Which option correctly creates a Set of unique integers in Swift?
You want to create a Set containing the numbers 1, 2, 3, 3, 4. Which code snippet does this correctly?
Alet numbers = Set([1, 2, 3, 3, 4])
Blet numbers = [1, 2, 3, 3, 4] as Set
Clet numbers = Set(1, 2, 3, 3, 4)
Dlet numbers: Set = [1, 2, 3, 3, 4]
Attempts:
2 left
💡 Hint
Sets can be created from arrays using the Set initializer.
🧠 Conceptual
advanced
2:00remaining
What happens when you access a dictionary key that does not exist in Swift?
Given a dictionary let dict = ["a": 1, "b": 2], what is the result of dict["c"]?
AIt causes a runtime error
BIt returns 0
CIt returns an empty string
DIt returns nil
Attempts:
2 left
💡 Hint
Think about optional values in Swift.
lifecycle
advanced
2:00remaining
What is the effect of modifying an Array while iterating over it in Swift?
Consider this code: var numbers = [1, 2, 3] for number in numbers { numbers.append(number + 3) } What happens when this runs?
iOS Swift
var numbers = [1, 2, 3]
for number in numbers {
  numbers.append(number + 3)
}
AIt throws a runtime error
BIt causes an infinite loop
CIt appends new elements only once and stops
DIt ignores the appended elements during iteration
Attempts:
2 left
💡 Hint
Think about how the for-in loop works with arrays.
🔧 Debug
expert
2:00remaining
Why does this Swift code cause a crash?
Examine this code: var dict = ["key1": "value1"] let value = dict["key2"]! print(value) Why does it crash?
iOS Swift
var dict = ["key1": "value1"]
let value = dict["key2"]!
print(value)
ABecause dict["key2"] is nil and force unwrapping causes a crash
BBecause the dictionary is empty
CBecause "key2" is not a valid key type
DBecause print cannot print optional values
Attempts:
2 left
💡 Hint
Force unwrapping nil causes a crash.