Recall & Review
beginner
What is a key difference between structs and classes in Swift regarding memory management?
Structs are value types, so they are copied when assigned or passed around. Classes are reference types, so they share a single instance in memory.
Click to reveal answer
beginner
When should you choose a struct over a class in Swift?Choose a struct when you want simple data containers that are copied on assignment, have no inheritance, and are thread-safe by default.
Click to reveal answer
beginner
What feature do classes have that structs do not in Swift?
Classes support inheritance, allowing one class to inherit properties and methods from another. Structs do not support inheritance.Click to reveal answer
intermediate
How does mutability differ between structs and classes in Swift?
Structs require the 'mutating' keyword to change properties inside methods, and instances must be declared 'var' to be mutable. Classes allow property changes without 'mutating' and can be mutable even if declared with 'let'.
Click to reveal answer
intermediate
Why might you prefer classes when working with shared, mutable state?
Because classes are reference types, multiple parts of your code can share and modify the same instance, which is useful for shared mutable state.
Click to reveal answer
Which of the following is true about Swift structs?
✗ Incorrect
Structs are value types, so they are copied on assignment. They do not support inheritance and cannot be mutated if declared with let.
What keyword is required to modify properties inside a struct method?
✗ Incorrect
The 'mutating' keyword is required in struct methods that modify properties.
Why might you choose a class over a struct?
✗ Incorrect
Classes support inheritance, which structs do not.
If you want multiple parts of your app to share and modify the same data instance, which should you use?
✗ Incorrect
Classes are reference types, so they allow shared mutable state.
Which statement about mutability is correct?
✗ Incorrect
Only struct methods that modify properties need the 'mutating' keyword. Classes do not require it.
Explain when you would choose a struct versus a class in Swift and why.
Think about how data is shared or copied and if you need inheritance.
You got /5 concepts.
Describe the impact of using structs on thread safety compared to classes.
Consider how copying versus sharing affects concurrent access.
You got /5 concepts.