JavaScript Quiz Questions 11-20

Question 11

What will the following code output?

console.log(1 + “2” + “2”);

a) 5
b) “32”
c) “122”
d) NaN

Answer: c) “122”

Explanation: When adding a number to a string, JavaScript performs string concatenation. 1 + “2” results in “12”, and then “12” + “2” results in “122”.

Question 12

What is the output of the following code?

console.log([] + []);

a) “”
b) []
c) undefined
d) NaN

Answer: a) “”

Explanation: When adding two empty arrays, JavaScript converts them to strings before concatenation. An empty array converts to an empty string, so the result is an empty string.

Question 13

What will be the result of the following code?

console.log(typeof null);

a) object
b) null
c) undefined
d) number

Answer: a) object

Explanation: In JavaScript, null is considered a primitive value but its type is object. This is a known bug in JavaScript and has been present since its inception.

Question 14

What does the Array.prototype.reduce method do? a) Combines the elements of an array into a single value
b) Removes the last element of an array
c) Filters the elements of an array
d) Maps the elements of an array

Answer: a) Combines the elements of an array into a single value

Explanation: The reduce method applies a function against an accumulator and each element in the array to reduce it to a single value.

Question 15

What will be the output of the following code?

const a = 3;

const b = new Number(3);

console.log(a == b, a === b);

a) true true
b) false false
c) true false
d) false true

Answer: c) true false

Explanation: The == operator compares values for equality after type coercion, so a == b is true. The === operator compares both value and type, and since a is a number and b is an object, a === b is false.

Question 16

What will the following code output?

console.log(typeof function() {});

a) function
b) object
c) undefined
d) number

Answer: a) function

Explanation: The typeof operator returns function for functions in JavaScript.

Question 17

What will the following code log to the console?

let a = 10;

{

  let a = 20;

  console.log(a);

}

console.log(a);

a) 10 10
b) 20 20
c) 20 10
d) 10 20

Answer: c) 20 10

Explanation: The let keyword declares block-scoped variables. Inside the block, a is 20. Outside the block, a remains 10.

Question 18

Which statement is true about the this keyword in JavaScript? a) It always refers to the global object
b) It refers to the object from which it was called
c) It cannot be used inside functions
d) It is a constant value

Answer: b) It refers to the object from which it was called

Explanation: The value of this depends on the context in which it is called. It refers to the object it belongs to in object methods, the global object in global context, and can be different in other scenarios such as arrow functions or event handlers.

Question 19

What is the purpose of the setTimeout function in JavaScript? a) To execute a function after a specified delay
b) To create an interval that repeatedly calls a function
c) To stop the execution of a function
d) To immediately execute a function

Answer: a) To execute a function after a specified delay

Explanation: setTimeout is used to execute a function once after a specified number of milliseconds.

Question 20

What will be the output of the following code?

const obj = {

  a: 1,

  b: 2,

  c: 3

};

console.log(Object.keys(obj));

a) [1, 2, 3]
b) [‘a’, ‘b’, ‘c’]
c) [a, b, c]
d) [‘1’, ‘2’, ‘3’]

Answer: b) [‘a’, ‘b’, ‘c’]

Explanation: Object.keys(obj) returns an array of the object’s own enumerable property names, in this case, [‘a’, ‘b’, ‘c’].