A struct is a value type that groups related data and functions together. It is copied when assigned or passed around, unlike classes which are reference types.
Click to reveal answer
beginner
Why are structs preferred over classes in Swift for many cases?
Structs are preferred because they are safer (copied by value), simpler, and avoid issues with shared mutable state. They also offer better performance in many cases.
Click to reveal answer
intermediate
How does value semantics of structs help in Swift programming?
Value semantics means each copy of a struct is independent. This prevents unexpected changes from other parts of code, making programs easier to understand and debug.
Click to reveal answer
intermediate
Name two benefits of using structs in Swift related to memory management.
1. Structs are often allocated on the stack which is faster. 2. They avoid reference counting overhead that classes have, improving performance.
Click to reveal answer
intermediate
When should you choose a class over a struct in Swift?
Choose a class when you need inheritance, reference semantics, or to share mutable state across different parts of your program.
Click to reveal answer
What type of semantics do Swift structs use?
AShared semantics
BReference semantics
CPointer semantics
DValue semantics
✗ Incorrect
Structs use value semantics, meaning each copy is independent.
Why do structs often perform better than classes in Swift?
ABecause they support inheritance
BBecause they use reference counting
CBecause they are allocated on the stack
DBecause they are slower
✗ Incorrect
Structs are usually allocated on the stack, which is faster than heap allocation used by classes.
Which of these is NOT a reason to prefer structs in Swift?
ANeed for inheritance
BSimpler code with value types
CAvoiding shared mutable state
DBetter safety and predictability
✗ Incorrect
Inheritance is a feature of classes, not structs.
When should you use a class instead of a struct in Swift?
AWhen you want value semantics
BWhen you need to share mutable state
CWhen you want faster performance
DWhen you want simpler code
✗ Incorrect
Classes allow shared mutable state through reference semantics.
What happens when you assign a struct to a new variable in Swift?
AA copy of the struct is created
BA reference to the original struct is created
CThe original struct is modified
DThe struct is deleted
✗ Incorrect
Assigning a struct creates a new independent copy.
Explain why Swift developers often prefer structs over classes.
Think about how copying and memory work differently for structs and classes.
You got /5 concepts.
Describe scenarios where using a class is better than a struct in Swift.
Consider when you want multiple parts of your program to see the same object changes.
You got /4 concepts.
Practice
(1/5)
1. Why are structs preferred over classes in Swift for small data types?
easy
A. Because structs allow inheritance and polymorphism
B. Because structs use more memory than classes
C. Because structs copy data, avoiding unexpected changes from shared references
D. Because structs require manual memory management
Solution
Step 1: Understand data copying in structs
Structs in Swift create a copy of data when assigned or passed, so changes to one copy don't affect others.
Step 2: Compare with classes and shared references
Classes use references, so multiple variables can point to the same data, causing unexpected changes.
Final Answer:
Because structs copy data, avoiding unexpected changes from shared references -> Option C
2. Which of the following is the correct way to define a struct in Swift?
easy
A. struct Person { var name: String }
B. class Person { var name: String }
C. struct Person = (name: String)
D. struct Person: String { var name }
Solution
Step 1: Recall Swift struct syntax
Structs are defined using the keyword struct followed by the name and curly braces containing properties.
Step 2: Check each option
struct Person { var name: String } correctly uses struct Person { var name: String }. class Person { var name: String } uses class, not struct. Options C and D have invalid syntax.
Final Answer:
struct Person { var name: String } -> Option A
Quick Check:
Correct struct syntax = struct Person { var name: String } [OK]
Hint: Use 'struct Name { var property: Type }' for structs [OK]
Common Mistakes:
Using 'class' keyword instead of 'struct'
Incorrect property declaration syntax
Trying to assign struct like a tuple
3. What will be the output of this Swift code?
struct Point { var x: Int; var y: Int }
var p1 = Point(x: 5, y: 10)
var p2 = p1
p2.x = 20
print(p1.x)
medium
A. 5
B. 20
C. 10
D. Compilation error
Solution
Step 1: Understand struct copying behavior
Assigning p1 to p2 copies the data. They are independent after assignment.
Step 2: Analyze the change to p2.x
Changing p2.x to 20 does not affect p1.x, which remains 5.
Final Answer:
5 -> Option A
Quick Check:
Struct copy means p1.x stays 5 [OK]
Hint: Struct assignment copies data; original stays unchanged [OK]
Common Mistakes:
Thinking p1.x changes when p2.x changes
Confusing structs with class reference behavior
Expecting runtime error due to assignment
4. Identify the error in this Swift code using structs:
struct Rectangle {
var width: Int
var height: Int
}
var rect1 = Rectangle(width: 10, height: 20)
var rect2 = rect1
rect2.width = 30
print(rect1.width)
medium
A. rect2.width cannot be changed because structs are immutable
B. rect1.width prints 30 because structs share references
C. Compilation error due to missing initializer
D. No error; rect1.width prints 10 because structs copy data
Solution
Step 1: Check mutability of struct instances
Since rect2 is a variable (var), its properties can be changed.
Step 2: Understand copying behavior
Assigning rect1 to rect2 copies the data. Changing rect2.width does not affect rect1.width.
Final Answer:
No error; rect1.width prints 10 because structs copy data -> Option D
Quick Check:
Struct copy means rect1.width stays 10 [OK]
Hint: Structs copy data; changing copy doesn't affect original [OK]
Common Mistakes:
Assuming structs are immutable by default
Thinking structs share references like classes
Expecting compilation error for missing initializer
5. You want to create a simple data type to store a user's profile with name and age. Why is using a struct better than a class in Swift for this case?
hard
A. Structs use reference counting, improving performance for user data
B. Structs provide automatic copying, preventing accidental shared changes in user data
C. Structs allow inheritance, making it easier to extend user profiles
D. Structs require manual memory management, giving more control over user data
Solution
Step 1: Consider data safety for user profiles
User profiles are simple data that should not change unexpectedly when copied or passed around.
Step 2: Compare structs and classes for this use
Structs copy data automatically, so each copy is independent, avoiding bugs from shared references common in classes.
Final Answer:
Structs provide automatic copying, preventing accidental shared changes in user data -> Option B
Quick Check:
Struct copy = safer user profile data [OK]
Hint: Use structs for simple data to avoid shared reference bugs [OK]