Bird
Raised Fist0
Swiftprogramming~20 mins

Mutating methods for value types in Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Swift Mutating Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Swift code with a mutating method?

Consider this Swift struct with a mutating method that changes a property. What will be printed?

Swift
struct Counter {
    var count = 0
    mutating func increment() {
        count += 1
    }
}

var myCounter = Counter()
myCounter.increment()
print(myCounter.count)
A1
B0
CCompilation error: mutating method called on a constant
DRuntime error
Attempts:
2 left
💡 Hint

Remember that mutating methods can change properties only if the instance is a variable.

❓ Predict Output
intermediate
2:00remaining
What error occurs when calling a mutating method on a constant struct?

What happens if you call a mutating method on a constant instance of a struct?

Swift
struct Point {
    var x = 0
    var y = 0
    mutating func moveBy(dx: Int, dy: Int) {
        x += dx
        y += dy
    }
}

let fixedPoint = Point()
fixedPoint.moveBy(dx: 5, dy: 5)
ANo error, point moves to (5,5)
BCompilation error: Cannot use mutating method on a let constant
CRuntime error: Cannot modify constant
DCompilation error: Missing mutating keyword
Attempts:
2 left
💡 Hint

Think about whether a constant struct instance can be changed.

🔧 Debug
advanced
2:00remaining
Why does this mutating method cause a compile error?

Identify the reason for the compile error in this Swift code.

Swift
struct Rectangle {
    var width: Int
    var height: Int

    func doubleSize() {
        width *= 2
        height *= 2
    }
}

var rect = Rectangle(width: 3, height: 4)
rect.doubleSize()
ANo error, code runs and doubles size
BError because width and height are constants
CError because doubleSize is not marked mutating but modifies properties
DError because Rectangle is a class, not a struct
Attempts:
2 left
💡 Hint

Check if the method that changes properties is marked correctly.

📝 Syntax
advanced
2:00remaining
Which option correctly declares a mutating method in Swift?

Choose the correct syntax for a mutating method that increments a value.

Amutate func increment() { value += 1 }
Bfunc mutating increment() { value += 1 }
Cfunc increment() mutating { value += 1 }
Dmutating func increment() { value += 1 }
Attempts:
2 left
💡 Hint

The mutating keyword goes before func.

🚀 Application
expert
2:00remaining
What is the final value of 'score' after calling the mutating method twice?

Given this Swift struct and code, what is the value of game.score after calling increaseScore() twice?

Swift
struct Game {
    var score = 0
    mutating func increaseScore() {
        score += 10
    }
}

var game = Game()
game.increaseScore()
game.increaseScore()
A20
B10
C0
DCompilation error
Attempts:
2 left
💡 Hint

Each call adds 10 to score. How many calls are made?

Practice

(1/5)
1. What is the purpose of the mutating keyword in Swift when used with methods inside a struct?
easy
A. It prevents the method from changing any properties.
B. It allows the method to modify properties of the struct.
C. It makes the method static and callable without an instance.
D. It marks the method as private and inaccessible outside the struct.

Solution

  1. Step 1: Understand the role of mutating in structs

    In Swift, structs are value types and their properties cannot be changed inside instance methods unless the method is marked mutating.
  2. Step 2: Effect of mutating keyword

    The mutating keyword allows the method to modify the struct's properties safely.
  3. Final Answer:

    It allows the method to modify properties of the struct. -> Option B
  4. Quick Check:

    mutating lets methods change struct data [OK]
Hint: Mutating means method can change struct's own properties [OK]
Common Mistakes:
  • Thinking mutating makes method static
  • Assuming mutating prevents changes
  • Confusing mutating with access control
2. Which of the following is the correct syntax to declare a mutating method named updateValue inside a struct in Swift?
easy
A. mutating func updateValue() { }
B. func mutating updateValue() { }
C. func updateValue() mutating { }
D. mutate func updateValue() { }

Solution

  1. Step 1: Recall correct order of keywords in method declaration

    In Swift, the mutating keyword comes before func when declaring a mutating method.
  2. Step 2: Match the syntax

    mutating func updateValue() { } correctly shows mutating func updateValue() { }, which is the proper syntax.
  3. Final Answer:

    mutating func updateValue() { } -> Option A
  4. Quick Check:

    Keyword order: mutating before func [OK]
Hint: Put 'mutating' before 'func' in method declaration [OK]
Common Mistakes:
  • Placing mutating after func
  • Using 'mutate' instead of 'mutating'
  • Adding mutating inside method body
3. Consider this Swift struct and code:
struct Counter {
    var count = 0
    mutating func increment() {
        count += 1
    }
}

var myCounter = Counter()
myCounter.increment()
print(myCounter.count)

What will be printed?
medium
A. 0
B. Runtime error
C. Compilation error
D. 1

Solution

  1. Step 1: Understand the mutating method effect

    The increment() method is marked mutating, so it can increase the count property by 1.
  2. 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.
  3. Final Answer:

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

  1. Step 1: Identify instance mutability

    The instance p is declared with let, making it a constant.
  2. Step 2: Understand mutating method call on constant

    Mutating methods cannot be called on constant instances because they change properties, which is not allowed.
  3. Final Answer:

    Cannot call mutating method on a constant instance. -> Option C
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    struct BankAccount { var balance: Double = 0 mutating func deposit(amount: Double) { if amount > 0 { balance += amount } } } -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard