0
0
Swiftprogramming~5 mins

Collection mutability tied to let/var in Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AThe array can be changed anytime
BThe array elements can be changed but not the array size
CThe array is deleted automatically
DThe array cannot be changed after creation
If you declare a dictionary with var, what can you do?
AOnly read elements
BOnly remove elements
CAdd, remove, or modify elements
DOnly add elements
Can you modify the properties of class instances inside a let-declared array?
AYes, properties of class instances can be changed
BNo, nothing can be changed
COnly if the array is declared with <code>var</code>
DOnly if the class is a struct
What keyword in Swift declares a mutable collection?
Alet
Bvar
Cconst
Dmutable
What kind of error do you get if you try to append to a let-declared array?
ACompile-time error
BRuntime error
CNo error
DLogical error
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.