1. What is generics?
AdvancedAnswer: 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.
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 bankAnswer: 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.
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.
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.
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.
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.
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.
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.
Answer: Adds functionality to existing types
Extensions add functionality: extension String { func reversed() -> String }. No subclassing needed. Retroactive modeling. Protocol conformance. Very powerful feature.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Answer: Handles throwing function errors
do-catch: do { try throwingFunc() } catch Error.specific { } catch { }. Multiple catch blocks. Pattern matching. Generic catch. Handle or propagate.
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.
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.
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.
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.
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.
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.
The full Swift bank has 90 questions across 3 difficulty levels — timed, shuffled, and scored.
Take the Swift quiz