Bird
Raised Fist0
Swiftprogramming~20 mins

Structs are value types (copy on assign) in Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Swift Struct Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Swift code?

Consider this Swift code using a struct. What will be printed?

Swift
struct Point {
    var x: Int
    var y: Int
}

var p1 = Point(x: 5, y: 10)
var p2 = p1
p2.x = 20
print(p1.x, p2.x)
A5 20
B20 20
C5 5
D20 5
Attempts:
2 left
💡 Hint

Remember that structs are value types and are copied when assigned.

❓ Predict Output
intermediate
2:00remaining
What is the value of 'a.count' after this code runs?

Look at this Swift code. What is the value of a.count after execution?

Swift
struct Counter {
    var count: Int
}

var a = Counter(count: 1)
var b = a
b.count += 1
ACompilation error
B2
C0
D1
Attempts:
2 left
💡 Hint

Think about whether a and b share the same data or not.

🔧 Debug
advanced
2:30remaining
Why does this code print different values?

Examine this Swift code. Why do the two print statements output different values?

Swift
struct Score {
    var points: Int
}

var s1 = Score(points: 10)
var s2 = s1
s2.points = 20
print(s1.points)
print(s2.points)
ABecause s2 was not properly assigned, it still references s1, so both print 10.
BBecause structs are value types, s1 and s2 are independent copies, so s1.points remains 10.
CBecause structs are reference types, s1 and s2 point to the same data, so both print 20.
DBecause the print statements are inside a closure capturing s1.points, both print 10.
Attempts:
2 left
💡 Hint

Recall how structs behave when assigned to new variables.

📝 Syntax
advanced
2:00remaining
Which option causes a compile-time error?

Given this struct, which code snippet will cause a compile-time error?

Swift
struct Person {
    var name: String
    var age: Int
}

let p1 = Person(name: "Alice", age: 30)
var p2 = p1
Ap1.name.append(" Smith")
Bp2 = Person(name: "Charlie", age: 25)
Cp2.age += 1
Dp2.name = "Bob"
Attempts:
2 left
💡 Hint

Consider mutability of struct instances and their properties.

🚀 Application
expert
3:00remaining
How many items are in the array after this code runs?

Consider this Swift code using structs and arrays. How many elements does arr2 contain after execution?

Swift
struct Item {
    var id: Int
}

var arr1 = [Item(id: 1), Item(id: 2)]
var arr2 = arr1
arr2.append(Item(id: 3))
A1
B2
C3
DCompilation error
Attempts:
2 left
💡 Hint

Think about how arrays and structs behave when assigned and modified.

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

  1. Step 1: Understand struct assignment behavior

    Structs in Swift are value types, so assigning one struct to another copies the data.
  2. Step 2: Compare with reference types

    Unlike classes, structs do not share references; each variable has its own copy.
  3. Final Answer:

    A new copy of the struct data is created. -> Option A
  4. 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

  1. Step 1: Recall Swift struct syntax

    Structs are defined using the keyword struct followed by the name and braces containing properties.
  2. 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.
  3. Final Answer:

    struct Person { var name: String } -> Option B
  4. 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

  1. Step 1: Understand struct copying on assignment

    Assigning p1 to p2 copies the data, so p2 is a separate copy.
  2. Step 2: Analyze mutation effect

    Changing p2.x to 20 does not affect p1.x, which remains 5.
  3. Final Answer:

    5 -> Option C
  4. 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

  1. Step 1: Understand parameter passing for structs

    Structs are passed by value, so the function receives a copy of c.
  2. Step 2: Analyze mutation inside function

    The function modifies its local copy, but c outside remains unchanged.
  3. Final Answer:

    0, because the function modifies a copy, not the original -> Option A
  4. 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

  1. Step 1: Recall how to modify struct properties

    Structs are value types; to modify properties inside methods, the method must be marked mutating.
  2. 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.
  3. Final Answer:

    Use a mutating method inside the struct to change the property. -> Option D
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard