Closures in Swift are self-contained blocks of functionality that can capture values and are widely used for concise, reusable code. Learn syntax, capturing, and escaping to elevate your Swift programming.
1. Introduction to Closures
What are closures in Swift?
A) Functions without a name
B) Data structures
C) Operators for performing operations
D) Built-in frameworks
Which of the following is a key feature of closures?
A) Capturing values from the surrounding context
B) Inheriting properties from classes
C) Modifying global variables
D) Encapsulating external libraries
How are closures represented in Swift?
A) func keyword
B) Curly braces {}
C) Parentheses ()
D) closure keyword
What type of closures are predefined in Swift?
A) Global functions and nested functions
B) Anonymous classes
C) Closure variables
D) External dependencies
In which scenarios are closures commonly used?
A) Callback mechanisms
B) Variable initialization
C) Data persistence
D) Error handling
Which keyword indicates that a closure is escaping?
A) @escaping
B) @closure
C) @capture
D) @return
2. Capturing Values
What does a closure capture by default in Swift?
A) References to constants and variables from its surrounding scope
B) Only global variables
C) Memory addresses
D) Return values of functions
How do closures capture values in Swift?
A) By making copies of the values
B) By referencing the memory of surrounding variables
C) By creating temporary objects
D) By using external pointers
What happens to captured variables in closures when their scope ends?
A) They persist as long as the closure exists
B) They are destroyed
C) They are reset to default values
D) They are moved to global scope
How can a closure modify a captured variable?
A) By using inout
B) By declaring the variable as mutable
C) By default, captured variables cannot be modified
D) By copying the variable into the closure
Can closures capture multiple variables from their surrounding scope?
A) Yes, there is no limit
B) No, only one variable at a time
C) Yes, but only constants
D) No, closures cannot capture variables
What kind of memory management issue can closures create if not handled properly?
A) Strong reference cycles
B) Memory fragmentation
C) Dangling pointers
D) Stack overflow
3. Closure Syntax and Shorthand Arguments
What is the correct syntax for a closure in Swift?
A) { (parameters) -> ReturnType in code }
B) [parameters -> ReturnType : code]
C) (parameters, code) => ReturnType
D) { parameters : ReturnType : code }
What can be omitted in a closure if Swift can infer the type?
A) Parameter types and return type
B) Curly braces {}
C) The in keyword
D) The entire closure body
How are shorthand argument names represented in Swift closures?
A) $0, $1, $2
B) _0, _1, _2
C) arg0, arg1, arg2
D) @0, @1, @2
What does the in keyword in a closure signify?
A) The start of the closure’s body
B) Variable initialization
C) Capturing of values
D) Ending the closure
Can closures return values in Swift?
A) Yes, closures can have return types
B) No, closures are only for executing code
C) Yes, but only Bool values
D) No, closures cannot have outputs
What is the shortest form of a closure in Swift?
A) { $0 + $1 }
B) { (a: Int, b: Int) -> Int in a + b }
C) { (Int, Int) -> Int in code }
D) { return $0 }
4. Closures as Function Parameters
How are closures passed as parameters in Swift?
A) As arguments using their syntax
B) By reference only
C) By wrapping them in functions
D) By using closure: prefix
Which syntax declares a function that accepts a closure parameter?
A) func example(closure: () -> Void)
B) closure example(Void -> Void)
C) func example() -> closure {}
D) example(func closure)
What is a trailing closure in Swift?
A) A closure passed outside the function’s parentheses
B) A closure that executes last in a program
C) A closure attached to a return value
D) A closure with no parameters
Which keyword is used for multiple closures as function parameters?
A) None, closures are added sequentially
B) They are separated by commas
C) @chaining
D) closure+
Can a closure parameter have a default value?
A) Yes, like other parameters
B) No, closures must always be defined
C) Yes, but only if it returns Void
D) No, closures cannot be optional
What is the primary benefit of passing closures as parameters?
A) Enables higher-order functions
B) Reduces memory usage
C) Eliminates the need for variables
D) Ensures thread safety
5. Escaping and Non-Escaping Closures
What is an escaping closure?
A) A closure that is executed after a function returns
B) A closure that cannot capture values
C) A closure that modifies global variables
D) A closure that is self-referential
How are non-escaping closures executed?
A) Before the function exits
B) After the function returns
C) They are ignored
D) Only in global context
Which keyword is used to define an escaping closure?
A) @escaping
B) @closure
C) @async
D) @return
Why might an escaping closure cause a retain cycle?
A) It creates a strong reference to self
B) It does not release variables properly
C) It modifies global objects
D) It returns void
Which of the following is NOT true about escaping closures?
A) They can outlive the function’s execution
B) They require explicit syntax
C) They can only be synchronous
D) They can introduce reference cycles
What is the main difference between escaping and non-escaping closures?
A) Escaping closures are executed later, non-escaping closures are executed immediately
B) Non-escaping closures cannot be passed into functions
C) Escaping closures always return a value
D) Non-escaping closures are not supported in Swift
Here are the answers again in the tabular format:
Qno
Answer
1
A) Functions without a name
2
A) Capturing values from the surrounding context
3
B) Curly braces {}
4
A) Global functions and nested functions
5
A) Callback mechanisms
6
A) @escaping
7
A) References to constants and variables from its surrounding scope
8
B) By referencing the memory of surrounding variables
9
A) They persist as long as the closure exists
10
A) By using inout
11
A) Yes, there is no limit
12
A) Strong reference cycles
13
A) { (parameters) -> ReturnType in code }
14
A) Parameter types and return type
15
A) $0, $1, $2
16
A) The start of the closure’s body
17
A) Yes, closures can have return types
18
A) { $0 + $1 }
19
A) As arguments using their syntax
20
A) func example(closure: () -> Void)
21
A) A closure passed outside the function’s parentheses
22
B) They are separated by commas
23
A) Yes, like other parameters
24
A) Enables higher-order functions
25
A) A closure that is executed after a function returns
26
A) Before the function exits
27
A) @escaping
28
A) It creates a strong reference to self
29
C) They can only be synchronous
30
A) Escaping closures are executed later, non-escaping closures are executed immediately