Error Handling & Concurrency Questions with Answers

16 practice questions on Error Handling & Concurrency (Swift) with answers and step-by-step solutions. Try 10 samples below, then practise all 16 free in the AptiNest app.

Sample questions

Question 1Easy

What is the output?

enum E: Error { case bad }
func f() throws { throw E.bad }
do { try f() } catch { print("caught") }
  1. bad
  2. Nothing
  3. caught
  4. Compile error

Answer: C. caught

Explanation

The function throws, so execution jumps to the catch block.

Step-by-step solution

  1. f() throws.
  2. do-catch handles it.
  3. Prints caught.

Why the other options are wrong

  • A. The catch block prints a fixed word.
  • B. The error is thrown and caught.
  • D. Calls to throwing functions need try.

Question 2Easy

Which keywords are used to handle a thrown error in Swift?

  1. do, try, catch
  2. try, except, finally
  3. begin, rescue, ensure
  4. throw, handle, end

Answer: A. do, try, catch

Explanation

Calls that can throw are marked with try inside a do block with catch clauses.

Step-by-step solution

  1. Throwing calls are marked with try.
  2. They are handled in a do block with catch clauses.
  3. So do, try and catch are the keywords (option A).

Why the other options are wrong

  • B. Except and finally are Python-style keywords, not Swift.
  • C. Begin, rescue and ensure are Ruby-style keywords.
  • D. Throw raises an error, but handle and end are not Swift keywords for handling.

Tip: Try must be written before any call that can throw.

Question 3Easy

How must you call a function marked throws?

  1. Normally, like any function
  2. Only with await
  3. With try, inside a do-catch or another throwing function
  4. Only on the main thread

Answer: C. With try, inside a do-catch or another throwing function

Explanation

The compiler forces callers to acknowledge errors using try, try? or try!.

Step-by-step solution

  1. throws may fail.
  2. Use try.
  3. Handle with do-catch or rethrow.

Why the other options are wrong

  • A. try is required.
  • B. await is for async.
  • D. Threading is unrelated.

Question 4Medium

What does try? return?

  1. An optional that is nil if the call throws
  2. A Bool that says whether the call succeeded
  3. The error value that was thrown, as a plain value
  4. It always crashes the program when the call throws

Answer: A. An optional that is nil if the call throws

Explanation

try? converts a thrown error into nil.

Step-by-step solution

  1. Try? runs the throwing call.
  2. If the call throws, try? converts the error into nil.
  3. So the result is an optional that is nil on error (option A).

Why the other options are wrong

  • B. Try? returns an optional value, not a Bool.
  • C. The error is discarded; the result is nil.
  • D. Try? does not crash when the call throws.

Tip: Use try? when you only care whether a call worked, not why it failed.

Question 5Medium

What does try! do if the function throws?

  1. It returns nil
  2. It ignores the error silently
  3. It crashes the program
  4. It rethrows the error

Answer: C. It crashes the program

Explanation

try! force-unwraps the result and traps at runtime on an error.

Step-by-step solution

  1. try! asserts success.
  2. Failure traps.
  3. Avoid in production.

Why the other options are wrong

  • A. That is try?.
  • B. It traps.
  • D. It doesn't propagate.

Question 6Medium

What does Task { ... } do?

  1. Blocks until the closure is done
  2. Creates an OS thread directly
  3. Creates a new unstructured task that runs asynchronous work
  4. Marks the closure as @escaping

Answer: C. Creates a new unstructured task that runs asynchronous work

Explanation

Task starts asynchronous work from synchronous code and inherits actor context and priority.

Step-by-step solution

  1. Sync code cannot await.
  2. Task provides async context.
  3. Runs concurrently.

Why the other options are wrong

  • A. It returns immediately.
  • B. Tasks are scheduled by the runtime.
  • D. That is unrelated.

Question 7Medium

What does async/await provide in Swift?

  1. Automatic multi-threading of every function
  2. A replacement for all closures in code
  3. A way to avoid any error handling
  4. Async code that suspends without blocking

Answer: D. Async code that suspends without blocking

Explanation

await marks a suspension point; the function resumes later.

Step-by-step solution

  1. Await marks a point where an async function may pause.
  2. While paused, the thread is free to do other work.
  3. So async/await suspends without blocking (option D).

Why the other options are wrong

  • A. Functions are not all run on many threads automatically.
  • B. Closures still exist; async/await is one option for asynchronous code.
  • C. Async functions can still throw, so error handling remains needed.

Tip: Await is a suspension point, not a thread creator.

Question 8Medium

What is the output?

enum E: Error { case bad }
func f() throws -> Int { throw E.bad }
print(try? f())
  1. Optional(0)
  2. nil
  3. Crash
  4. bad

Answer: B. nil

Explanation

try? turns a thrown error into nil.

Step-by-step solution

  1. f throws.
  2. try? returns nil.
  3. nil.

Why the other options are wrong

  • A. try? converts the error into nil.
  • C. try? never crashes.
  • D. The error is swallowed.

Question 9Hard

What does the Sendable protocol indicate?

  1. The value can be serialised to JSON
  2. The value is safe to pass between concurrency domains
  3. The value is thread-blocking
  4. The value is immutable only

Answer: B. The value is safe to pass between concurrency domains

Explanation

Sendable marks types that can cross actor or task boundaries without data races.

Step-by-step solution

  1. Concurrency safety.
  2. Compiler checks it.
  3. Value types usually conform.

Why the other options are wrong

  • A. That's Codable.
  • C. No.
  • D. Value types, final classes and actors may qualify.

Question 10Hard

How do you read an actor's property from outside the actor?

  1. Use await, because access is serialised through the actor
  2. Directly, like any property
  3. Only from the main thread
  4. It is impossible

Answer: A. Use await, because access is serialised through the actor

Explanation

Actors protect their mutable state by letting one task access it at a time, so outside access is asynchronous.

Step-by-step solution

  1. Actor isolates state.
  2. Outside code must await.
  3. Prevents data races.

Why the other options are wrong

  • B. Actor isolation requires await.
  • C. Any async context can.
  • D. Cross-actor access is allowed with await.

Practise all 16 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 →

Related topics

See all Swift topics · All topics