0
0
Swiftprogramming~20 mins

Actor isolation concept in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Actor 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 actor code?

Consider this Swift code using an actor. What will be printed when printCounter() is called?

Swift
actor Counter {
    var value = 0
    func increment() {
        value += 1
    }
    func printCounter() {
        print("Counter value: \(value)")
    }
}

let counter = Counter()
Task {
    await counter.increment()
    await counter.printCounter()
}
ARuntime error due to actor isolation violation
BCounter value: 1
CCompilation error due to missing await
DCounter value: 0
Attempts:
2 left
💡 Hint

Remember that actor methods must be called with await outside the actor.

🧠 Conceptual
intermediate
1:30remaining
Which statement best describes actor isolation in Swift?

Choose the statement that correctly explains actor isolation.

AActors isolate their mutable state to ensure only one task accesses it at a time.
BActors allow shared mutable state without any synchronization.
CActors require all methods to be synchronous to avoid data races.
DActors automatically copy their state when accessed from multiple tasks.
Attempts:
2 left
💡 Hint

Think about how actors protect their data from concurrent access.

🔧 Debug
advanced
2:00remaining
What error does this Swift code produce?

Examine this code snippet. What error will the compiler report?

Swift
actor BankAccount {
    var balance = 1000
    func deposit(amount: Int) {
        balance += amount
    }
}

let account = BankAccount()
account.deposit(amount: 500)
AError: Call to actor-isolated method 'deposit' in a synchronous context requires 'await'
BError: Cannot mutate 'balance' because it is a constant
CNo error, code runs successfully
DError: Missing 'async' keyword in function declaration
Attempts:
2 left
💡 Hint

Think about how actor methods must be called from outside the actor.

📝 Syntax
advanced
1:30remaining
Which option correctly declares an actor with an isolated mutable property?

Choose the correct Swift code that declares an actor with a mutable property count initialized to 0.

A
actor Counter {
    let count = 0
}
B
actor Counter {
    var count: Int
    init() { count = 0 }
}
C
actor Counter {
    static var count = 0
}
D
actor Counter {
    var count: Int = 0
}
Attempts:
2 left
💡 Hint

Remember that mutable properties inside actors are declared with var.

🚀 Application
expert
2:30remaining
How many times will the actor's method be executed concurrently?

Given this Swift code, how many concurrent executions of increment() can happen inside the actor?

Swift
actor Counter {
    var value = 0
    func increment() {
        value += 1
    }
}

let counter = Counter()
Task {
    await withTaskGroup(of: Void.self) { group in
        for _ in 1...5 {
            group.addTask {
                await counter.increment()
            }
        }
    }
}
ANo executions happen because increment is not async
B5 concurrent executions happen inside the actor
COnly 1 execution happens at a time due to actor isolation
DExecutions run concurrently but cause a data race
Attempts:
2 left
💡 Hint

Think about how actor isolation serializes access to its mutable state.