Swift collections—arrays, dictionaries, sets, and tuples—are fundamental for effective data management and manipulation. Dive into this well-structured chapter with MCQs tailored to sharpen your Swift programming skills.
let numbers = Array<Int>()let numbers = [Int]let numbers: [Int] = []array.add(element)array.append(element)array.insert(element)array.addElement(element)removeFirst()removeLast()pop()remove()let fruits = ["Apple", "Banana", "Cherry"] print(fruits[1]) a) Applefor element in arrayarray.eachwhile let element in arraylet dict = [String: Int]()let dict = Dictionary<String, Int>()let dict: [String: Int] = [:]nilremove(key:)deleteValue(forKey:)removeValue(forKey:)removeElement(key:)dict.keysdict.getKeys()dict.allKeyslet scores = ["Math": 90, "Science": 95] How can you update the value for key "Math" to 100?scores.updateValue(100, forKey: "Math")scores["Math"] = 100var set: Set = [1, 2, 3]var set = Set<Int>()set.contains(element)set.has(element)set.includes(element)set.elementExists()set1.merge(set2)set1.formUnion(set2)set1.append(set2)set1.combine(set2)union()intersection()difference()exclusive()let setA: Set = [1, 2, 3] let setB: Set = [3, 4, 5] setA.isSubset(of: setB) a) truefalselet tuple = ("John", 25)let tuple: (String, Int)let tuple = (name: "John", age: 25)tuple[1]tuple.1tuple.secondtuple["second"]let person = (name: "Alice", age: 30) print(person.name) a) Alice+ operatortuple1.combine(tuple2)(tuple1, tuple2)| QNo | Answer |
|---|---|
| 1 | d) All of the above |
| 2 | b) array.append(element) |
| 3 | b) removeLast() |
| 4 | b) Banana |
| 5 | a) Using for element in array |
| 6 | d) All of the above |
| 7 | b) Returns nil |
| 8 | c) removeValue(forKey:) |
| 9 | a) dict.keys |
| 10 | c) Both a and b |
| 11 | c) Both a and b |
| 12 | a) set.contains(element) |
| 13 | b) set1.formUnion(set2) |
| 14 | b) intersection() |
| 15 | b) false |
| 16 | d) All of the above |
| 17 | b) tuple.1 |
| 18 | c) Yes, optionally |
| 19 | a) Alice |
| 20 | c) Using (tuple1, tuple2) |