Recall & Review
beginner
What does reference sharing mean in Swift?
Reference sharing means multiple variables point to the same instance of a class. Changing the instance through one variable affects all others pointing to it.
Click to reveal answer
beginner
What is a side effect in programming?
A side effect happens when a function or operation changes something outside its own scope, like modifying a shared object or variable.
Click to reveal answer
intermediate
How do
struct and class differ in Swift regarding reference sharing?Structs are value types, so each variable has its own copy. Classes are reference types, so variables share the same instance and changes affect all references.
Click to reveal answer
beginner
What happens when you modify a class instance through one reference in Swift?All references to that instance see the change because they point to the same object in memory.
Click to reveal answer
intermediate
Why can reference sharing cause bugs in Swift programs?
Because changes through one reference affect others unexpectedly, leading to unintended side effects and harder-to-find bugs.
Click to reveal answer
In Swift, which type shares references by default?
✗ Incorrect
Classes are reference types, so variables share the same instance. Structs, enums, and tuples are value types and do not share references.
What is a side effect?
✗ Incorrect
A side effect means modifying something outside the function, like changing a shared object or global variable.
If two variables reference the same class instance and you change a property via one variable, what happens?
✗ Incorrect
Because both variables point to the same instance, changes through one are visible through the other.
Which Swift type avoids side effects by copying data on assignment?
✗ Incorrect
Structs are value types and copy their data, so changes do not affect other copies.
Why is understanding reference sharing important?
✗ Incorrect
Knowing how references work helps prevent bugs caused by unexpected changes to shared data.
Explain reference sharing in Swift and how it can lead to side effects.
Think about how classes work compared to structs.
You got /4 concepts.
Describe the difference between value types and reference types in Swift with examples.
Consider what happens when you assign or pass these types.
You got /4 concepts.