What does this print?
let n = Int("4x")print(n ?? 0)What this Swift test measures, in short
This test checks whether you can predict what real Swift does: how optionals unwrap, when a struct is copied versus a class shared, how closures capture values, what protocol extensions dispatch to, and how enums, switch and try fit together.
Snippets assume Swift 5.9 or later at top level. The questions are our own and not standardised against a population, so treat the result as a knowledge check, not a certification.
By What's Your IQ · Reviewed 4 Oct 2026- Format
- 15 predict-the-output questions, 4 options each
- Topics
- Optionals, structs and classes, closures, protocols, enums and errors
- Version
- Swift 5.9+
- Language
- Started by Chris Lattner at Apple; announced 2014
What happens when the box is empty?
An Int? is a box that holds an Int or nothing. Pick what the user typed, then compare how each technique handles it.
if let n { print(n + 1)} else { print("none")}→ 43The everyday choice. Inside the braces n is a plain Int. Since Swift 5.7 you can write if let n without repeating = n.guard let n else { return "none"}return "\(n + 1)"→ "43"Exits early when nil and keeps n unwrapped for the rest of the function. The usual way to validate input.print((n ?? 0) + 1)→ 43Always produces a value. Good when a fallback really makes sense, risky when 0 hides a bug.
print(n?.description)→ Optional("42")Never crashes, but the result is still optional, so the problem moves further down your code.
print(n.map { $0 * 2 })→ Optional(84)Transforms the value if there is one, keeps nil if not. Useful in chains.print(n! + 1)→ 43Fine only when nil is truly impossible. When it isn't, the app crashes.
Objective-C habits, Swift solutions
let url = URL(string: text)!let data = try! Data(contentsOf: url)show(data)
guard let url = URL(string: text) else { return showError("Bad link")}do { show(try Data(contentsOf: url))} catch { showError(error.localizedDescription)}Every ! and try! is a possible crash. guard and do/catch handle the failure cases explicitly and keep the happy path flat.
- Swift.org. About Swift.
- Abrahams, D. (2015). Protocol-oriented programming in Swift. Apple WWDC, session 408.
- Swift.org. Swift 5.0 and 5.5 release announcements. Swift Blog.