December Code

Swift for placements: interview questions by topic

Swift is the language of iOS, iPadOS and macOS development, and product companies and agencies building apps for Apple's platforms interview on it directly. Candidates often arrive from Java, Kotlin or JavaScript, and interviewers know it, so the questions concentrate on what Swift does differently: optionals instead of null, structs as the default building block, protocols instead of deep class hierarchies, and memory managed by reference counting rather than a garbage collector.

Expect to explain when a struct beats a class and what happens when one is copied, how to unwrap an optional without crashing, what a closure captures and when it must be marked @escaping, how protocol extensions provide default behaviour, how enums with associated values model state, the difference between try, try? and try!, and how [weak self] breaks a reference cycle. Short snippets that ask what a few lines print are common, because they show quickly whether someone knows the rules.

The pages below take one topic each, with answers whose code was compiled with swiftc 6.4 and run, and one question exactly as the diagnostic asks it. The diagnostic draws from every Swift topic in the bank and names the ones to read first.

New to this? How to prepare for placements — the method — and what the readiness test measures.

Swift topics, one page each

8 of the 8 Swift topics in the bank have a page so far; the diagnostic draws from all 8.

  1. Swift optionals interview questions

    Swift optionals for interviews: what an optional is, if let and guard let, force unwrapping, optional chaining, Optional.map and switch.

  2. Swift structs and classes interview questions

    Swift structs and classes for interviews: value versus reference types, mutating, ===, inout, convenience initializers and property observers.

  3. Swift closures interview questions

    Swift closures for interviews: closure syntax, trailing closures and $0, capturing values, passing functions, reduce and nested functions.

  4. Swift protocols interview questions

    Swift protocols for interviews: requirements, protocol extensions, extending types, associated types, synthesized Hashable, composition, generics.

  5. Swift enums interview questions

    Swift enums for interviews: enums with methods, associated values, raw values, switch patterns, CaseIterable, indirect enums and Optional.

  6. Swift error handling interview questions

    Swift error handling for interviews: throws and do/catch, try? and try!, custom errors with values, defer, the Result type and typed throws.

  7. Swift collections interview questions

    Swift collections for interviews: Array, Set and Dictionary, safe lookups, filter and map, iterating dictionaries, sets, strings and slices.

  8. Swift ARC and memory management interview questions

    Swift memory management for interviews: how ARC works, ARC versus garbage collection, reference cycles, [weak self], value types and leaks.

One question, exactly as the diagnostic asks it

From the Swift bank — 30 questions across 8 topics. The answer is marked because this one is public; in a sitting you choose first, then see why each option is right or wrong.

Optionals · easySWIFT-004

What does this Swift code print?

let x: Int? = 5
print(x)
print(x!)
  1. 15, then 5
  2. 2Optional(5), then Optional(5)
  3. 3It does not compile: print needs an unwrapped value
  4. 4Optional(5), then 5correct

x is an Int?, an optional wrapping 5. print accepts Any, so it receives the optional itself and describes it as Optional(5); the compiler warns that the expression is implicitly coerced from Int? to Any, which is a hint that this is rarely intended. x! force-unwraps the value, so the second line prints 5. 5, then 5 assumes print unwraps automatically. Optional(5) twice ignores the !. There is no compile error, only the warning. Use if let, ?? or string interpolation with a default to show optionals.

More on this topic: Swift optionals interview questions

Measure it

Reading the answers tells you what’s true. A diagnostic tells you what you get wrong.

10 Swift questions across its topics, easy to hard, about fifteen minutes. You get a readiness figure with the arithmetic shown, the topics you missed named, and a practice set sized for today. Free: 1 diagnostic a month and 15 problems a day. No card.

By Harshit · updated