0
0
Swiftprogramming~20 mins

Comparing structs vs classes decision in Swift - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Struct vs Class Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of modifying struct vs class instances

Consider the following Swift code. What will be printed?

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

class Location {
    var x: Int
    var y: Int
    init(x: Int, y: Int) {
        self.x = x
        self.y = y
    }
}

var p1 = Point(x: 1, y: 1)
var p2 = p1
p2.x = 5

var l1 = Location(x: 1, y: 1)
var l2 = l1
l2.x = 5

print("p1.x = \(p1.x), p2.x = \(p2.x)")
print("l1.x = \(l1.x), l2.x = \(l2.x)")
A
p1.x = 1, p2.x = 5
l1.x = 5, l2.x = 5
B
p1.x = 5, p2.x = 5
l1.x = 1, l2.x = 5
C
p1.x = 1, p2.x = 1
l1.x = 1, l2.x = 5
D
p1.x = 5, p2.x = 1
l1.x = 5, l2.x = 1
Attempts:
2 left
💡 Hint

Remember that structs are value types and classes are reference types in Swift.

🧠 Conceptual
intermediate
1:30remaining
Choosing between struct and class for data modeling

You want to model a simple data type that represents a color with red, green, and blue values. You do not need inheritance or shared mutable state. Which is the best choice?

AUse a struct because classes cannot have stored properties.
BUse a class because structs cannot have methods.
CUse a class because it supports inheritance and reference semantics.
DUse a struct because it is a value type and better for simple data containers.
Attempts:
2 left
💡 Hint

Think about whether you want copies or shared references.

🔧 Debug
advanced
2:00remaining
Why does this class code cause unexpected behavior?

Look at this Swift code snippet. Why does modifying person2 also change person1?

Swift
class Person {
    var name: String
    init(name: String) {
        self.name = name
    }
}

var person1 = Person(name: "Alice")
var person2 = person1
person2.name = "Bob"

print(person1.name)
print(person2.name)
ABecause classes are reference types, both variables point to the same object.
BBecause structs are copied on assignment, person2 is a copy of person1.
CBecause the name property is immutable, it cannot be changed.
DBecause person2 is a different object, changing it does not affect person1.
Attempts:
2 left
💡 Hint

Think about how classes and structs behave differently when assigned.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in struct with mutating method

Which option correctly fixes the syntax error in this Swift struct method that modifies a property?

Swift
struct Counter {
    var count = 0
    func increment() {
        count += 1
    }
}
AChange <code>var count</code> to <code>let count</code>.
BAdd the <code>mutating</code> keyword before <code>func increment()</code>.
CMake <code>increment()</code> a static method.
DRemove the <code>func</code> keyword.
Attempts:
2 left
💡 Hint

Struct methods that change properties must be marked specially.

🚀 Application
expert
2:30remaining
Deciding struct vs class for a complex app model

You are designing a Swift app with a model representing a user profile that can be shared and updated across multiple views. The profile has mutable properties and needs identity tracking. Which is the best choice?

AUse a struct because it is copied on assignment and safer for concurrency.
BUse a class but make all properties immutable.
CUse a class because it supports reference semantics and identity tracking.
DUse a struct with static properties to share data globally.
Attempts:
2 left
💡 Hint

Consider if you need shared mutable state and identity.