MCQs on Closures | Swift

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

  1. What are closures in Swift?
    • A) Functions without a name
    • B) Data structures
    • C) Operators for performing operations
    • D) Built-in frameworks
  2. 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
  3. How are closures represented in Swift?
    • A) func keyword
    • B) Curly braces {}
    • C) Parentheses ()
    • D) closure keyword
  4. What type of closures are predefined in Swift?
    • A) Global functions and nested functions
    • B) Anonymous classes
    • C) Closure variables
    • D) External dependencies
  5. In which scenarios are closures commonly used?
    • A) Callback mechanisms
    • B) Variable initialization
    • C) Data persistence
    • D) Error handling
  6. Which keyword indicates that a closure is escaping?
    • A) @escaping
    • B) @closure
    • C) @capture
    • D) @return

2. Capturing Values

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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

  1. 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 }
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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

  1. 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
  2. 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)
  3. 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
  4. 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+
  5. 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
  6. 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

  1. 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
  2. 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
  3. Which keyword is used to define an escaping closure?
    • A) @escaping
    • B) @closure
    • C) @async
    • D) @return
  4. 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
  5. 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
  6. 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:

QnoAnswer
1A) Functions without a name
2A) Capturing values from the surrounding context
3B) Curly braces {}
4A) Global functions and nested functions
5A) Callback mechanisms
6A) @escaping
7A) References to constants and variables from its surrounding scope
8B) By referencing the memory of surrounding variables
9A) They persist as long as the closure exists
10A) By using inout
11A) Yes, there is no limit
12A) Strong reference cycles
13A) { (parameters) -> ReturnType in code }
14A) Parameter types and return type
15A) $0, $1, $2
16A) The start of the closure’s body
17A) Yes, closures can have return types
18A) { $0 + $1 }
19A) As arguments using their syntax
20A) func example(closure: () -> Void)
21A) A closure passed outside the function’s parentheses
22B) They are separated by commas
23A) Yes, like other parameters
24A) Enables higher-order functions
25A) A closure that is executed after a function returns
26A) Before the function exits
27A) @escaping
28A) It creates a strong reference to self
29C) They can only be synchronous
30A) Escaping closures are executed later, non-escaping closures are executed immediately

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top