MCQs on Collections | Swift

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.


Topic 1: Arrays – Declaration, Operations, and Iteration

  1. Which of the following correctly declares an empty array of integers in Swift?
    a) let numbers = Array<Int>()
    b) let numbers = [Int]
    c) let numbers: [Int] = []
    d) All of the above
  2. How do you append a new element to an existing array?
    a) array.add(element)
    b) array.append(element)
    c) array.insert(element)
    d) array.addElement(element)
  3. Which method removes the last element of an array in Swift?
    a) removeFirst()
    b) removeLast()
    c) pop()
    d) remove()
  4. What does the following code output?swiftCopy codelet fruits = ["Apple", "Banana", "Cherry"] print(fruits[1]) a) Apple
    b) Banana
    c) Cherry
    d) Error
  5. How can you iterate through all elements of an array?
    a) Using for element in array
    b) Using array.each
    c) Using while let element in array
    d) All of the above

Topic 2: Dictionaries – Key-Value Pair Management

  1. How do you declare an empty dictionary in Swift?
    a) let dict = [String: Int]()
    b) let dict = Dictionary<String, Int>()
    c) let dict: [String: Int] = [:]
    d) All of the above
  2. What happens if you try to access a non-existent key in a dictionary?
    a) Error
    b) Returns nil
    c) Returns default value
    d) None of the above
  3. Which method removes a key-value pair from a dictionary?
    a) remove(key:)
    b) deleteValue(forKey:)
    c) removeValue(forKey:)
    d) removeElement(key:)
  4. How can you retrieve all keys from a dictionary?
    a) dict.keys
    b) dict.getKeys()
    c) dict.allKeys
    d) None of the above
  5. Given the dictionary:swiftCopy codelet scores = ["Math": 90, "Science": 95] How can you update the value for key "Math" to 100?
    a) scores.updateValue(100, forKey: "Math")
    b) scores["Math"] = 100
    c) Both a and b
    d) None of the above

Topic 3: Sets – Unique Elements and Membership

  1. Which of the following declares a set of integers?
    a) var set: Set = [1, 2, 3]
    b) var set = Set<Int>()
    c) Both a and b
    d) None of the above
  2. Which operation checks if a set contains an element?
    a) set.contains(element)
    b) set.has(element)
    c) set.includes(element)
    d) set.elementExists()
  3. How do you combine two sets in Swift?
    a) set1.merge(set2)
    b) set1.formUnion(set2)
    c) set1.append(set2)
    d) set1.combine(set2)
  4. Which operation returns elements that are common to two sets?
    a) union()
    b) intersection()
    c) difference()
    d) exclusive()
  5. What does the following code return?swiftCopy codelet setA: Set = [1, 2, 3] let setB: Set = [3, 4, 5] setA.isSubset(of: setB) a) true
    b) false
    c) Error
    d) None of the above

Topic 4: Tuples – Grouping Values

  1. Which of the following correctly declares a tuple?
    a) let tuple = ("John", 25)
    b) let tuple: (String, Int)
    c) let tuple = (name: "John", age: 25)
    d) All of the above
  2. How do you access the second element of a tuple?
    a) tuple[1]
    b) tuple.1
    c) tuple.second
    d) tuple["second"]
  3. Can tuples have named elements?
    a) Yes, always
    b) No, never
    c) Yes, optionally
    d) None of the above
  4. What is the output of the following code?swiftCopy codelet person = (name: "Alice", age: 30) print(person.name) a) Alice
    b) 30
    c) Error
    d) None of the above
  5. Which operation creates a new tuple by combining two tuples?
    a) Using + operator
    b) Using tuple1.combine(tuple2)
    c) Using (tuple1, tuple2)
    d) None of the above

Answers Table

QNoAnswer
1d) All of the above
2b) array.append(element)
3b) removeLast()
4b) Banana
5a) Using for element in array
6d) All of the above
7b) Returns nil
8c) removeValue(forKey:)
9a) dict.keys
10c) Both a and b
11c) Both a and b
12a) set.contains(element)
13b) set1.formUnion(set2)
14b) intersection()
15b) false
16d) All of the above
17b) tuple.1
18c) Yes, optionally
19a) Alice
20c) Using (tuple1, tuple2)

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top