Sample questions
Question 1Easy
What does the await keyword mark?
- A call that is guaranteed to run on the main thread
- A call that blocks the current thread until it ends
- A call whose errors are silently ignored
- A point where the current task may suspend
Answer: D. A point where the current task may suspend
Explanation
await marks a possible suspension point. While the task is suspended its thread is free to run other work.
Step-by-step solution
- Await marks a point where the current task can pause execution.
- When await is used, the thread is released to handle other tasks.
- The task resumes when the awaited operation completes.
- This allows asynchronous code to run without blocking threads.
- The correct answer is: A point where the current task may suspend.
Why the other options are wrong
- A. await does not pick the main thread; it marks a suspension point.
- B. await does not block the thread; the thread is released while the task waits.
- C. await does not hide errors; they still need try and handling.
Tip: Remember, await lets the thread do other work while waiting for an operation to finish.
Question 2Easy
What does marking a Swift function async mean?
- It returns immediately and its result is discarded
- It always runs on a new background thread
- It can suspend, and callers must use await
- It can only be called from the main thread
Answer: C. It can suspend, and callers must use await
Explanation
An async function may pause at suspension points without blocking a thread. Callers must be in an async context and mark the call with await.
Step-by-step solution
- Async functions can suspend execution to avoid blocking threads.
- Callers must use
await to wait for results when calling async functions. - Suspension points allow the function to pause and resume later.
- This means the caller must be in an async context to use await properly.
- The correct answer is: It can suspend, and callers must use await.
Why the other options are wrong
- A. async does not mean fire-and-forget; the result is still returned to an awaiting caller.
- B. async does not force a new thread; the runtime decides where the work runs.
- D. async functions are not limited to the main thread; they can run from other contexts.
Tip: Remember: await is required for async function calls in Swift.
Question 3Easy
Does this code compile?
func fetch() async -> Int { 1 }
func run() {
let x = await fetch()
print(x)
}
- No, because async functions cannot return values
- Yes, because
fetch() returns a plain Int - No: a sync function can't
await without a Task - Yes, but the call blocks the thread until
fetch() returns
Answer: C. No: a sync function can't await without a Task
Explanation
await is only allowed in an async context. run() is synchronous, so the call must move into an async function or a Task.
Step-by-step solution
- Fetch is async, so await can only be used in an async context.
- Run is a plain synchronous function, so it cannot use await directly.
- This is a compile error; the fix is to make run async or wrap the call in a Task.
- So option C is correct.
Why the other options are wrong
- A. Async functions can return values; fetch returns Int.
- B. The await in a synchronous function is the error, even though the return type is fine.
- D. Await does not block the thread, and the code still does not compile.
Tip: Await only works inside an async function or a Task.
Question 4Medium
How do async let bindings change this code's behaviour compared with two sequential awaits?
async let a = download(url1)
async let b = download(url2)
let (d1, d2) = await (a, b)
- Nothing changes; async let is only shorter syntax
- Both downloads start at once and run concurrently
- The second download waits for the first to finish
- Both downloads run on the main actor, one after the other
Answer: B. Both downloads start at once and run concurrently
Explanation
async let starts each child task immediately. The await on the tuple then waits for both, so total time is about the slower one, not the sum.
Step-by-step solution
- async let starts each task right away.
- Both downloads run concurrently.
- The final await waits for both.
Why the other options are wrong
- A. Sequential awaits would run one after the other.
- C. The second does not wait.
- D. They are child tasks and not forced onto the main actor.
Tip: async let = parallel.
Question 5Medium
How does task cancellation work in Swift concurrency?
- The task's thread is killed immediately wherever it is
- Cancellation only works for tasks on the main actor
- It's cooperative: the task must check for it
- Cancelled tasks are paused and resumed later
Answer: C. It's cooperative: the task must check for it
Explanation
Cancelling only sets a flag. The task's code must notice it (Task.isCancelled, try Task.checkCancellation(), or cancellation-aware APIs like Task.sleep) and stop.
Step-by-step solution
- Swift concurrency uses cancellation flags, not immediate thread killing.
- Tasks must check for cancellation using
Task.isCancelled or try Task.checkCancellation(). - Cancellation is cooperative, requiring explicit checks in the task's code.
- No automatic pausing or resuming of cancelled tasks occurs.
- The task must actively respond to cancellation signals to stop.
- Final answer: It's cooperative: the task must check for it.
Why the other options are wrong
- A. Swift never kills a task's thread; cancellation sets a flag that the task must check.
- B. Cancellation works on any task, not only main-actor ones.
- D. Cancelled tasks are not paused and resumed; they are told to stop.
Tip: Always check for cancellation in your async code using Task.checkCancellation().
Question 6Medium
What is the output when this runs as a Swift script (top-level code may use await)?
func v(_ n: Int) async -> Int { n }
async let a = v(1)
async let b = v(2)
print(await a + b)
- 12
- Nothing; async let needs a Task
- 3
- Optional(3)
Answer: C. 3
Explanation
Both child tasks run concurrently and await a + b waits for both values, giving 1 + 2 = 3.
Step-by-step solution
- Both child tasks start together.
- await a + b waits for both.
- 1 + 2 = 3.
Why the other options are wrong
- A. + adds numbers, not text.
- B. async let works without an explicit Task here.
- D. The result is an Int, not Optional.
Tip: await the results.
Question 7Medium
When should you use withTaskGroup rather than async let?
- When the number of child tasks is only known at runtime
- When the code runs outside any async context
- When there is exactly one child task to run
- When the tasks must run one after another in order
Answer: A. When the number of child tasks is only known at runtime
Explanation
async let suits a fixed number of concurrent operations. A task group lets you add a dynamic number of child tasks, such as one per URL in an array, and collect their results.
Step-by-step solution
- Async let is for known number of tasks upfront.
- WithTaskGroup handles tasks added at runtime.
- Dynamic task count needs withTaskGroup.
- Runtime determined tasks use withTaskGroup.
- Correct answer is A (When the number of child tasks is only known at runtime).
Why the other options are wrong
- B. withTaskGroup needs an async context, so this is not the reason to choose it.
- C. A fixed child task suits async let; withTaskGroup is for dynamic counts.
- D. Ordering is not the reason; task groups run child tasks concurrently.
Tip: Use withTaskGroup for dynamic task counts, async let for known ones.
Question 8Medium
You are inside a synchronous button action and need to call an async function. What is the usual way?
- Call
load() directly; Swift adds await for you - Use
try? load() to make it synchronous - Wrap the call in
Task { await load() } - Mark the button action with
await
Answer: C. Wrap the call in Task { await load() }
Explanation
A synchronous function cannot await. Task { } creates a new task that can run async code, inheriting the current actor context.
Step-by-step solution
- Synchronous functions cannot use
await directly. - Use
Task { await load() } to run async code in a new task. Task { } inherits the current actor context correctly.- This ensures async function runs properly inside synchronous action.
- Final answer: Wrap the call in
Task { await load() }.
Why the other options are wrong
- A. Swift never adds await for you; a synchronous function must start a Task to call it.
- B. try? does not make a call synchronous; it only turns errors into nil, and load still needs await.
- D. A button action cannot be marked await; it needs a Task to start the work.
Tip: Use Task { await ... } for async calls inside synchronous contexts.
Question 9Hard
Why is try await Task.sleep(for: .seconds(1)) preferred over Thread.sleep(forTimeInterval: 1) inside async code?
- Task.sleep runs the rest of the code on the main thread
- Thread.sleep is not allowed to be called on iOS
- Thread.sleep makes the task throw a cancellation error
- Task.sleep suspends the task without blocking the thread
Answer: D. Task.sleep suspends the task without blocking the thread
Explanation
Thread.sleep blocks one of the cooperative thread pool's few threads. Task.sleep suspends only the task, so the thread can run other work, and it also responds to cancellation.
Step-by-step solution
- Thread.sleep blocks the thread it runs on, so other work on that thread has to wait.
- Task.sleep(for:) suspends the task instead, which frees the thread for other work.
- Marked answer: option D, it suspends without blocking the thread.
Why the other options are wrong
- A. Task.sleep does not change which thread runs the rest of the code.
- B. Thread.sleep is allowed on iOS; the real problem is that it blocks the thread.
- C. Thread.sleep never throws a cancellation error; Task.sleep throws only when the task is cancelled.
Tip: In async code, use try await Task.sleep; Thread.sleep blocks the thread.
Question 10Hard
Which is a property of structured concurrency (async let and task groups)?
- Child tasks keep running after the parent function returns
- The parent can't finish until its child tasks finish
- Child tasks always run on the main thread
- Cancelling a parent has no effect on its children
Answer: B. The parent can't finish until its child tasks finish
Explanation
Child tasks are scoped to the parent: the scope waits for them, and cancelling the parent cancels them too. Task { } creates unstructured tasks that lack these guarantees.
Step-by-step solution
- Async let and task groups are structured concurrency.
- The parent scope waits for all of its child tasks before it can finish.
- So a parent cannot finish while its children are still running, which is option B.
Why the other options are wrong
- A. Child tasks finish before their parent scope ends.
- C. Child tasks do not run only on the main thread.
- D. Cancelling a parent cancels its child tasks.
Tip: In structured concurrency, child tasks end before their parent scope ends.
Practise all 13 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →