Functions & Closures Questions with Answers

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

Sample questions

Question 1Easy

What is the output?

func f(x: Int = 4) -> Int { x * x }
print(f())
  1. 16
  2. 4
  3. 8
  4. Compile error

Answer: A. 16

Explanation

With no argument, x uses its default 4, and 4 * 4 = 16.

Step-by-step solution

  1. x defaults to 4.
  2. x * x = 16.
  3. 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?

  1. func f() -> Int
  2. def f(): Int
  3. function f(): int
  4. int f()

Answer: A. func f() -> Int

Explanation

Swift uses func and -> for the return type.

Step-by-step solution

  1. Swift declares functions with the func keyword.
  2. The return type comes after -> ; func f() -> Int returns an Int.
  3. 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")
  1. Hi name
  2. Hi to Ann
  3. Hi Ann
  4. 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

  1. Call uses to:.
  2. Inside, name = "Ann".
  3. 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))
  1. 5
  2. 23
  3. 6
  4. Compile error

Answer: A. 5

Explanation

A single expression body returns its value without return.

Step-by-step solution

  1. a=2, b=3.
  2. Add.
  3. 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?

  1. The closure is not allowed to throw any errors
  2. The closure may be called after the function returns
  3. The closure is always run on the main thread
  4. 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

  1. An escaping closure can be stored or run after the function returns.
  2. This happens for stored callbacks or async work.
  3. 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?

  1. It runs the argument immediately
  2. It wraps the passed expression in a closure so it is evaluated only if used
  3. It makes the parameter optional
  4. 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

  1. Expression wrapped automatically.
  2. Evaluated only when called.
  3. 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 })
  1. [2]
  2. [nil, 2, nil]
  3. [1, 2, 3]
  4. [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 -> 1.
  2. 2 -> nil.
  3. [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))
  1. 10 6
  2. 10 10
  3. 6 10
  4. Compile error

Answer: A. 10 6

Explanation

f() uses the default 5 giving 10, and f(x: 3) gives 6.

Step-by-step solution

  1. f() uses the default x = 5, so it returns 5 × 2 = 10.
  2. f(x: 3) passes 3, so it returns 3 × 2 = 6.
  3. Print shows the two results separated by a space.
  4. 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?

  1. The closure from ever running more than a single time
  2. A retain cycle between the closure and the object that owns it
  3. The closure from being able to access self at all
  4. 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

  1. A strong closure capture of self can keep self and the closure alive in a loop.
  2. [weak self] holds self weakly, so no strong cycle forms.
  3. 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))
  1. 5
  2. Optional(5)
  3. 23
  4. Compile error

Answer: A. 5

Explanation

The dictionary value is a closure; force-unwrapping and calling it gives 5.

Step-by-step solution

  1. Look up "+".
  2. Unwrap and call.
  3. 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 →

Related topics

See all Swift topics · All topics