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?
AThey support inheritance.
BThey are copied when assigned to a new variable.
CThey are reference types.
DThey can be mutated even if declared with let.
✗ 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?
Amutating
Bvar
Cchange
Dmutate
✗ Incorrect
The 'mutating' keyword is required in struct methods that modify properties.
Why might you choose a class over a struct?
AYou want value semantics.
BYou want automatic copying.
CYou need inheritance.
DYou want thread safety by default.
✗ 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?
AClass
BStruct
CEnum
DTuple
✗ Incorrect
Classes are reference types, so they allow shared mutable state.
Which statement about mutability is correct?
AClasses require mutating keyword for property changes.
BClass instances declared with let cannot have their properties changed.
CStruct instances declared with let can have their properties changed.
DStruct methods that modify properties must be marked mutating.
✗ 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.
Practice
(1/5)
1. Which statement best describes when to use a struct instead of a class in Swift?
easy
A. Use struct when you want to share data between multiple parts of your app.
B. Use struct when you need inheritance and shared references.
C. Use struct for simple data that should be copied when assigned or passed.
D. Use struct only for UI elements like views.
Solution
Step 1: Understand struct behavior
Structs are value types, meaning they are copied when assigned or passed around.
Step 2: Compare with class behavior
Classes are reference types, shared by multiple references and support inheritance.
Final Answer:
Use struct for simple data that should be copied when assigned or passed. -> Option C
2. Which of the following is the correct way to declare a struct in Swift?
easy
A. class Person { var name: String }
B. struct Person { var name: String }
C. struct Person: Class { var name: String }
D. class Person: struct { var name: String }
Solution
Step 1: Recall struct declaration syntax
In Swift, structs are declared with the keyword struct followed by the name and braces.
Step 2: Check each option
struct Person { var name: String } is correct. Using class is incorrect for structs. Structs cannot inherit from Class, and classes cannot inherit from struct.
Final Answer:
struct Person { var name: String } -> Option B
Quick Check:
Struct declaration = struct Name { } [OK]
Hint: Structs start with 'struct' keyword, not 'class' [OK]
Common Mistakes:
Using 'class' keyword to declare a struct
Trying to inherit from a class in a struct
Mixing struct and class syntax
3. Consider this Swift code:
struct Point {
var x: Int
var y: Int
}
var p1 = Point(x: 1, y: 2)
var p2 = p1
p2.x = 10
print(p1.x)
What is the output?
medium
A. 1
B. 10
C. 0
D. Compilation error
Solution
Step 1: Understand struct copying behavior
Assigning p1 to p2 copies the data because Point is a struct (value type).
Step 2: Analyze mutation effect
Changing p2.x to 10 does not affect p1.x, which remains 1.
Final Answer:
1 -> Option A
Quick Check:
Struct copy means original unchanged = 1 [OK]
Hint: Structs copy on assignment; original stays same [OK]
Common Mistakes:
Assuming p1.x changes when p2.x changes
Confusing structs with classes (reference types)
Expecting runtime error on mutation
4. This Swift code tries to share data between two variables:
class Counter {
var count = 0
}
var c1 = Counter()
var c2 = c1
c2.count = 5
print(c1.count)
But the output is not as expected. What is the problem?
medium
A. Counter is a class, so c1 and c2 share the same instance
B. Counter is a class, but c2 is a copy, so changes don't reflect
C. Counter should be a struct to share data
D. Counter needs to conform to a protocol to share data
Solution
Step 1: Identify class reference behavior
Classes are reference types, so c1 and c2 point to the same object.
Step 2: Understand shared mutation
Changing c2.count to 5 changes the shared instance, so c1.count also becomes 5.
Final Answer:
Counter is a class, so c1 and c2 share the same instance -> Option A
Quick Check:
Classes share references = shared data [OK]
Hint: Classes share instances; structs copy data [OK]
Common Mistakes:
Thinking classes copy on assignment
Expecting structs to share data
Believing protocols affect sharing automatically
5. You are designing a Swift app that tracks user sessions. Each session has a unique ID and mutable state shared across multiple parts of the app. Which type should you choose and why?
Options: A) Use a struct because it copies data and avoids bugs. B) Use a class because it allows shared mutable state with identity. C) Use a struct with inheritance to share state. D) Use a class but avoid mutability to keep data safe.
hard
A. Use a struct because it copies data and avoids bugs.
B. Use a class but avoid mutability to keep data safe.
C. Use a struct with inheritance to share state.
D. Use a class because it allows shared mutable state with identity.
Solution
Step 1: Analyze requirements for shared mutable state
The session data must be shared and mutable across multiple parts, requiring a single shared instance.
Step 2: Choose between struct and class
Structs copy data, so changes in one copy won't reflect elsewhere. Classes share references and support identity, fitting the need.
Step 3: Evaluate other options
Structs do not support inheritance, so Use a struct with inheritance to share state. is invalid. Avoiding mutability in classes (Use a class but avoid mutability to keep data safe.) contradicts the requirement for mutable shared state.
Final Answer:
Use a class because it allows shared mutable state with identity. -> Option D
Quick Check:
Shared mutable state = class [OK]
Hint: Shared mutable data needs class, not struct [OK]
Common Mistakes:
Choosing struct for shared mutable state
Thinking structs support inheritance
Avoiding mutability in classes when needed
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.