Bird
Raised Fist0
Swiftprogramming~20 mins

Why classes exist alongside structs in Swift - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Swift Classes and Structs Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use classes instead of structs in Swift?

In Swift, both classes and structs can store data and have methods. But classes have a feature that structs do not. What is the main reason to choose a class over a struct?

AStructs can only store numbers, while classes can store any type of data.
BClasses support inheritance, allowing one class to inherit properties and methods from another.
CStructs cannot have methods, but classes can.
DClasses are always faster than structs because they use less memory.
Attempts:
2 left
💡 Hint

Think about how you can create a new type based on an existing one.

❓ Predict Output
intermediate
2:00remaining
What is the output of this Swift code using class and struct?

Consider this Swift code:

struct PointStruct {
    var x: Int
    var y: Int
}

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

var a = PointStruct(x: 1, y: 1)
var b = a
b.x = 2

var c = PointClass(x: 1, y: 1)
var d = c
d.x = 2

print(a.x)
print(c.x)

What will be printed?

A
2
1
B
2
2
C
1
1
D
1
2
Attempts:
2 left
💡 Hint

Think about how structs and classes are copied or referenced.

🔧 Debug
advanced
2:00remaining
Why does this Swift code cause a compile error?

Look at this Swift code:

struct Person {
    var name: String
    mutating func changeName(newName: String) {
        name = newName
    }
}

Why does this code cause a compile error?

ABecause 'changeName' tries to modify a property in a struct method that is not marked as 'mutating'.
BBecause structs cannot have functions that modify properties.
CBecause 'name' is a constant and cannot be changed.
DBecause 'newName' parameter is not optional.
Attempts:
2 left
💡 Hint

Think about how structs handle methods that change their own data.

📝 Syntax
advanced
2:00remaining
Which Swift code correctly defines a class with a deinitializer?

Choose the correct Swift code that defines a class with a deinitializer that prints "Goodbye" when the object is destroyed.

A
class MyClass {
    destructor {
        print("Goodbye")
    }
}
B
class MyClass {
    func deinit() {
        print("Goodbye")
    }
}
C
class MyClass {
    deinit {
        print("Goodbye")
    }
}
D
class MyClass {
    deinitializer() {
        print("Goodbye")
    }
}
Attempts:
2 left
💡 Hint

Look for the special keyword Swift uses for cleanup code in classes.

🚀 Application
expert
3:00remaining
How many instances are created and shared in this Swift code?

Consider this Swift code:

class Counter {
    var count = 0
}

var c1 = Counter()
var c2 = c1
c2.count += 1

struct Wrapper {
    var counter: Counter
}

var w1 = Wrapper(counter: Counter())
var w2 = w1
w2.counter.count += 1

print(c1.count)
print(w1.counter.count)

What will be printed?

A
1
1
B
1
0
C
0
1
D
0
0
Attempts:
2 left
💡 Hint

Remember how classes and structs behave when assigned and copied.

Practice

(1/5)
1. Why do classes exist alongside structs in Swift?
easy
A. Because classes cannot store data, only methods
B. Because structs are slower than classes in all cases
C. Because classes support shared references and inheritance, while structs do not
D. Because structs allow inheritance but classes do not

Solution

  1. Step 1: Understand the difference between classes and structs

    Structs are value types, meaning each copy is independent. Classes are reference types, meaning multiple variables can share the same instance.
  2. Step 2: Identify why classes are needed

    Classes allow shared data through references and support inheritance, which structs do not.
  3. Final Answer:

    Because classes support shared references and inheritance, while structs do not -> Option C
  4. Quick Check:

    Classes = shared references + inheritance [OK]
Hint: Classes share data and support inheritance; structs do not [OK]
Common Mistakes:
  • Thinking structs support inheritance
  • Believing classes cannot store data
  • Assuming structs are always slower
2. Which of the following is the correct way to declare a class in Swift?
easy
A. struct MyClass { var name: String }
B. class MyClass { var name: String }
C. func MyClass() { var name: String }
D. let MyClass = class { var name: String }

Solution

  1. Step 1: Recall Swift syntax for class declaration

    Classes in Swift are declared using the keyword class followed by the class name and braces.
  2. Step 2: Check each option

    class MyClass { var name: String } correctly uses class MyClass { var name: String }. struct MyClass { var name: String } uses struct, not class. func MyClass() { var name: String } uses func, which is incorrect. let MyClass = class { var name: String } uses invalid syntax.
  3. Final Answer:

    class MyClass { var name: String } -> Option B
  4. Quick Check:

    Class declaration uses 'class' keyword [OK]
Hint: Use 'class' keyword to declare a class in Swift [OK]
Common Mistakes:
  • Using 'struct' instead of 'class'
  • Trying to declare class inside a function
  • Incorrect syntax with 'let' or 'func'
3. What will be the output of this Swift code?
class Person {
  var name: String
  init(name: String) { self.name = name }
}

var p1 = Person(name: "Alice")
var p2 = p1
p2.name = "Bob"
print(p1.name)
medium
A. Bob
B. Alice
C. Compilation error
D. nil

Solution

  1. Step 1: Understand class reference behavior

    Classes are reference types, so p1 and p2 point to the same object.
  2. Step 2: Analyze the code changes

    Changing p2.name to "Bob" changes the shared object. Printing p1.name shows "Bob".
  3. Final Answer:

    Bob -> Option A
  4. Quick Check:

    Class variables share references, so name = Bob [OK]
Hint: Classes share instances; changing one changes all references [OK]
Common Mistakes:
  • Thinking p1 and p2 are independent copies
  • Expecting 'Alice' as output
  • Confusing class with struct behavior
4. Identify the error in this Swift code that tries to use inheritance with a struct:
struct Vehicle {
  var speed: Int
}

struct Car: Vehicle {
  var brand: String
}
medium
A. Cannot declare variables inside structs
B. Missing initializer in Car struct
C. Syntax error: colon should be replaced with comma
D. Structs cannot inherit from other structs or classes

Solution

  1. Step 1: Understand inheritance rules in Swift

    In Swift, only classes support inheritance. Structs cannot inherit from other structs or classes.
  2. Step 2: Analyze the code

    The code tries to make Car inherit from Vehicle, both structs, which is invalid.
  3. Final Answer:

    Structs cannot inherit from other structs or classes -> Option D
  4. Quick Check:

    Inheritance = classes only, structs no [OK]
Hint: Only classes support inheritance, not structs [OK]
Common Mistakes:
  • Thinking structs can inherit
  • Confusing initializer errors with inheritance
  • Misreading colon syntax as error
5. You want to create a data model where multiple parts of your app share and modify the same user profile data. Which Swift type should you choose and why?
hard
A. Use a class because it allows shared references and identity
B. Use a class because structs cannot store properties
C. Use a struct because it supports inheritance for flexibility
D. Use a struct because it copies data safely between parts

Solution

  1. Step 1: Identify the need for shared data

    The requirement is that multiple parts share and modify the same user profile data, meaning shared references are needed.
  2. Step 2: Choose the correct Swift type

    Classes are reference types that allow shared references and identity, so changes in one place reflect everywhere. Structs copy data, so changes do not propagate.
  3. Final Answer:

    Use a class because it allows shared references and identity -> Option A
  4. Quick Check:

    Shared data needs class reference type [OK]
Hint: Shared mutable data needs classes, not structs [OK]
Common Mistakes:
  • Choosing structs for shared mutable data
  • Thinking structs support inheritance
  • Believing classes cannot store properties
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