All interview guides

Swift Interview Questions and Answers

24 questions that come up in Swift technical interviews, each with the answer and an explanation of why it is right.

Test yourself — 90 question bank

1. What is generics?

Advanced

Answer: Write flexible, reusable functions and types

Generics enable type-safe reusable code: func swap<T>(_ a: inout T, _ b: inout T). Array<T>, Dictionary<K, V>. Constraints with where. Core Swift feature.

2. What is Swift?

Beginner

Answer: Modern, safe, fast programming language for Apple platforms

Swift is Apple's modern programming language for iOS, macOS, watchOS, and tvOS. Type-safe, fast, expressive. Introduced in 2014. Replaces Objective-C.

3. What is a protocol?

Intermediate

Answer: All of the above

Protocol defines requirements: protocol Drawable { func draw() }. Types conform: class Circle: Drawable. Multiple protocols. Protocol-oriented programming. Core Swift paradigm.

4. What is ARC?

Advanced

Answer: Automatic Reference Counting for memory management

ARC automatically manages memory. Tracks references. Deallocates at zero. Classes only. Strong/weak/unowned references. No garbage collection. Deterministic cleanup.

5. What is protocol-oriented programming?

Intermediate

Answer: Design paradigm using protocols over inheritance

POP uses protocols and extensions. Composition over inheritance. Protocol extensions provide default implementations. Value types with protocol conformance. Swift philosophy.

6. What is the difference between var and let?

Beginner

Answer: var is mutable, let is immutable

var creates mutable variable. let creates immutable constant. Prefer let for safety. Use var only when value needs to change. Immutability is encouraged.

7. What is strong reference?

Advanced

Answer: Default reference, keeps object alive

Strong reference increases retain count. Default. Keeps object alive. let obj = MyClass(). Most common. Potential retain cycles. Be aware of ownership.

8. What is extension?

Intermediate

Answer: Adds functionality to existing types

Extensions add functionality: extension String { func reversed() -> String }. No subclassing needed. Retroactive modeling. Protocol conformance. Very powerful feature.

9. How do you declare a variable?

Beginner

Answer: Both a and b

var age: Int = 25 for mutable. let name = "John" for immutable. Type inference: let count = 5. Explicit type: var price: Double = 9.99. Both are valid.

10. What is error handling?

Intermediate

Answer: do-catch-throw for managing errors

Error handling: throw errors, catch in do-catch. func throws. try, try?, try!. Error protocol. Typed errors. Different from exceptions. Swift approach.

11. What are Swift's basic types?

Beginner

Answer: Int, Double, Bool, String, Character

Basic types: Int (integers), Double/Float (decimals), Bool (true/false), String (text), Character (single char). Type-safe. No implicit conversions.

12. What is weak reference?

Advanced

Answer: Optional reference, doesn't prevent deallocation

weak reference doesn't increase count. weak var delegate: Protocol? Always optional. Becomes nil when deallocated. Break retain cycles. Common for delegates.

13. What is unowned reference?

Advanced

Answer: Non-optional reference, assumes always valid

unowned assumes always valid. unowned let parent: Parent. Not optional. Crashes if accessed after deallocation. Use when guaranteed lifetime. Careful usage.

14. What is throw?

Intermediate

Answer: Propagates error from function

throw raises error: throw MyError.invalid. Function must be marked throws. Propagates up call stack. Caller must handle. Type-safe error propagation.

15. What is type inference?

Beginner

Answer: Compiler automatically determines type

Type inference: compiler determines type from initial value. let count = 5 infers Int. Still type-safe. Can specify explicitly: let count: Int = 5. Cleaner code.

16. How do you create a function?

Beginner

Answer: func name(param: Type) -> ReturnType { }

func greet(name: String) -> String { return "Hello, \\(name)" }. Parameters have labels. Return type with ->. Omit -> for Void. Clear syntax.

17. What is retain cycle?

Advanced

Answer: Two objects hold strong references to each other

Retain cycle: mutual strong references. Objects never deallocated. Memory leak. Break with weak/unowned. Common with closures. Use capture lists.

18. What is do-catch?

Intermediate

Answer: Handles throwing function errors

do-catch: do { try throwingFunc() } catch Error.specific { } catch { }. Multiple catch blocks. Pattern matching. Generic catch. Handle or propagate.

19. What is string interpolation?

Beginner

Answer: \(expression) embeds values in strings

String interpolation: "Hello, \\(name)". Any expression works: "Sum: \\(a + b)". More readable than concatenation. Type-safe conversion. Very Swift-like.

20. What is try??

Intermediate

Answer: try converts error to optional

try? converts result to optional. Returns nil on error. let result = try? throwingFunc(). Ignores error details. Use when error unimportant. Convenient shorthand.

21. What is capture list in closures?

Advanced

Answer: Specifies how closure captures values

Capture list: { [weak self] in }. Control reference type. Avoid retain cycles. [weak self], [unowned self], [weak obj]. Essential for memory safety.

22. What is try!?

Intermediate

Answer: Force try, crashes if error

try! force tries. Crashes on error. Use only when certain no error. let result = try! throwingFunc(). Dangerous. Avoid in production. Debug/test use.

23. How do you create an array?

Beginner

Answer: Both a and b

Arrays: var numbers: [Int] = [1, 2, 3] or var names = ["John", "Jane"]. Type inference. Shorthand [Int] or full Array<Int>. Both valid syntax.

24. What is async/await?

Advanced

Answer: Modern concurrency with async functions

async/await: async func fetch() -> Data. Call with await. Structured concurrency. Task and Task Group. Swift 5.5+ feature. Replaces completion handlers.

Ready to test yourself?

The full Swift bank has 90 questions across 3 difficulty levels — timed, shuffled, and scored.

Take the Swift quiz