Closures Questions with Answers

12 practice questions on Closures (iOS (Swift & SwiftUI)) with answers and step-by-step solutions. Try 10 samples below, then practise all 12 free in the AptiNest app.

Sample questions

Question 1Easy

Are Swift closures value types or reference types?

  1. Neither; they are copied as raw pointers
  2. Value types unless marked @escaping
  3. Reference types
  4. Value types

Answer: C. Reference types

Explanation

Closures are reference types: assigning one to two constants makes both refer to the same closure and its captured state.

Step-by-step solution

  1. Swift closures are reference types: a closure shares the variables it captures.
  2. They are not copied like structs, so they are not value types.
  3. So the answer is option C.

Why the other options are wrong

  • A. Closures are not copied as raw pointers in Swift code.
  • B. @escaping controls how a closure can be stored, not whether it is a value type.
  • D. Closures are not value types in this sense.

Tip: Closures share their captured variables, so they behave like reference objects.

Question 2Easy

In a closure such as { $0 * 2 }, what is $0?

  1. The shorthand name for the closure's first argument
  2. The number of arguments passed to the closure
  3. The closure's return value
  4. A reference to the enclosing function

Answer: A. The shorthand name for the closure's first argument

Explanation

Swift gives closure arguments the shorthand names $0, $1 and so on, so short closures don't need a parameter list.

Step-by-step solution

  1. In Swift closures, $0 refers to the first argument passed to the closure.
  2. $0 is used as a shorthand for the closure's first parameter without needing a full parameter list.
  3. The closure { $0 * 2 } uses $0 as its only argument, which is multiplied by 2.
  4. This makes $0 equivalent to the closure's first argument in this context.
  5. Therefore, $0 is the shorthand name for the closure's first argument.

Why the other options are wrong

  • B. $0 is not a count of arguments; it names the first argument itself.
  • C. $0 is not the return value; the result is the value of the expression.
  • D. $0 is not a reference to the enclosing function; it is just the first parameter.

Tip: Remember: $0 is always the first argument in a closure with no explicit parameter names.

Question 3Easy

What is the output?

print(["1", "x", "3"].compactMap { Int($0) })
  1. [1, 0, 3]
  2. [1, 3]
  3. ["1", "3"]
  4. [1, nil, 3]

Answer: B. [1, 3]

Explanation

compactMap applies the transform and drops nil results. Int("x") is nil, so only 1 and 3 remain.

Step-by-step solution

  1. Int("1") = 1, so first element is 1.
  2. Int("x") = nil, so it's removed.
  3. Int("3") = 3, so last element is 3.
  4. Result is [1, 3].
  5. Final answer: [1, 3]

Why the other options are wrong

  • A. The x value is not turned into 0; Int of it is nil, and compactMap drops nil results.
  • C. Strings are not kept; Int converts them, so the output holds numbers.
  • D. nil is not kept in the result; compactMap removes nil values, so none is printed.

Tip: Use compactMap to filter out nils from mapped results.

Question 4Easy

What is the output?

let nums = [3, 1, 2]
print(nums.sorted { $0 > $1 })
  1. [1, 2, 3]
  2. [3, 1, 2]
  3. [3, 2, 1]
  4. Compile error: missing parentheses

Answer: C. [3, 2, 1]

Explanation

sorted takes the comparison closure as a trailing closure. $0 > $1 sorts in descending order.

Step-by-step solution

  1. Nums is initialized to [3, 1, 2].
  2. Sorted with { $0 > $1 } arranges elements from highest to lowest.
  3. 3 is higher than 2, so 3 comes first.
  4. 2 is higher than 1, so 2 comes next.
  5. Final sorted array is [3, 2, 1].
  6. Output is [3, 2, 1].

Why the other options are wrong

  • A. [1, 2, 3] is the ascending order; the closure asks for descending order.
  • B. The output is not the original order; sorted returns a new sorted array.
  • D. No compile error: trailing closure syntax is valid with sorted, so it compiles.

Tip: Use $0 > $1 for descending sort in Swift.

Question 5Medium

Inside an escaping closure in a class method, why does Swift require self. to be written explicitly (unless self is captured in a capture list)?

  1. To make the strong capture of self visible to the programmer
  2. Because the closure runs on a different actor
  3. Because properties cannot be accessed without self in a class
  4. To tell the compiler the closure is non-escaping

Answer: A. To make the strong capture of self visible to the programmer

Explanation

An escaping closure that uses self keeps the object alive, which can cause retain cycles. Requiring self. makes the capture explicit so you think about using [weak self].

Step-by-step solution

  1. An escaping closure outlives the call.
  2. If it uses self, it holds a strong reference.
  3. Writing self. makes the capture visible.

Why the other options are wrong

  • B. Actors are not the reason.
  • C. Properties can be used without self in many places.
  • D. Explicit self signals escaping, not non-escaping.

Tip: Escaping + self: think [weak self].

Question 6Medium

What is the output?

func twice(_ f: (Int) -> Int, _ v: Int) -> Int { f(f(v)) }
print(twice({ $0 + 1 }, 0))
  1. 4
  2. 3
  3. 6
  4. 2

Answer: D. 2

Explanation

Applying the closure twice to 0: 0 + 1 = 1, then 1 + 1 = 2. Passing a closure as a value works like passing any function.

Step-by-step solution

  1. First apply the closure to 0: 0 + 1 = 1.
  2. Then apply the closure again to result: 1 + 1 = 2.
  3. Final output is 2.

Why the other options are wrong

  • A. 4 would need the closure to add 2 each time; here it adds 1 twice, giving 2.
  • B. 3 would need only one application of the closure; twice applies it two times.
  • C. 6 is wrong; the closure adds 1, it does not multiply.

Tip: Apply function step by step and track intermediate values.

Question 7Medium

When must a closure parameter be marked @escaping?

  1. When it is written with trailing closure syntax
  2. When it throws errors that the caller must handle
  3. When it may be called after the function has returned
  4. When it captures any variables from the caller

Answer: C. When it may be called after the function has returned

Explanation

Closure parameters are non-escaping by default. If the function stores the closure or calls it asynchronously later, it outlives the call and must be marked @escaping.

Step-by-step solution

  1. Closure parameters are non-escaping by default.
  2. If the closure is called after the function returns, it outlives the function call.
  3. Such closures must be marked @escaping to indicate they can be called later.
  4. This ensures the compiler knows the closure may live beyond the function's scope.
  5. The correct answer is when it may be called after the function has returned.
  6. Final Answer: C (When it may be called after the function has returned).

Why the other options are wrong

  • A. Trailing closure syntax is only a way to write a closure; it does not need @escaping.
  • B. Throwing is separate from escaping; a closure that throws does not need @escaping for that.
  • D. Capturing variables does not require @escaping; only a closure outliving the call does.

Tip: @escaping is needed when a closure outlives its enclosing function.

Question 8Medium

What is the output?

func makeCounter() -> () -> Int {
    var c = 0
    return { c += 1; return c }
}
let a = makeCounter()
let b = makeCounter()
_ = a()
print(a(), b())
  1. 2 1
  2. 2 2
  3. 1 1
  4. 1 2

Answer: A. 2 1

Explanation

Each call to makeCounter creates a new c captured by its own closure. a is called twice, so its second result is 2; b's first call returns 1.

Step-by-step solution

  1. MakeCounter creates a new c on each call, so a and b have separate counters.
  2. _ = a() moves a's counter to 1.
  3. print(a(), b()) calls a() first, making its counter 2, then b() for the first time, returning 1.
  4. Output is 2 1, so the answer is option A.

Why the other options are wrong

  • B. B has its own counter, so b() returns 1, not 2.
  • C. a() was already called once, so its next value is 2.
  • D. Arguments are evaluated left to right, so a() gives 2 first.

Tip: Each call to a function that returns a closure creates new captured variables.

Question 9Hard

What is the output?

var x = 1
let f = { [x] in print(x) }
x = 2
f()
  1. nil
  2. 2
  3. 1
  4. Compile error: x is captured twice

Answer: C. 1

Explanation

The capture list [x] copies the value of x when the closure is created. Changing x afterwards doesn't affect the copy.

Step-by-step solution

  1. The capture list [x] copies the value of x (1) when the closure is created.
  2. x = 2 changes the variable afterwards, but the closure keeps its copy.
  3. f() prints the captured 1.
  4. So the answer is option C.

Why the other options are wrong

  • A. Print runs and prints the captured value, not nil.
  • B. Printing 2 would need capture by reference; the list captures the value 1.
  • D. Capturing x once in a capture list is valid.

Tip: A capture list copies values at the moment the closure is created.

Question 10Hard

What does @autoclosure do to a parameter?

  1. Wraps the argument in a closure so it's evaluated lazily
  2. Runs the closure on the main thread
  3. Captures self weakly inside the closure body
  4. Lets the closure parameter escape the function automatically

Answer: A. Wraps the argument in a closure so it's evaluated lazily

Explanation

With @autoclosure the caller writes a plain expression, which is wrapped in a closure and evaluated only if needed. The ?? operator and assert use this to skip evaluating the right side.

Step-by-step solution

  1. The @autoclosure attribute wraps the parameter in a closure.
  2. This closure is evaluated lazily, only when needed.
  3. The caller writes a plain expression, not a closure.
  4. The argument is not executed until it's required.
  5. This avoids unnecessary computation and improves performance.

Why the other options are wrong

  • B. @autoclosure does not change threads; the closure runs where it is called.
  • C. Self capture is not changed by this attribute; it only wraps the argument expression.
  • D. It does not make the parameter escape; the argument stays non-escaping.

Tip: Use @autoclosure for lazy evaluation of parameters to avoid premature computation.

Practise all 12 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 →

Related topics

See all iOS (Swift & SwiftUI) topics · All topics