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") }
- bad
- Nothing
- caught
- Compile error
Answer: C. caught
Explanation
The function throws, so execution jumps to the catch block.
Step-by-step solution
- f() throws.
- do-catch handles it.
- 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?
- do, try, catch
- try, except, finally
- begin, rescue, ensure
- 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
- Throwing calls are marked with try.
- They are handled in a do block with catch clauses.
- 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?
- Normally, like any function
- Only with
await - With
try, inside a do-catch or another throwing function - 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
- throws may fail.
- Use try.
- 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
- An optional that is nil if the call throws
- A Bool that says whether the call succeeded
- The error value that was thrown, as a plain value
- 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
- Try? runs the throwing call.
- If the call throws, try? converts the error into nil.
- 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?
- It returns nil
- It ignores the error silently
- It crashes the program
- 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
- try! asserts success.
- Failure traps.
- 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?
- Blocks until the closure is done
- Creates an OS thread directly
- Creates a new unstructured task that runs asynchronous work
- 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
- Sync code cannot await.
- Task provides async context.
- 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?
- Automatic multi-threading of every function
- A replacement for all closures in code
- A way to avoid any error handling
- 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
- Await marks a point where an async function may pause.
- While paused, the thread is free to do other work.
- 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())
- Optional(0)
- nil
- Crash
- bad
Answer: B. nil
Explanation
try? turns a thrown error into nil.
Step-by-step solution
- f throws.
- try? returns nil.
- 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?
- The value can be serialised to JSON
- The value is safe to pass between concurrency domains
- The value is thread-blocking
- 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
- Concurrency safety.
- Compiler checks it.
- 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?
- Use await, because access is serialised through the actor
- Directly, like any property
- Only from the main thread
- 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
- Actor isolates state.
- Outside code must await.
- 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 →