MCQs on Functional Programming | Swift

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

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

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

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

  1. 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
  2. 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
  3. Which of these collections supports lazy evaluation in Swift?
    • A) Arrays
    • B) Dictionaries
    • C) Sequences
    • D) Sets
  4. 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
  5. 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

  1. Which higher-order function can be used to transform elements of a collection in Swift?
    • A) filter
    • B) reduce
    • C) map
    • D) sort
  2. 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
  3. What is the output of the following code: [1, 2, 3].reduce(0, +)?
    • A) [1, 2, 3]
    • B) 6
    • C) 0
    • D) 3
  4. 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 }
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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:

QnoAnswer
1B) A function that accepts other functions as arguments
2D) map<U>(transform: (T) -> U)
3B) It removes elements based on a condition
4C) Combine elements into a single result
5A) [1, 2, 3].map { $0 * 2 }
6C) A block of code that can be passed around and executed
7B) let closure = { (x: Int, y: Int) -> Int in return x + y }
8B) It allows the closure to be written outside the method call
9B) To store values that the closure can access even after the function has returned
10C) let closure = { [a] in return a + 2 }
11B) The inability to change the value of variables
12B) let x = 10
13B) Always produces the same output for the same input
14B) It always returns a constant result for the same input
15B) It avoids accidental changes to data
16B) The process of evaluating values only when needed
17A) Using the lazy keyword before a property
18C) Sequences
19B) It delays the computation of a property until it is accessed
20C) A collection of elements that can be accessed sequentially
21C) map
22C) By applying an operation that combines the values
23B) 6
24A) [1, 2, 3].filter { $0 > 1 }
25B) filter
26A) Flattens a collection of collections into a single collection
27A) Closures can capture values from their surrounding context
28B) They help in improving code maintainability
29C) The array is evaluated only when accessed
30A) A collection of transformed values

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