Master the concepts of Functional Programming in Swift with 30 multiple-choice questions. Explore higher-order functions, closures, immutability, pure functions, lazy evaluation, and sequences to enhance your Swift skills.
1. Higher-Order Functions: map, filter, reduce
What is a higher-order function in Swift?
A) A function that returns a value
B) A function that accepts other functions as arguments
C) A function that performs a calculation
D) A function that performs a loop
Which of these is the correct signature for the map function in Swift?
A) map<T>(items: [T])
B) map<T>(transform: (T) -> T)
C) map<T>(closure: (T) -> U)
D) map<U>(transform: (T) -> U)
What does the filter function do in Swift?
A) It transforms elements of a collection
B) It removes elements based on a condition
C) It reduces elements to a single value
D) It maps values to a new array
The reduce function in Swift is used to:
A) Concatenate arrays
B) Map values to a collection
C) Combine elements into a single result
D) Filter elements based on a condition
Which of the following is an example of using map on an array in Swift?
A) [1, 2, 3].map { $0 * 2 }
B) [1, 2, 3].filter { $0 > 1 }
C) [1, 2, 3].reduce { $0 + $1 }
D) [1, 2, 3].map { $0 + 1 }
2. Closures in Functional Programming
What is a closure in Swift?
A) A function with no parameters
B) A function that accepts other functions as arguments
C) A block of code that can be passed around and executed
D) A class with no properties
Which of the following is the correct syntax for defining a closure in Swift?
A) let closure = { x, y in return x + y }
B) let closure = { (x: Int, y: Int) -> Int in return x + y }
C) let closure = { x, y => x + y }
D) let closure = (x, y) -> Int in return x + y
How does Swift handle trailing closure syntax?
A) It always requires parentheses
B) It allows the closure to be written outside the method call
C) It requires the closure to be an expression
D) It can only be used with functions that return values
What is the purpose of capturing values in closures?
A) To pass values between functions
B) To store values that the closure can access even after the function has returned
C) To bind values to a specific scope
D) To modify the values passed to the closure
Which of the following is an example of a closure with a captured value in Swift?
A) let closure = { a in return a * 2 }
B) let closure = { a in return a + 2 }
C) let closure = { [a] in return a + 2 }
D) let closure = { (a: Int) -> Int in return a + 2 }
3. Immutability and Pure Functions
What is immutability in functional programming?
A) The ability to change the value of variables
B) The inability to change the value of variables
C) The ability to modify values after they are defined
D) The process of making variables reusable
Which of the following is an example of an immutable variable in Swift?
A) var x = 10
B) let x = 10
C) x = 10
D) let x
A pure function is one that:
A) Depends on external states or variables
B) Always produces the same output for the same input
C) Modifies global variables
D) Performs side effects like printing to the console
Which of these is a characteristic of a pure function?
A) It modifies external state
B) It always returns a constant result for the same input
C) It has no return type
D) It relies on external data
Why is immutability important in functional programming?
A) It ensures functions can have side effects
B) It avoids accidental changes to data
C) It allows values to be changed after initialization
D) It makes the code harder to test
4. Lazy Evaluation and Sequences
What is lazy evaluation in Swift?
A) The process of evaluating values immediately
B) The process of evaluating values only when needed
C) The process of caching evaluated values
D) The process of evaluating values based on user input
How is lazy evaluation implemented in Swift?
A) Using the lazy keyword before a property
B) Using map function on collections
C) Using a for loop
D) Using guard statements
Which of these collections supports lazy evaluation in Swift?
A) Arrays
B) Dictionaries
C) Sequences
D) Sets
What does the lazy keyword in Swift do?
A) It makes a function lazy
B) It delays the computation of a property until it is accessed
C) It makes the function faster
D) It improves the performance of closures
What is a Sequence in Swift?
A) A collection that can only be iterated once
B) A type of closure
C) A collection of elements that can be accessed sequentially
D) A generic type that cannot be modified
5. More on Functional Programming
Which higher-order function can be used to transform elements of a collection in Swift?
A) filter
B) reduce
C) map
D) sort
How do you combine values in a collection into a single result using reduce in Swift?
A) By applying a transformation function to each value
B) By applying a filtering condition to each value
C) By applying an operation that combines the values
D) By mapping the values to a new collection
What is the output of the following code: [1, 2, 3].reduce(0, +)?
A) [1, 2, 3]
B) 6
C) 0
D) 3
Which of the following is a correct use of filter in Swift?
A) [1, 2, 3].filter { $0 > 1 }
B) [1, 2, 3].reduce { $0 + $1 }
C) [1, 2, 3].map { $0 * 2 }
D) [1, 2, 3].filter { $0 % 2 == 0 }
In Swift, which function can be used to eliminate elements from a collection based on a condition?
A) map
B) filter
C) reduce
D) sort
What does the flatMap function do in Swift?
A) Flattens a collection of collections into a single collection
B) Maps the values in a collection to new values
C) Filters values based on a condition
D) Reduces the collection to a single value
Which of these is true about closures in Swift?
A) Closures can capture values from their surrounding context
B) Closures cannot accept parameters
C) Closures do not have return types
D) Closures can only be used in functions
What is an advantage of using functional programming techniques like map, reduce, and filter?
A) They make the code harder to read
B) They help in improving code maintainability
C) They limit the flexibility of functions
D) They slow down the execution of programs
What is the effect of using lazy with an array in Swift?
A) The array is evaluated eagerly
B) The array is never evaluated
C) The array is evaluated only when accessed
D) The array is stored as a constant
What does the map function return in Swift?
A) A collection of transformed values
B) A new function
C) A single combined value
D) A filtered collection
Answers Table:
Qno
Answer
1
B) A function that accepts other functions as arguments
2
D) map<U>(transform: (T) -> U)
3
B) It removes elements based on a condition
4
C) Combine elements into a single result
5
A) [1, 2, 3].map { $0 * 2 }
6
C) A block of code that can be passed around and executed
7
B) let closure = { (x: Int, y: Int) -> Int in return x + y }
8
B) It allows the closure to be written outside the method call
9
B) To store values that the closure can access even after the function has returned
10
C) let closure = { [a] in return a + 2 }
11
B) The inability to change the value of variables
12
B) let x = 10
13
B) Always produces the same output for the same input
14
B) It always returns a constant result for the same input
15
B) It avoids accidental changes to data
16
B) The process of evaluating values only when needed
17
A) Using the lazy keyword before a property
18
C) Sequences
19
B) It delays the computation of a property until it is accessed
20
C) A collection of elements that can be accessed sequentially
21
C) map
22
C) By applying an operation that combines the values
23
B) 6
24
A) [1, 2, 3].filter { $0 > 1 }
25
B) filter
26
A) Flattens a collection of collections into a single collection
27
A) Closures can capture values from their surrounding context