0
0
Swiftprogramming~20 mins

Actor declaration syntax in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Actor Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of actor method call
What is the output of this Swift code using an actor?
Swift
actor Counter {
    var value = 0
    func increment() async -> Int {
        value += 1
        return value
    }
}

let counter = Counter()
Task {
    let result = await counter.increment()
    print(result)
}
A0
BRuntime error
CCompilation error
D1
Attempts:
2 left
💡 Hint
Remember that actor methods are asynchronous and state changes inside actors are safe.
📝 Syntax
intermediate
2:00remaining
Identify the correct actor declaration
Which of the following is the correct syntax to declare an actor named Logger in Swift?
A
actor Logger {
    func log(_ message: String) {
        print(message)
    }
}
B
class Logger {
    actor func log(_ message: String) {
        print(message)
    }
}
C
actor Logger() {
    func log(_ message: String) {
        print(message)
    }
}
D
actor Logger {
    actor func log(_ message: String) {
        print(message)
    }
}
Attempts:
2 left
💡 Hint
Actors are declared like classes but with the keyword 'actor' and no parentheses.
🔧 Debug
advanced
2:00remaining
Why does this actor code cause a compile error?
Consider this Swift code: actor BankAccount { var balance: Int = 0 func deposit(amount: Int) { balance += amount } } let account = BankAccount() account.deposit(amount: 100) Why does this code cause a compile error?
Swift
actor BankAccount {
    var balance: Int = 0
    func deposit(amount: Int) async {
        balance += amount
    }
}

let account = BankAccount()
Task {
    await account.deposit(amount: 100)
}
ABecause actors cannot have methods that modify state
BBecause 'balance' cannot be a variable inside an actor
CBecause actor methods must be called with 'await' since they are asynchronous
DBecause 'deposit' must be marked as 'async' explicitly
Attempts:
2 left
💡 Hint
Think about how actor methods are called from outside the actor.
🧠 Conceptual
advanced
2:00remaining
Actor isolation and data race prevention
Which statement best describes how actors prevent data races in Swift?
AActors use locks internally to block threads when accessing variables
BActors serialize access to their mutable state by allowing only one task at a time to access it
CActors copy their state to each thread to avoid sharing data
DActors require all variables to be immutable to prevent data races
Attempts:
2 left
💡 Hint
Think about how actors handle concurrent access to their properties.
Predict Output
expert
2:00remaining
Output of concurrent actor calls
What is the output of this Swift code using an actor with concurrent tasks?
Swift
actor Counter {
    var value = 0
    func increment() async -> Int {
        value += 1
        return value
    }
}

let counter = Counter()

Task {
    async let a = counter.increment()
    async let b = counter.increment()
    let results = await [a, b]
    print(results.sorted())
}
A[1, 2]
B[2, 2]
C[1, 1]
DRuntime error due to data race
Attempts:
2 left
💡 Hint
Actors serialize access, so increments happen one after another.