Sample questions
Question 1Easy
What is the output?
func f(x: Int = 4) -> Int { x * x }
print(f())
- 16
- 4
- 8
- Compile error
Answer: A. 16
Explanation
With no argument, x uses its default 4, and 4 * 4 = 16.
Step-by-step solution
- x defaults to 4.
- x * x = 16.
- 16.
Why the other options are wrong
- B. Default x is 4 and the function squares.
- C. 4 * 4 = 16.
- D. Defaults can be omitted.
Question 2Easy
Which syntax declares a Swift function returning an Int?
- func f() -> Int
- def f(): Int
- function f(): int
- int f()
Answer: A. func f() -> Int
Explanation
Swift uses func and -> for the return type.
Step-by-step solution
- Swift declares functions with the func keyword.
- The return type comes after -> ; func f() -> Int returns an Int.
- So option A is correct.
Why the other options are wrong
- B. Def is Python syntax, and the type is written after a colon.
- C. Function and int are not Swift keywords.
- D. Int f() is C-style syntax.
Tip: Swift: func name(params) -> ReturnType.
Question 3Easy
What is the output?
func greet(to name: String) { print("Hi \(name)") }
greet(to: "Ann")
- Hi name
- Hi to Ann
- Hi Ann
- Compile error
Answer: C. Hi Ann
Explanation
to is the external label used at the call site; name is the internal parameter name.
Step-by-step solution
- Call uses to:.
- Inside, name = "Ann".
- Hi Ann.
Why the other options are wrong
- A. The internal name holds the value.
- B. The label is not printed.
- D.
to is an argument label.
Question 4Easy
What is the output?
func add(_ a: Int, _ b: Int) -> Int { a + b }
print(add(2, 3))
- 5
- 23
- 6
- Compile error
Answer: A. 5
Explanation
A single expression body returns its value without return.
Step-by-step solution
- a=2, b=3.
- Add.
- 5.
Why the other options are wrong
- B. The function adds numbers.
- C. 2 + 3 = 5.
- D. Single-expression bodies return implicitly.
Question 5Medium
What does the @escaping attribute indicate on a closure parameter?
- The closure is not allowed to throw any errors
- The closure may be called after the function returns
- The closure is always run on the main thread
- The closure is copied by value and never shared
Answer: B. The closure may be called after the function returns
Explanation
Escaping closures outlive the call, for example stored for later or run asynchronously.
Step-by-step solution
- An escaping closure can be stored or run after the function returns.
- This happens for stored callbacks or async work.
- So option B is correct.
Why the other options are wrong
- A. @escaping does not restrict throwing.
- C. @escaping does not set the thread; it is about lifetime.
- D. Escaping closures are not copied by value; they are kept and called later.
Tip: If a closure is stored or called later, mark it @escaping.
Question 6Medium
What does @autoclosure do on a parameter?
- It runs the argument immediately
- It wraps the passed expression in a closure so it is evaluated only if used
- It makes the parameter optional
- It runs the closure on a background thread
Answer: B. It wraps the passed expression in a closure so it is evaluated only if used
Explanation
@autoclosure turns an argument expression into a closure, enabling lazy evaluation like in &&.
Step-by-step solution
- Expression wrapped automatically.
- Evaluated only when called.
- Used in assert and &&.
Why the other options are wrong
- A. It delays evaluation.
- C. It doesn't change optionality.
- D. It has no threading role.
Question 7Medium
What is the output?
print([1, 2, 3].compactMap { $0 % 2 == 0 ? nil : $0 })
- [2]
- [nil, 2, nil]
- [1, 2, 3]
- [1, 3]
Answer: D. [1, 3]
Explanation
compactMap keeps non-nil results; 2 maps to nil and is dropped.
Step-by-step solution
- 1 -> 1.
- 2 -> nil.
- [1, 3].
Why the other options are wrong
- A. compactMap removes nil results.
- B. compactMap drops nils.
- C. The even number maps to nil.
Question 8Medium
What is the output?
func f(x: Int = 5) -> Int { x * 2 }
print(f(), f(x: 3))
- 10 6
- 10 10
- 6 10
- Compile error
Answer: A. 10 6
Explanation
f() uses the default 5 giving 10, and f(x: 3) gives 6.
Step-by-step solution
- f() uses the default x = 5, so it returns 5 × 2 = 10.
- f(x: 3) passes 3, so it returns 3 × 2 = 6.
- Print shows the two results separated by a space.
- So the output is 10 6 (option A).
Why the other options are wrong
- B. f(x: 3) returns 6, not 10, because x is 3.
- C. The order is wrong: the first call uses the default and gives 10.
- D. Default parameter values and labelled arguments are valid Swift; the code compiles.
Tip: A parameter with a default value can be left out; a labelled call passes its value explicitly.
Question 9Hard
What does [weak self] in a closure capture list prevent?
- The closure from ever running more than a single time
- A retain cycle between the closure and the object that owns it
- The closure from being able to access self at all
- Automatic reference counting from tracking the object
Answer: B. A retain cycle between the closure and the object that owns it
Explanation
A weak reference does not increase the retain count, so self can be freed; inside, self becomes optional.
Step-by-step solution
- A strong closure capture of self can keep self and the closure alive in a loop.
- [weak self] holds self weakly, so no strong cycle forms.
- So it prevents a retain cycle (option B).
Why the other options are wrong
- A. The closure can still run many times; the capture list only changes ownership.
- C. The closure can still use self, but self is optional inside.
- D. ARC still tracks the object; weak only avoids the cycle.
Tip: Use [weak self] in escaping closures that capture the owning object.
Question 10Hard
What is the output?
let ops: [String: (Int, Int) -> Int] = ["+": { $0 + $1 }]
print(ops["+"]!(2, 3))
- 5
- Optional(5)
- 23
- Compile error
Answer: A. 5
Explanation
The dictionary value is a closure; force-unwrapping and calling it gives 5.
Step-by-step solution
- Look up "+".
- Unwrap and call.
- 2 + 3 = 5.
Why the other options are wrong
- B. The call is force-unwrapped.
- C. The closure adds integers.
- D. Functions are first-class values.
Practise all 21 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →