The increment() method is marked mutating, so it can increase the count property by 1.
Step 2: Trace the code execution
We create myCounter as a variable instance, call increment(), so count changes from 0 to 1, then print outputs 1.
Final Answer:
1 -> Option D
Quick Check:
Mutating method changes count from 0 to 1 [OK]
Hint: Mutating methods change struct vars if instance is variable [OK]
Common Mistakes:
Expecting 0 because method looks like a function
Thinking mutating methods cause errors always
Using constant instance instead of variable
4. Given this Swift code:
struct Point {
var x = 0
var y = 0
mutating func moveTo(newX: Int, newY: Int) {
x = newX
y = newY
}
}
let p = Point()
p.moveTo(newX: 10, newY: 20)
What is the problem here?
medium
A. The method moveTo is missing the mutating keyword.
B. Structs cannot have methods that change properties.
C. Cannot call mutating method on a constant instance.
D. The properties x and y must be declared as constants.
Solution
Step 1: Identify instance mutability
The instance p is declared with let, making it a constant.
Step 2: Understand mutating method call on constant
Mutating methods cannot be called on constant instances because they change properties, which is not allowed.
Final Answer:
Cannot call mutating method on a constant instance. -> Option C
Quick Check:
Mutating methods need variable instances [OK]
Hint: Mutating methods require variable (var) instances, not constants [OK]
Common Mistakes:
Thinking mutating keyword is missing
Believing structs can't have mutating methods
Assuming properties must be constants
5. You want to create a Swift struct BankAccount with a balance property. You need a mutating method deposit(amount:) that adds money to the balance only if the amount is positive. Which implementation correctly uses a mutating method to safely update the balance?
hard
A. struct BankAccount {
var balance: Double = 0
mutating func deposit(amount: Double) {
if amount > 0 {
balance += amount
}
}
}
B. struct BankAccount {
var balance: Double = 0
func deposit(amount: Double) {
if amount > 0 {
balance += amount
}
}
}
C. struct BankAccount {
let balance: Double = 0
mutating func deposit(amount: Double) {
balance += amount
}
}
D. struct BankAccount {
var balance: Double = 0
mutating func deposit(amount: Double) {
balance = amount
}
}
Solution
Step 1: Check mutating method and property mutability
The method must be marked mutating to change balance, which must be a variable (var), not constant.
Step 2: Verify correct logic for deposit
struct BankAccount {
var balance: Double = 0
mutating func deposit(amount: Double) {
if amount > 0 {
balance += amount
}
}
} adds amount only if positive, correctly updating balance. struct BankAccount {
var balance: Double = 0
func deposit(amount: Double) {
if amount > 0 {
balance += amount
}
}
} lacks mutating, so it cannot change balance. struct BankAccount {
let balance: Double = 0
mutating func deposit(amount: Double) {
balance += amount
}
} uses let for balance, so it cannot be changed. struct BankAccount {
var balance: Double = 0
mutating func deposit(amount: Double) {
balance = amount
}
} replaces balance with amount without checking positivity.
Final Answer:
struct BankAccount {
var balance: Double = 0
mutating func deposit(amount: Double) {
if amount > 0 {
balance += amount
}
}
} -> Option A
Quick Check:
Mutating method + var property + condition = correct [OK]
Hint: Mutating method must change var property with proper checks [OK]
Common Mistakes:
Forgetting mutating keyword
Using let instead of var for properties
Not checking if amount is positive
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.