0
0
Swiftprogramming~20 mins

Why classes exist alongside structs in Swift - Challenge Your Understanding

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