Dive deep into Chapter 20 with these 30 carefully designed multiple-choice questions (MCQs) covering Pure Functions, Immutability, map/filter/reduce, and Currying & Partial Application. Perfect for mastering functional programming!
Pure Functions (8 Questions)
Which of the following best defines a pure function? a) A function that modifies global state b) A function that relies on external input c) A function that returns the same output for the same input d) A function that uses random values internally
What is a characteristic of pure functions? a) They can modify arguments passed to them b) They don’t rely on external variables c) They must always return a Boolean value d) They can throw exceptions
Which of the following breaks the concept of purity in functional programming? a) Stateless functions b) Functions with no side effects c) Functions that read external files d) Functions that return predictable outputs
What makes debugging easier in the context of pure functions? a) They interact directly with I/O b) They involve no side effects c) They utilize global state d) They execute asynchronously
Which statement is not true about pure functions? a) They can return different outputs for the same input b) They do not change global variables c) They are easy to test d) They enable better optimization by compilers
Pure functions are primarily designed to: a) Perform in-place updates b) Reduce memory usage by reusing mutable objects c) Avoid side effects and external state dependency d) Introduce randomness into the application
A function add(a, b) that returns a + b is an example of: a) A pure function b) A higher-order function c) A function with side effects d) A state-dependent function
Why are pure functions essential in functional programming? a) They allow dynamic variable scoping b) They promote mutability in code c) They simplify reasoning and testing d) They reduce memory allocation
Immutability (7 Questions)
What does immutability mean in functional programming? a) Objects can be modified at runtime b) Objects remain unchanged after creation c) Functions rely on global variables d) State changes dynamically during execution
Which of these structures is typically immutable? a) JavaScript objects b) Python dictionaries c) Strings in most programming languages d) Arrays in Java
How can you enforce immutability in JavaScript? a) By using const keyword for variables b) By using the Object.freeze method c) By modifying nested properties of an object d) Both a and b
Immutability benefits functional programming by: a) Encouraging frequent state changes b) Making code harder to debug c) Preventing unintended side effects d) Reducing memory usage at runtime
What happens when you try to mutate an immutable object? a) It silently updates b) A runtime error occurs c) A new object is created with the updated value d) The program crashes
Which of the following is not a benefit of immutability? a) Easier concurrency handling b) Simplified debugging and testing c) Reduced object copying d) Avoidance of state changes
In functional programming, immutability encourages: a) Frequent updates to the object state b) Using data structures that don’t change after creation c) Mutating objects for performance gains d) Relying on mutable global variables
map, filter, reduce in Functional Style (8 Questions)
What does the map function do in functional programming? a) It filters elements of an array b) It applies a function to every element in a collection c) It reduces the size of a collection d) It modifies an object directly
Which of the following transforms a list of numbers by doubling each value? a) list.filter(x => x * 2) b) list.map(x => x * 2) c) list.reduce((a, b) => a + b) d) list.find(x => x * 2)
The filter function in functional programming is used to: a) Aggregate values in an array b) Modify all elements in an array c) Select elements matching a given condition d) None of the above
Which operation is typically achieved using reduce? a) Transforming elements of an array b) Concatenating strings in an array c) Summing all elements in a collection d) Both b and c
What is returned when reduce is applied to an empty array without an initial value? a) An empty array b) A TypeError c) The last element in the array d) A zero value
Which of these is an advantage of using map, filter, and reduce? a) They are mutable operations b) They promote declarative programming style c) They inherently involve side effects d) They directly modify the original array
What is the output of [1, 2, 3].filter(x => x % 2 === 0)? a) [1, 3] b) [2] c) [1, 2, 3] d) []
Combining map and reduce allows: a) Data transformation and aggregation in a single operation b) Efficient mutation of global state c) Immediate side effects on the input data d) Simplified nested loops in imperative programming
Currying and Partial Application (7 Questions)
Currying transforms a function into: a) A function that accepts a fixed number of arguments b) A series of functions that each accept one argument c) A function with no arguments d) A recursive function
What is the purpose of currying? a) To reduce the complexity of loops b) To break down multi-argument functions into single-argument functions c) To modify external state d) To optimize memory usage
Partial application is a technique where: a) A function is executed partially and deferred b) A new function is created with some arguments pre-filled c) Arguments are processed lazily d) Functions are curried
How does currying differ from partial application? a) Currying transforms functions; partial application pre-fills arguments b) Currying allows mutable operations c) Partial application creates recursive functions d) Currying changes return types
Given f(a, b) = a + b, what does g = f.bind(null, 2) create? a) A function g that adds 2 to its argument b) A function g that multiplies its argument by 2 c) A function g that accepts no arguments d) None of the above
In JavaScript, which library is commonly used for currying functions? a) Lodash b) jQuery c) Bootstrap d) Axios
Currying is beneficial because it: a) Forces all arguments to be passed at once b) Enables partial evaluation of functions c) Reduces function overloading d) Supports mutable programming
Answer Key
Qno
Answer
1
c) A function that returns the same output for the same input
2
b) They don’t rely on external variables
3
c) Functions that read external files
4
b) They involve no side effects
5
a) They can return different outputs for the same input
6
c) Avoid side effects and external state dependency
7
a) A pure function
8
c) They simplify reasoning and testing
9
b) Objects remain unchanged after creation
10
c) Strings in most programming languages
11
d) Both a and b
12
c) Preventing unintended side effects
13
c) A new object is created with the updated value
14
c) Reduced object copying
15
b) Using data structures that don’t change after creation
16
b) It applies a function to every element in a collection
17
b) list.map(x => x * 2)
18
c) Select elements matching a given condition
19
d) Both b and c
20
b) A TypeError
21
b) They promote declarative programming style
22
b) [2]
23
a) Data transformation and aggregation in a single operation
24
b) A series of functions that each accept one argument
25
b) To break down multi-argument functions into single-argument functions
26
b) A new function is created with some arguments pre-filled
27
a) Currying transforms functions; partial application pre-fills arguments