JavaScript MCQs – ES6+ Features and Functional Programming

21.) What is the result of the following code?

const arr = [1, 2, 3];  
const [a, ...rest] = arr;  
console.log(a, rest);
A) 1 [2, 3]
B) 1 [1, 2, 3]
C) [1, 2] 3
D) Error

Answer: Option A

Explanation: The …rest syntax collects the remaining elements of the array into a new array. Here, a is assigned 1, and rest is [2, 3].

22.) What is the output of the following code?

const obj = { x: 10, y: 20 };  
const { x, z = 30 } = obj;  
console.log(x, z);
A) 10 undefined
B) 10 30
C) undefined 30
D) Error

Answer: Option B

Explanation: The destructuring syntax assigns z a default value of 30 because z is not a property of the object.

23.) What will be logged in the following code?

const multiply = (x, y = 2) => x * y;  
console.log(multiply(3));
A) 3
B) 6
C) undefined
D) Error

Answer: Option B

Explanation: The function has a default parameter for y, so calling multiply(3) results in 3 * 2 = 6.

24.) What is the output of the following code?

const square = n => n * n;  
console.log([1, 2, 3].map(square));
A) Error
B) [1, 2, 3]
C) [1, 4, 9]
D) [1, 4, 9, undefined]

Answer: Option C

Explanation: The map() method applies the square function to each element of the array, producing [1, 4, 9].

25.) What is the result of the following code?

const obj1 = { a: 1, b: 2 };  
const obj2 = { b: 3, c: 4 };  
const merged = { ...obj1, ...obj2 };  
console.log(merged);
A) { a: 1, b: 2, c: 4 }
B) { a: 1, b: 3, c: 4 }
C) { b: 3, c: 4 }
D) Error

Answer: Option B

Explanation: The spread operator merges obj1 and obj2, and if keys overlap, the value from the latter object (obj2) overrides the former (obj1).

Leave a Reply

Your email address will not be published. Required fields are marked *