Swift interview questions

25 questions from the Swift course, each with the correct answer and an explanation. Try to answer before you open the answer.

Getting Started with Swift

1. Which feature is central to Swift's safety against null-pointer crashes?

  1. Optionals that make the absence of a value explicit
  2. Garbage collection
  3. Manual memory management
  4. Dynamic typing
Show answer

Answer: Optionals that make the absence of a value explicit

Optionals force code to handle missing values explicitly.

Lesson: What Swift Is and Where It Runs

2. Which command runs the tests of a Swift package?

  1. swift run
  2. swift package init
  3. xcodebuild install
  4. swift test
Show answer

Answer: swift test

swift test builds and runs the package's test targets.

Lesson: Xcode, Playgrounds and the Swift Package Manager

3. What happens when you add an Int and a Double directly in Swift?

  1. The Int is converted automatically
  2. The Double is truncated
  3. It is a compile error; you must convert one explicitly
  4. The result is a String
Show answer

Answer: It is a compile error; you must convert one explicitly

Swift has no implicit numeric conversions, so you write Double(x) or Int(y).

Lesson: Constants, Variables, Types and Strings

Optionals, Control Flow and Collections

4. What does `guard let user else { return }` do when user is nil?

  1. Crashes the program
  2. Assigns a default user
  3. Exits the current scope early via the else branch
  4. Continues with user as nil
Show answer

Answer: Exits the current scope early via the else branch

guard requires the else branch to exit; after it, user is unwrapped for the rest of the scope.

Lesson: Optionals

5. Which statement about Swift switch is true?

  1. Cases fall through by default
  2. switch must be exhaustive and cases do not fall through
  3. switch only works with integers
  4. Every case needs break
Show answer

Answer: switch must be exhaustive and cases do not fall through

Exhaustiveness and no implicit fall-through make switch safer than in C.

Lesson: Control Flow and Pattern Matching

6. What does copy-on-write mean for Swift arrays?

  1. Copies share storage until one of them is modified, then the data is actually copied
  2. Arrays cannot be copied
  3. Every assignment copies all elements immediately
  4. Arrays are reference types
Show answer

Answer: Copies share storage until one of them is modified, then the data is actually copied

Copy-on-write gives value semantics with the efficiency of shared storage.

Lesson: Arrays, Dictionaries, Sets and Functional Operations

Structs, Enums and Classes

7. Why must some struct methods be marked mutating?

  1. Because they modify the struct's own properties, which value types only allow explicitly
  2. To make them public
  3. To make them asynchronous
  4. Because structs cannot have methods
Show answer

Answer: Because they modify the struct's own properties, which value types only allow explicitly

mutating marks methods that change self, and they require a var instance.

Lesson: Structs and Value Semantics

8. What advantage do associated values give enums?

  1. They make enums faster
  2. They allow inheritance
  3. They remove the need for switch
  4. Each case can carry its own relevant data, making invalid states unrepresentable
Show answer

Answer: Each case can carry its own relevant data, making invalid states unrepresentable

Associated values keep exactly the data each case needs.

Lesson: Enums with Associated Values

9. What does the === operator check for class instances?

  1. Equal property values
  2. Whether two classes share a superclass
  3. Whether two references point to the same instance
  4. Whether instances are Equatable
Show answer

Answer: Whether two references point to the same instance

=== is identity comparison; == uses Equatable value equality.

Lesson: Classes, Inheritance and Initialisation

Functions, Closures and Errors

10. How do you let a function modify a caller's variable in Swift?

  1. Use a var parameter
  2. Return a pointer
  3. Declare the parameter inout and pass the argument with &
  4. It is not possible
Show answer

Answer: Declare the parameter inout and pass the argument with &

inout parameters write their final value back to the caller's variable.

Lesson: Functions and Argument Labels

11. When must a closure parameter be marked @escaping?

  1. Always
  2. When the function stores the closure or calls it after returning
  3. When the closure returns a value
  4. When it uses $0
Show answer

Answer: When the function stores the closure or calls it after returning

Escaping closures outlive the function call, which affects memory management and capturing.

Lesson: Closures and Capture Lists

12. What does try? do with a thrown error?

  1. Converts the result to nil when an error is thrown
  2. Crashes the app
  3. Rethrows it automatically
  4. Prints it
Show answer

Answer: Converts the result to nil when an error is thrown

try? turns failures into nil optionals, discarding the error value.

Lesson: Error Handling

Protocols, Extensions and Generics

13. How can a protocol provide behaviour shared by all conforming types?

  1. Through a protocol extension with default implementations
  2. Through inheritance only
  3. By making the protocol a class
  4. It cannot
Show answer

Answer: Through a protocol extension with default implementations

Protocol extensions supply default implementations that conforming types inherit.

Lesson: Protocols and Extensions

14. When do you need `any Protocol` rather than `some Protocol`?

  1. Never
  2. When returning from SwiftUI body
  3. When using generics with constraints
  4. When values of different concrete types must be stored together, such as in [any Shape]
Show answer

Answer: When values of different concrete types must be stored together, such as in [any Shape]

Existentials allow heterogeneous values; opaque types stand for one fixed concrete type.

Lesson: Generics, Associated Types, some and any

15. Which protocol lets a Swift type be converted to and from JSON with JSONEncoder and JSONDecoder?

  1. Hashable
  2. Identifiable
  3. Codable
  4. Comparable
Show answer

Answer: Codable

Codable combines Encodable and Decodable for serialisation.

Lesson: Codable, Equatable, Hashable and Comparable

Memory, Access Control and Language Features

16. What happens to a weak reference when the object it refers to is deallocated?

  1. The app crashes
  2. It keeps the object alive
  3. It automatically becomes nil
  4. It points to a copy of the object
Show answer

Answer: It automatically becomes nil

Weak references are zeroed automatically, which is why they must be optional variables.

Lesson: Automatic Reference Counting

17. What is the default access level in Swift?

  1. private
  2. internal
  3. public
  4. open
Show answer

Answer: internal

Declarations are internal by default: visible throughout their module.

Lesson: Access Control and Modules

18. Which Swift feature makes SwiftUI's declarative view syntax inside VStack { ... } possible?

  1. Result builders
  2. Property wrappers
  3. Typed throws
  4. Access control
Show answer

Answer: Result builders

Result builders combine the statements in a closure into a single view value.

Lesson: Property Wrappers, Result Builders and Macros

Concurrency

19. What does `async let` do?

  1. Starts a child task that runs concurrently and is awaited later
  2. Runs code synchronously
  3. Creates a detached task with no parent
  4. Blocks the thread until done
Show answer

Answer: Starts a child task that runs concurrently and is awaited later

async let creates a concurrent child task bound to the current scope.

Lesson: async/await and Structured Concurrency

20. What does the Swift 6 language mode add to concurrency?

  1. A garbage collector
  2. Automatic multithreading of all loops
  3. Removal of async/await
  4. Compile-time checking of isolation and Sendable to prevent data races
Show answer

Answer: Compile-time checking of isolation and Sendable to prevent data races

Swift 6 turns potential data races into compile-time errors.

Lesson: Actors, MainActor, Sendable and Swift 6 Data-Race Safety

21. How does cancellation work in Swift concurrency?

  1. It forcibly kills the thread
  2. It restarts the task
  3. It is cooperative: tasks check for cancellation and stop themselves
  4. It is not supported
Show answer

Answer: It is cooperative: tasks check for cancellation and stop themselves

cancel() sets a flag that code observes via checkCancellation or isCancelled.

Lesson: Cancellation and AsyncSequence

Apps, Testing, Ecosystem and Revision

22. In SwiftUI, what does `var body: some View` use?

  1. An existential any View
  2. A class hierarchy
  3. An opaque return type: one specific view type hidden from callers
  4. A property wrapper
Show answer

Answer: An opaque return type: one specific view type hidden from callers

some View lets the compiler know the exact type while keeping it hidden and efficient.

Lesson: A First Look at SwiftUI

23. In the Swift Testing framework, which macro checks a condition and reports sub-expression values on failure?

  1. XCTAssertTrue
  2. #expect
  3. @Test
  4. @testable
Show answer

Answer: #expect

#expect is Swift Testing's primary expectation macro.

Lesson: Testing with Swift Testing and XCTest

25. Which keyword marks a class reference that does not keep the object alive and becomes nil automatically?

  1. strong
  2. unowned
  3. lazy
  4. weak
Show answer

Answer: weak

weak references are zeroing and do not increase the reference count.

Lesson: Revision and Interview Questions

← Back to the course