What does it mean that structs are value types in Swift?
It means when you assign a struct to a new variable or pass it to a function, Swift makes a copy of the struct instead of sharing the same instance.
Click to reveal answer
beginner
How does assigning one struct variable to another affect their data?
Each variable gets its own separate copy of the data. Changing one does not affect the other.
Click to reveal answer
intermediate
Compare structs and classes in Swift regarding assignment behavior.
Structs are value types and are copied on assignment. Classes are reference types and share the same instance when assigned.
Click to reveal answer
intermediate
Why might Swift use value types like structs for some data?
Value types help avoid unexpected changes because each copy is independent. This makes code safer and easier to understand.
Click to reveal answer
beginner
If you change a property of a struct after assigning it to another variable, what happens?
Only the struct you changed is affected. The other variable still has the original data because it has its own copy.
Click to reveal answer
What happens when you assign a Swift struct to a new variable?
AA new copy of the struct is created.
BBoth variables point to the same struct instance.
CThe struct is converted to a class automatically.
DThe original struct is deleted.
✗ Incorrect
Structs are value types, so assignment creates a new copy.
If you modify a struct after assigning it to another variable, what happens to the other variable?
AIt stays the same because each variable has its own copy.
BIt also changes because they share the same data.
CIt becomes nil automatically.
DIt throws a runtime error.
✗ Incorrect
Each struct variable holds its own copy, so changes do not affect others.
Which of these is a reference type in Swift?
AEnum
BStruct
CClass
DTuple
✗ Incorrect
Classes are reference types; structs, enums, and tuples are value types.
Why are structs considered safer for some data than classes?
ABecause they use less memory.
BBecause they are copied on assignment, avoiding shared state bugs.
CBecause they can inherit from other structs.
DBecause they run faster than classes.
✗ Incorrect
Copying on assignment prevents unexpected changes from shared references.
Which keyword defines a struct in Swift?
Aclass
Benum
Cvalue
Dstruct
✗ Incorrect
The keyword 'struct' is used to define a struct in Swift.
Explain in your own words what it means that structs are value types and how assignment works.
Think about what happens when you give a copy of a drawing to a friend.
You got /4 concepts.
Describe the difference between structs and classes in Swift regarding how they handle data when assigned to new variables.
Imagine copying a photo versus sharing a link to the photo.
You got /4 concepts.
Practice
(1/5)
1. What happens when you assign one struct variable to another in Swift?
easy
A. A new copy of the struct data is created.
B. Both variables point to the same data in memory.
C. The original struct is deleted.
D. The struct becomes a class automatically.
Solution
Step 1: Understand struct assignment behavior
Structs in Swift are value types, so assigning one struct to another copies the data.
Step 2: Compare with reference types
Unlike classes, structs do not share references; each variable has its own copy.
Final Answer:
A new copy of the struct data is created. -> Option A
Quick Check:
Struct assignment = copy data [OK]
Hint: Structs copy data on assignment, not references [OK]
Common Mistakes:
Thinking structs share data like classes
Assuming assignment links variables
Confusing structs with reference types
2. Which of the following is the correct way to define a struct in Swift?
easy
A. struct Person = { var name: String }
B. struct Person { var name: String }
C. class Person { var name: String }
D. struct Person() { var name: String }
Solution
Step 1: Recall Swift struct syntax
Structs are defined using the keyword struct followed by the name and braces containing properties.
Step 2: Identify correct syntax
The correct syntax is struct Person { var name: String }. Using = after the name is invalid, using class defines a class instead of a struct, and adding parentheses like Person() is incorrect.
Final Answer:
struct Person { var name: String } -> Option B
Quick Check:
Struct syntax = struct Name { properties } [OK]
Hint: Use 'struct Name { }' to define structs [OK]
Common Mistakes:
Using class keyword instead of struct
Adding parentheses after struct name
Using equals sign in struct definition
3. Consider 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)
What is the output?
medium
A. 20
B. Compilation error
C. 5
D. 10
Solution
Step 1: Understand struct copying on assignment
Assigning p1 to p2 copies the data, so p2 is a separate copy.
Step 2: Analyze mutation effect
Changing p2.x to 20 does not affect p1.x, which remains 5.
Final Answer:
5 -> Option C
Quick Check:
Struct copy means original unchanged [OK]
Hint: Changing copy doesn't affect original struct [OK]
Common Mistakes:
Assuming p1.x changes to 20
Confusing structs with reference types
Expecting runtime error on assignment
4. This Swift code tries to update a struct inside a function:
struct Counter { var count: Int }
func increment(counter: Counter) {
var counter = counter
counter.count += 1
}
var c = Counter(count: 0)
increment(counter: c)
print(c.count)
What is the output and why?
medium
A. 0, because the function modifies a copy, not the original
B. 1, because the function updates the original struct
C. Compilation error due to modifying parameter
D. Runtime error due to immutable struct
Solution
Step 1: Understand parameter passing for structs
Structs are passed by value, so the function receives a copy of c.
Step 2: Analyze mutation inside function
The function modifies its local copy, but c outside remains unchanged.
Final Answer:
0, because the function modifies a copy, not the original -> Option A
Quick Check:
Structs passed by value = original unchanged [OK]
Hint: Structs passed to functions are copies, not originals [OK]
Common Mistakes:
Expecting original struct to change
Thinking function parameter is reference
Assuming compilation or runtime error
5. You want to create a Swift struct that tracks a score and allows updating it inside a method. Which approach correctly updates the struct's property?
hard
A. Use a global variable to hold the struct and update it.
B. Use a non-mutating method and assign a new struct to self.
C. Declare the struct as a class to allow property changes.
D. Use a mutating method inside the struct to change the property.
Solution
Step 1: Recall how to modify struct properties
Structs are value types; to modify properties inside methods, the method must be marked mutating.
Step 2: Evaluate options
Use a mutating method inside the struct to change the property. correctly uses a mutating method. Use a non-mutating method and assign a new struct to self. is incorrect because non-mutating methods cannot change properties. Declare the struct as a class to allow property changes. changes the type to class, which is different. Use a global variable to hold the struct and update it. uses a global variable, which is not ideal.
Final Answer:
Use a mutating method inside the struct to change the property. -> Option D
Quick Check:
Mutating methods update struct properties [OK]
Hint: Mark methods mutating to change struct properties [OK]
Common Mistakes:
Trying to modify properties in non-mutating methods
Confusing structs with classes for mutability
Using global variables instead of methods
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.