Functional Programming Quiz - MCQ Questions and Answers

Functional Programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids changing state or mutable data. It emphasizes the use of pure functions, higher-order functions, and immutability.

This quiz will test your basic understanding of functional programming concepts, such as pure functions, immutability, higher-order functions, and more.

Let’s begin with these multiple-choice questions (MCQs) to test your knowledge of Functional Programming.

1. What is a pure function in functional programming?

a) A function that depends on external variables
b) A function that always produces the same output for the same input and has no side effects
c) A function that modifies the global state
d) A function that does not return a value

Answer:

b) A function that always produces the same output for the same input and has no side effects

Explanation:

A pure function is one that always returns the same output given the same input and does not produce any side effects, such as modifying global variables or performing I/O operations.

2. What does "immutability" mean in functional programming?

a) Data cannot be changed after it is created
b) Variables can be modified freely
c) Data is mutable but can only be changed by one function
d) Only functions are immutable

Answer:

a) Data cannot be changed after it is created

Explanation:

Immutability means that once data is created, it cannot be changed. Instead of modifying data, functional programming encourages creating new data structures with the required changes.

3. Which of the following is a key feature of functional programming?

a) Object inheritance
b) Mutable state
c) Higher-order functions
d) Imperative loops

Answer:

c) Higher-order functions

Explanation:

Higher-order functions are functions that take other functions as arguments or return them as results, which is a fundamental feature of functional programming.

4. What is a higher-order function?

a) A function that returns the maximum value
b) A function that takes another function as an argument or returns a function
c) A function that runs indefinitely
d) A function that uses global variables

Answer:

b) A function that takes another function as an argument or returns a function

Explanation:

A higher-order function is one that can take other functions as arguments or return them as results, which allows for more abstract and flexible code in functional programming.

5. In functional programming, what is a "side effect"?

a) A function that returns a value
b) A change in the system’s state or interaction with the outside world during function execution
c) A function with no return value
d) A function that never terminates

Answer:

b) A change in the system’s state or interaction with the outside world during function execution

Explanation:

A side effect occurs when a function modifies some state or interacts with the outside world, such as updating a global variable or performing I/O, which is discouraged in functional programming.

6. What is "recursion" in functional programming?

a) A function that calls itself
b) A function that modifies a global variable
c) A loop that repeats until a condition is met
d) A function that runs indefinitely

Answer:

a) A function that calls itself

Explanation:

Recursion is a technique in which a function calls itself to solve a problem, commonly used in functional programming to replace loops.

7. Which of the following is a common functional programming language?

a) Python
b) Java
c) Haskell
d) HTML

Answer:

c) Haskell

Explanation:

Haskell is a purely functional programming language known for its strong support for immutability and higher-order functions.

8. What is "referential transparency" in functional programming?

a) The ability to reference global variables
b) When a function consistently yields the same result given the same inputs
c) A property of functions that return nothing
d) The use of object-oriented features in functions

Answer:

b) When a function consistently yields the same result given the same inputs

Explanation:

Referential transparency means that a function will always return the same result when called with the same arguments, making the code more predictable and easier to reason about.

9. Which of the following is discouraged in functional programming?

a) Immutable data
b) Side effects
c) Higher-order functions
d) Pure functions

Answer:

b) Side effects

Explanation:

Side effects, such as modifying global variables or performing I/O operations, are discouraged in functional programming because they can make the behavior of programs less predictable.

10. What is "currying" in functional programming?

a) Transforming a function so that it takes multiple arguments into a sequence of functions, taking one argument
b) A technique to modify a function’s behavior
c) The process of memoization
d) Recursion on multiple arguments

Answer:

a) Transforming a function so that it takes multiple arguments into a sequence of functions, taking one argument

Explanation:

Currying is a technique used in functional programming. In this technique, a function with multiple arguments is transformed into a series of functions, each taking one argument.

11. In functional programming, which statement is true about loops?

a) Loops are commonly used
b) Loops are replaced with recursion or higher-order functions like map or reduce
c) Loops modify global state
d) Loops are used to introduce side effects

Answer:

b) Loops are replaced with recursion or higher-order functions like map or reduce

Explanation:

In functional programming, loops are typically replaced with recursion or higher-order functions like map and reduce to iterate over data structures.

12. Which of the following is an example of a higher-order function?

a) A function that takes a string as input
b) A function that returns a number
c) A function that takes another function as an argument
d) A function that modifies global variables

Answer:

c) A function that takes another function as an argument

Explanation:

A higher-order function is one that takes another function as an argument or returns a function as a result.

13. In functional programming, what does the map function do?

a) Transforms each element in a list using a function and returns a new list
b) Filters out elements from a list
c) Sorts the elements in a list
d) Modifies the original list in place

Answer:

a) Transforms each element in a list using a function and returns a new list

Explanation:

The map function applies a given function to each element in a list and returns a new list with the transformed values.

14. What is "lazy evaluation" in functional programming?

a) A function that runs slower than expected
b) An evaluation strategy where expressions are evaluated only when their value is needed
c) A method of memorizing function calls
d) A technique to create recursive loops

Answer:

b) An evaluation strategy where expressions are evaluated only when their value is needed

Explanation:

Lazy evaluation delays the evaluation of an expression until its value is actually needed, improving performance and reducing unnecessary calculations.

15. What does the reduce function do in functional programming?

a) Increases the size of a list
b) Transforms a list into a single value by applying a function cumulatively
c) Sorts elements in ascending order
d) Maps elements to their index

Answer:

b) Transforms a list into a single value by applying a function cumulatively

Explanation:

The reduce function applies a function cumulatively to the elements of a list, reducing the list to a single value (e.g., summing the elements).

16. Which of the following is a feature of functional programming?

a) Mutable state
b) Inheritance
c) First-class functions
d) Imperative loops

Answer:

c) First-class functions

Explanation:

First-class functions are a key feature of functional programming, meaning functions can be treated like any other value and passed around as arguments or returned from other functions.

17. What does "composition" refer to in functional programming?

a) The use of inheritance between classes
b) Combining two or more functions to form a new function
c) Defining functions inside a loop
d) Modifying the global state

Answer:

b) Combining two or more functions to form a new function

Explanation:

Function composition in functional programming is the process of combining two or more functions to produce a new function, where the output of one function becomes the input to the next.

18. In functional programming, what does "partial application" mean?

a) Applying a function to all arguments at once
b) Applying a function to some arguments and returning a new function for the remaining arguments
c) Applying a function conditionally
d) Using mutable state in functions

Answer:

b) Applying a function to some arguments and returning a new function for the remaining arguments

Explanation:

Partial application is a technique in which a function is applied to some of its arguments, producing a new function that takes the remaining arguments.

19. What is the primary benefit of using functional programming?

a) It increases the complexity of the code
b) It encourages immutability, making code easier to reason about and test
c) It encourages side effects
d) It allows frequent modification of global variables

Answer:

b) It encourages immutability, making code easier to reason about and test

Explanation:

One of the main benefits of functional programming is its emphasis on immutability and pure functions, which lead to more predictable, testable, and maintainable code.

20. What is a lambda function in functional programming?

a) A function with no arguments
b) An anonymous function defined without a name
c) A function that performs I/O operations
d) A function with global scope

Answer:

b) An anonymous function defined without a name

Explanation:

A lambda function is an anonymous function that is defined without a name and can be used as a value, often passed to higher-order functions.

These questions cover the basic concepts of functional programming, including pure functions, immutability, higher-order functions, recursion, and more. Understanding these fundamentals will help you get started with functional programming languages and techniques.

Comments