Recall & Review
beginner
What happens when you declare a collection with
let in Swift?Declaring a collection with
let makes it immutable. You cannot add, remove, or change elements after initialization.Click to reveal answer
beginner
How does declaring a collection with
var affect its mutability?Declaring a collection with
var makes it mutable. You can add, remove, or modify elements freely.Click to reveal answer
intermediate
Can you modify the elements of a collection declared with
let if the elements themselves are reference types?Yes, if the elements are reference types (like classes), you can modify their internal properties even if the collection is declared with
let. But you cannot change the collection structure itself.Click to reveal answer
beginner
Why does Swift tie collection mutability to
let and var?Swift uses
let and var to clearly express intent. let means the collection won't change, helping prevent bugs. var allows changes when needed.Click to reveal answer
beginner
What error occurs if you try to modify a
let-declared collection?You get a compile-time error saying you cannot use mutating methods on an immutable value.
Click to reveal answer
What does declaring an array with
let in Swift mean?✗ Incorrect
Declaring with
let makes the array immutable, so you cannot add, remove, or change elements.If you declare a dictionary with
var, what can you do?✗ Incorrect
Using
var allows full mutability of the collection.Can you modify the properties of class instances inside a
let-declared array?✗ Incorrect
The collection is immutable, but the reference type elements can have their properties changed.
What keyword in Swift declares a mutable collection?
✗ Incorrect
var declares a mutable collection.What kind of error do you get if you try to append to a
let-declared array?✗ Incorrect
Swift prevents this at compile time to avoid mutating an immutable collection.
Explain how
let and var affect collection mutability in Swift.Think about whether you can change the collection after creating it.
You got /4 concepts.
Describe what happens when you try to modify a
let-declared collection and why Swift enforces this.Consider Swift's goal to avoid bugs by enforcing immutability.
You got /4 concepts.