0
0
Swiftprogramming~20 mins

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

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