This quiz covers the fundamentals of working with collections in Swift, specifically arrays and dictionaries. These questions cover a range of topics including the initialization and manipulation of arrays and dictionaries, their properties and methods, and specific usage scenarios like mutable vs immutable collections, accessing and modifying data, and iterating through collections.
1.
Which of the following data types can be used as keys in a Swift dictionary?
2.
Which property of a collection returns the number of elements it contains?
3.
How do you iterate over the key-value pairs of a dictionary in Swift?
4.
What is the output of the following code?
var colorDict = [1: "red", 2: "green"]
colorDict[3] = "blue"
print(colorDict)
5.
Which syntax correctly declares a dictionary with default values?
6.
What happens when you assign nil to a dictionary key?
7.
How do you remove a key-value pair from a dictionary in Swift?
8.
What does the following code do?
bookDict["200-532874"] = "War and Peace"
9.
What will happen if you try to access a key in a dictionary that does not exist?
10.
Which of the following is true about collections in Swift?
11.
How can you create a dictionary from two arrays in Swift?
12.
What is the purpose of the randomElement() method in Swift arrays?
13.
What is the result of the following code?
let macArray = ["MacBook Pro", "Mac mini", "MacBook Air", "Mac Studio"]
let shuffledMacs = macArray.shuffled()
14.
Which syntax correctly initializes an empty dictionary in Swift?
15.
How can you insert an item at a specific index in an array?
16.
What will be printed after executing the following code?
let macArray = ["MacBook Pro", "Mac Mini", "MacBook Air", "Mac Studio"]
macArray[1] = "Mac Pro"
print(macArray[1])
17.
Which method can be used to remove the last element of an array?
18.
How do you check if an array is empty in Swift?
19.
What is the correct way to append an item to an array in Swift?
20.
Which method can be used to randomly shuffle the contents of an array?
21.
Which syntax is used to initialize an empty array in Swift?
22.
How can you make a collection immutable in Swift?
Congratulations on completing the Swift Arrays and Collections Quiz.
Click the Submit button to review your results.
Enter your email address below if you would like to receive a copy of your test results.