Bird
Raised Fist0
Swiftprogramming~20 mins

Comparing structs vs classes decision in Swift - Practice Questions

Choose your learning style10 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.

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

  1. Step 1: Understand struct behavior

    Structs are value types, meaning they are copied when assigned or passed around.
  2. Step 2: Compare with class behavior

    Classes are reference types, shared by multiple references and support inheritance.
  3. Final Answer:

    Use struct for simple data that should be copied when assigned or passed. -> Option C
  4. Quick Check:

    Structs = copied data [OK]
Hint: Structs copy data; classes share references [OK]
Common Mistakes:
  • Confusing structs with classes as reference types
  • Thinking structs support inheritance
  • Using structs for shared mutable state
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

  1. Step 1: Recall struct declaration syntax

    In Swift, structs are declared with the keyword struct followed by the name and braces.
  2. 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.
  3. Final Answer:

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

  1. Step 1: Understand struct copying behavior

    Assigning p1 to p2 copies the data because Point is a struct (value type).
  2. Step 2: Analyze mutation effect

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

    1 -> Option A
  4. 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

  1. Step 1: Identify class reference behavior

    Classes are reference types, so c1 and c2 point to the same object.
  2. Step 2: Understand shared mutation

    Changing c2.count to 5 changes the shared instance, so c1.count also becomes 5.
  3. Final Answer:

    Counter is a class, so c1 and c2 share the same instance -> Option A
  4. 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

  1. Step 1: Analyze requirements for shared mutable state

    The session data must be shared and mutable across multiple parts, requiring a single shared instance.
  2. 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.
  3. 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.
  4. Final Answer:

    Use a class because it allows shared mutable state with identity. -> Option D
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard