All quizzesHard
Composition & Monads
Preview — 3 of 10 questions
Which of the following is a characteristic of a pure function?
javascript
// Pure function
function add(a, b) {
return a + b;
}
add(2, 3); // Always returns 5
add(2, 3); // Always returns 5AIt always produces the same output given the same input.
BIt modifies its input arguments.
CIt relies on external states like global variables.
DIt always produces random output.
What is the benefit of immutability in functional programming?
javascript
const user = { name: "Alice", age: 30 };
// Modifying the original object
function addYear(user) {
user.age++; // Side effect: modifies the original
return user;
}
addYear(user);
console.log(user.age); // 31 (original was mutated!)AIt allows functions to modify input data directly.
BIt makes data predictable, reducing the chances of bugs caused by unintended side effects.
CIt increases the performance of the application.
DIt makes the code more complex to understand.
Which of the following is an example of a side effect in JavaScript?
javascript
let globalCount = 0;
// Impure function with side effects
function processUser(user) {
globalCount++; // Side effect 1: modifies global state
user.processed = true; // Side effect 2: modifies parameter
console.log("Processing..."); // Side effect 3: I/O operation
return user;
}AReturning a value from a function.
BModifying a global variable inside a function.
CCalculating a mathematical result.
DAssigning a value to a local variable.
Sign up free to play
Answer all 10 questions (7 more), see explanations for every answer, and track your score.