Structured concurrency means organizing asynchronous tasks so they start and finish in a clear, predictable way, like following a neat schedule. It helps avoid tasks running forever or getting lost.
Click to reveal answer
beginner
What Swift keyword starts a new asynchronous task within a structured concurrency context?
The async keyword marks functions that run asynchronously, and Task creates new concurrent tasks that are managed in a structured way.
Click to reveal answer
intermediate
Why is it important that child tasks complete before their parent task ends in structured concurrency?
This ensures all work is done in order and no tasks are left unfinished, just like finishing all your homework before going out to play.
Click to reveal answer
intermediate
What Swift feature helps wait for multiple child tasks to finish before continuing?
The await keyword pauses the current task until the child tasks complete, ensuring results are ready before moving on.
Click to reveal answer
beginner
How does structured concurrency improve app stability?
By keeping tasks organized and predictable, it prevents bugs from tasks running unexpectedly or crashing the app, making apps smoother and safer.
Click to reveal answer
What does structured concurrency help you manage in Swift?
AOnly synchronous code execution
BUI layout constraints
CAsynchronous tasks in a clear, organized way
DMemory allocation manually
✗ Incorrect
Structured concurrency organizes asynchronous tasks so they run predictably and finish properly.
Which keyword do you use to wait for an asynchronous task to finish in Swift?
Aawait
Bpause
Cwait
Dstop
✗ Incorrect
The await keyword pauses execution until the async task completes.
What happens if a child task does not finish before its parent task ends in structured concurrency?
AThe parent task waits for the child task
BThe child task runs forever
CThe child task is automatically cancelled
DThe parent task ignores the child task
✗ Incorrect
Structured concurrency ensures the parent waits for all child tasks to finish.
Which Swift type is commonly used to create a new concurrent task?
AOperation
BThread
CQueue
DTask
✗ Incorrect
The Task type creates new asynchronous tasks in Swift's structured concurrency.
3. What will be the output of this Swift code snippet using a task group?
func fetchValues() async -> [Int] {
await withTaskGroup(of: Int.self) { group in
group.addTask { 10 }
group.addTask { 20 }
var results = [Int]()
for await value in group {
results.append(value)
}
return results.sorted()
}
}
Task {
let values = await fetchValues()
print(values)
}
medium
A. Compilation error
B. [20, 10]
C. [10]
D. [10, 20]
Solution
Step 1: Understand task group execution
Two tasks return 10 and 20 concurrently; results are collected in 'results'.
Step 2: Analyze result sorting and output
Results are sorted before returning, so output is [10, 20].
Final Answer:
[10, 20] -> Option D
Quick Check:
Task group collects and sorts results [OK]
Hint: Task group results can arrive unordered; sorting fixes order [OK]
Common Mistakes:
Assuming order of results matches task add order
Forgetting to sort results
Expecting compilation error due to async
4. Identify the error in this Swift code using structured concurrency:
func loadData() async {
await withTaskGroup(of: String.self) { group in
group.addTask { "First" }
group.addTask { "Second" }
for value in group {
print(value)
}
}
}
medium
A. Incorrect type specified in withTaskGroup
B. group.addTask syntax is invalid
C. Missing 'await' before 'for' loop to iterate group
D. No error, code is correct
Solution
Step 1: Check iteration over task group
Iterating over a task group requires 'for await' to await each task's result.
Step 2: Identify missing 'await' keyword
The code uses 'for value in group' instead of 'for await value in group', causing error.
Final Answer:
Missing 'await' before 'for' loop to iterate group -> Option C
Quick Check:
Use 'for await' to iterate async task groups [OK]
Hint: Use 'for await' to loop over async task groups [OK]
Common Mistakes:
Using 'for' instead of 'for await' on task groups
Confusing task group type with task return type
Misusing addTask syntax
5. You want to fetch data from multiple URLs concurrently and combine their results safely. Which approach best uses structured concurrency in Swift?
hard
A. Use 'await withTaskGroup' to add a task for each URL fetch, then collect and combine results
B. Create multiple detached tasks without waiting, then combine results later
C. Fetch URLs one by one synchronously in a loop
D. Use DispatchQueue.global().async without task groups
Solution
Step 1: Understand structured concurrency for multiple async tasks
Using 'await withTaskGroup' allows running multiple async tasks concurrently and safely collecting results.
Step 2: Compare approaches for concurrency and safety
Use 'await withTaskGroup' to add a task for each URL fetch, then collect and combine results uses task groups correctly; others either run tasks unsafely or synchronously.
Final Answer:
Use 'await withTaskGroup' to add a task for each URL fetch, then collect and combine results -> Option A
Quick Check:
Task groups = safe concurrent task management [OK]
Hint: Use task groups to run and gather multiple async tasks safely [OK]
Common Mistakes:
Using detached tasks without waiting causes race conditions
Fetching synchronously loses concurrency benefits
Using DispatchQueue without structured concurrency loses safety
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.