Bird
0
0

Consider this code snippet:

hard📝 Application Q9 of 15
iOS Swift - Concurrency
Consider this code snippet:
await withTaskGroup(of: Int.self) { group in
  for i in 1...3 {
    group.addTask { [i] in i * 2 }
  }
  var results = [Int]()
  for await value in group {
    results.append(value)
  }
  print(results.sorted())
What does this print?
A[1, 2, 3]
B[6, 4, 2]
C[2, 4, 6]
DCompilation error
Step-by-Step Solution
Solution:
  1. Step 1: Analyze task group tasks

    Tasks multiply i by 2 for i in 1 to 3, producing 2, 4, 6.
  2. Step 2: Understand result collection and sorting

    Results collected in any order, then sorted ascending before printing.
  3. Final Answer:

    [2, 4, 6] -> Option C
  4. Quick Check:

    Sorted doubled values = [2,4,6] [OK]
Quick Trick: Task group results may be unordered; sort if needed [OK]
Common Mistakes:
  • Assuming results are in order added
  • Confusing original i values with doubled
  • Expecting compilation errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes