Swift Sets and Dictionaries Quiz

Topics: Swift Sets and Swift Dictionaries

Swift Quiz #06 - Swift Sets and Dictionaries

6 mins read

1. Which code snippet correctly declares an empty set of integers in Swift?

2. What is the output?

 var dict = ["apple": 3, "banana": 5, "orange": 2]
dict["banana"] = nil

3. What is the output?

 let set1: Set<Int> = [1, 2, 3, 4, 5]
let set2: Set<Int> = [1, 2]
let isSuperset = set1.isSuperset(of: set2)
print(isSuperset)

4. What is the output?

 let set1: Set<String> = ["Apple", "Banana", "Orange"]
let set2: Set<String> = ["Banana", "Orange", "Apple"]
let areEqual = set1 == set2
print(areEqual)

5. What is the output?

 let set1: Set<String> = ["Apple", "Banana", "Orange"]
let set2: Set<String> = ["Banana", "Orange", "Apple"]
let isStrictSubset = set1.isStrictSubset(of: set2)
print(isStrictSubset)

6. What is the output?

 var dictionary: [String: [Int]] = ["Fruits": [1, 2, 3], "Vegetables": [4, 5, 6]]
let values = Array(dictionary.values.joined())
print(values.count)

7. What is the output?

 let set1: Set<Int> = [1, 2, 3, 4]
let set2: Set<Int> = [3, 4, 5, 6]
let symmetricDifference = set1.symmetricDifference(set2) print(symmetricDifference.count)

8. What is the output?

 let set1: Set<Int> = [1, 2, 3 ]
let set2: Set<Int> = [1, 2, 3, 4, 5]
let isStrictSubset = set1.isStrictSubset(of: set2)
print(isStrictSubset)

9. What is the difference between superset and strictSuperset?