Sobes.tech
Junior — Senior

What will be the output of the program? No. 16

livecode

Task condition

In a Swift program, a global queue and an array of employees are created. Then, two asynchronous blocks are added to the queue: the first one sequentially prints the names from the array with a 1-second pause, and the second one, after a 0.5-second delay, prints a message and removes the first element from the array. Determine which lines will appear in the console.

let queue = DispatchQueue.global()
var employees = ["Bill", "Bob", "Joe"]

queue.async {
    let count = employees.count
    for index in 0..<count {
        print(employees[index])
        Thread.sleep(forTimeInterval: 1)
    }
}

queue.async {
    Thread.sleep(forTimeInterval: 0.5)
    print("remove")
    employees.remove(at: 0)
}