Recall & Review
beginner
What is type inference in Swift when creating an array?
Type inference means Swift automatically figures out the type of the array based on the values you put inside it, so you don't have to write the type explicitly.
Click to reveal answer
beginner
How do you create an empty array of integers in Swift?
You can create it by writing:
var numbers: [Int] = [] or var numbers = [Int]().Click to reveal answer
beginner
What happens if you write <code>let fruits = ["Apple", "Banana", "Cherry"]</code> in Swift?Swift infers that
fruits is an array of strings: [String].Click to reveal answer
intermediate
Can Swift infer the type of an array if it contains mixed types like
[1, "two", 3]?No, Swift requires all elements in an array to be of the same type. Mixed types cause a compile error.
Click to reveal answer
beginner
How do you create an array with repeated values in Swift?
Use
Array(repeating: value, count: number). For example, Array(repeating: 0, count: 3) creates [0, 0, 0].Click to reveal answer
What type does Swift infer for
let numbers = [1, 2, 3]?✗ Incorrect
Swift sees all values are integers, so it infers the array type as [Int].
Which of these is a valid way to create an empty array of strings in Swift?
✗ Incorrect
You must specify the type when creating an empty array, like var names: [String] = [].
What happens if you try to create
let mixed = [1, "two", 3] in Swift?✗ Incorrect
Swift arrays must have elements of the same type; mixed types cause a compile error.
How do you create an array with five zeros in Swift?
✗ Incorrect
Use Array(repeating: 0, count: 5) to create an array with five zeros.
If you write
var items = ["pen", "pencil"], what is the type of items?✗ Incorrect
Swift infers the type as [String] because all elements are strings.
Explain how Swift uses type inference when you create an array with values.
Think about how Swift guesses the type based on what you put inside the array.
You got /3 concepts.
Describe two ways to create an empty array in Swift and why you might need to specify the type.
Remember empty arrays have no values, so Swift can't guess the type automatically.
You got /3 concepts.