JavaScript is a dynamic and powerful language that is essential for web development. Here are 50 more multiple-choice questions with detailed explanations to help you further enhance your knowledge and skills.
Question 1
What will the following code output?
console.log([] == []);
A) true
B) false
C) undefined
D) Error
Answer: B) false
Explanation: In JavaScript, two array references are only equal if they refer to the same instance. Here, [] == [] compares two different array instances, so it returns false.
Question 2
What is the output of the following code?
console.log(‘5’ + 5 + 5);
A) “15”
B) “555”
C) 15
D) “105”
Answer: B) “555”
Explanation: JavaScript performs string concatenation from left to right. ‘5’ + 5 results in “55” and “55” + 5 results in “555”.
Question 3
What is the result of the following code?
let x;
console.log(x === undefined);
A) true
B) false
C) null
D) Error
Answer: A) true
Explanation: If a variable is declared but not assigned a value, it is undefined by default. Therefore, x === undefined evaluates to true.
Question 4
Which of the following methods can be used to merge arrays? A) concat()
B) merge()
C) join()
D) combine()
Answer: A) concat()
Explanation: The concat() method is used to merge two or more arrays without modifying the existing arrays.
Question 5
What is the output of the following code?
let x = 10;
{
let x = 20;
console.log(x);
}
console.log(x);
A) 10, 10
B) 20, 20
C) 20, 10
D) 10, 20
Answer: C) 20, 10
Explanation: The let keyword allows for block-scoped variables. Inside the block, x is 20, and outside the block, x remains 10.
Question 6
What does the Array.prototype.find() method do? A) Returns the index of the first element that matches the condition.
B) Returns the first element that matches the condition.
C) Returns a new array with all elements that match the condition.
D) Returns true if at least one element matches the condition.
Answer: B) Returns the first element that matches the condition.
Explanation: The find() method returns the first element in the array that satisfies the provided testing function.
Question 7
What will be the output of the following code?
let obj = { a: 1, b: 2 };
console.log(‘a’ in obj);
A) true
B) false
C) “true”
D) “false”
Answer: A) true
Explanation: The in operator returns true if the specified property is in the specified object.
Question 8
What is the output of the following code?
console.log(typeof NaN === ‘number’);
A) true
B) false
C) undefined
D) NaN
Answer: A) true
Explanation: In JavaScript, NaN is of type “number”.
Question 9
Which method is used to check if an array includes a certain element? A) includes()
B) contains()
C) has()
D) within()
Answer: A) includes()
Explanation: The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.
Question 10
What is the result of the following code?
let a = [1, 2, 3];
let b = a.slice();
b.push(4);
console.log(a, b);
A) [1, 2, 3], [1, 2, 3, 4]
B) [1, 2, 3, 4], [1, 2, 3, 4]
C) [1, 2], [1, 2, 3, 4]
D) [1, 2, 3], [1, 2, 3]
Answer: A) [1, 2, 3], [1, 2, 3, 4]
Explanation: The slice() method returns a shallow copy of a portion of an array, so modifying b does not affect a.
Question 11
Which of the following is used to create an object prototype? A) proto
B) prototype
C) Object.create()
D) All of the above
Answer: D) All of the above
Explanation: All of the methods shown can be used to create or manipulate object prototypes in JavaScript.
Question 12
What will the following code output?
console.log(0 || 1);
A) 0
B) 1
C) true
D) false
Answer: B) 1
Explanation: The || operator returns the first truthy value, which in this case is 1.
Question 13
Which statement is used to skip the current iteration of a loop? A) skip
B) continue
C) break
D) pass
Answer: B) continue
Explanation: The continue statement terminates the current iteration of a loop and proceeds to the next iteration.
Question 14
What will be the output of the following code?
console.log(1 + “1” – 1);
A) “11”
B) 1
C) 10
D) “10”
Answer: C) 10
Explanation: 1 + “1” results in “11” (string concatenation), then “11” – 1 converts “11” to 11 (number) and performs subtraction, resulting in 10.
Question 15
What is the purpose of the call() method in JavaScript? A) It creates a new function that, when called, has its this keyword set to the provided value.
B) It calls a function with a given this value and arguments provided individually.
C) It calls a function with a given this value and arguments provided as an array.
D) It adds properties to an object.
Answer: B) It calls a function with a given this value and arguments provided individually.
Explanation: The call() method calls a function with a given this value and arguments provided individually.
Question 16
Which method converts a JavaScript object into a JSON string? A) JSON.stringify()
B) JSON.parse()
C) JSON.toString()
D) JSON.object()
Answer: A) JSON.stringify()
Explanation: JSON.stringify() converts a JavaScript object into a JSON string.
Question 17
What will be the output of the following code?
console.log(2 + ‘2’ – ‘2’);
A) “22”
B) 2
C) 0
D) NaN
Answer: B) 2
Explanation: 2 + ‘2’ results in “22”, then “22” – ‘2’ converts both to numbers and performs subtraction, resulting in 20.
Question 18
Which of the following methods is used to extract a section of a string and return it as a new string? A) slice()
B) split()
C) substr()
D) substring()
Answer: A) slice()
Explanation: The slice() method extracts a section of a string and returns it as a new string, without modifying the original string.
Question 19
What will be the output of the following code?
console.log(1 + 2 + ‘3’);
A) 33
B) “33”
C) 6
D) “123”
Answer: B) “33”
Explanation: 1 + 2 results in 3, then 3 + ‘3’ results in “33” due to string concatenation.
Question 20
Which of the following is a valid way to define a JavaScript function? A) function myFunc() {}
B) var myFunc = function() {};
C) var myFunc = () => {};
D) All of the above
Answer: D) All of the above
Explanation: All of the methods shown are valid ways to define a function in JavaScript.
Question 21
What will the following code output?
console.log(5 < 6 < 7);
A) true
B) false
C) undefined
D) Error
Answer: A) true
Explanation: 5 < 6 results in true, which is coerced to 1 in the next comparison, so 1 < 7 results in true.
Question 22
What will be the output of the following code?
let a = 1;
let b = 2;
let c = 3;
console.log(a && b || c);
A) 1
B) 2
C) 3
D) false
Answer: B) 2
Explanation: a && b evaluates to b (2), and 2 || c evaluates to 2.
Question 23
What will be the output of the following code?
console.log(null == undefined);
A) true
B) false
C) Error
D) undefined
Answer: A) true
Explanation: In JavaScript, null and undefined are considered equal with == but not with ===.
Question 24
What does the Array.prototype.reduce() method do? A) It executes a provided function once for each array element.
B) It creates a new array with the results of calling a provided function on every element in the calling array.
C) It creates a new array with all elements that pass the test implemented by the provided function.
D) It applies a function against an accumulator and each element in the array to reduce it to a single value.
Answer: D) It applies a function against an accumulator and each element in the array to reduce it to a single value.
Explanation: The reduce() method executes a reducer function (that you provide) on each element of the array, resulting in a single output value.
Question 25
What will the following code output?
console.log([] == ![]);
A) true
B) false
C) undefined
D) Error
Answer: A) true
Explanation: ![] evaluates to false, and [] == false is coerced to true.
Question 26
What is the output of the following code?
console.log(0 == false);
A) true
B) false
C) undefined
D) Error
Answer: A) true
Explanation: 0 is falsy in JavaScript, so 0 == false is true.
Question 27
Which method is used to remove properties from an object? A) delete
B) remove
C) unset
D) discard
Answer: A) delete
Explanation: The delete operator is used to remove properties from an object.
Question 28
What will be the output of the following code?
console.log(typeof []);
A) “object”
B) “array”
C) “undefined”
D) “null”
Answer: A) “object”
Explanation: Arrays are objects in JavaScript, so typeof [] returns “object”.
Question 29
What will be the output of the following code?
console.log(!!false);
A) true
B) false
C) 0
D) undefined
Answer: B) false
Explanation: !!false converts false to true and then back to false, resulting in false.
Question 30
What will the following code output?
console.log(1 + “1” + 1);
A) 111
B) 3
C) 21
D) 12
Answer: A) 111
Explanation: 1 + “1” results in “11”, then “11” + 1 results in “111” due to string concatenation.
Question 31
Which of the following methods is used to check if all elements in an array pass a test implemented by the provided function? A) every()
B) some()
C) filter()
D) map()
Answer: A) every()
Explanation: The every() method tests whether all elements in the array pass the test implemented by the provided function.
Question 32
What will be the output of the following code?
let x = 5;
console.log(++x);
A) 5
B) 6
C) 7
D) 4
Answer: B) 6
Explanation: The ++ operator increments x before returning its value, so ++x results in 6.
Question 33
Which method is used to convert a string to an integer? A) parseInt()
B) parseFloat()
C) Number()
D) toInteger()
Answer: A) parseInt()
Explanation: The parseInt() function parses a string argument and returns an integer.
Question 34
What will be the output of the following code?
console.log(Math.max(10, 20, 30));
A) 10
B) 20
C) 30
D) NaN
Answer: C) 30
Explanation: The Math.max() function returns the largest of the numbers given as input parameters.
Question 35
Which of the following is a valid way to create a regular expression in JavaScript? A) var regex = /pattern/;
B) var regex = new RegExp(‘pattern’);
C) var regex = RegExp(‘pattern’);
D) All of the above
Answer: D) All of the above
Explanation: All of the methods shown are valid ways to create a regular expression in JavaScript.
Question 36
What will be the output of the following code?
console.log(1 == ‘1’);
A) true
B) false
C) undefined
D) Error
Answer: A) true
Explanation: The == operator performs type coercion, so 1 and ‘1’ are considered equal.
Question 37
Which of the following is not a falsy value in JavaScript? A) false
B) 0
C) “”
D) “false”
Answer: D) “false”
Explanation: “false” is a non-empty string, and non-empty strings are truthy in JavaScript.
Question 38
What will be the output of the following code?
console.log(typeof ‘Hello’ === ‘string’);
A) true
B) false
C) “true”
D) “false”
Answer: A) true
Explanation: ‘Hello’ is a string, so typeof ‘Hello’ returns “string”, and the comparison is true.
Question 39
What does the isNaN() function do? A) It returns true if the argument is not a number.
B) It returns false if the argument is not a number.
C) It converts the argument to a number.
D) It checks if the argument is an integer.
Answer: A) It returns true if the argument is not a number.
Explanation: The isNaN() function determines whether a value is NaN (Not-a-Number).
Question 40
What will the following code output?
console.log(‘2’ + 2);
A) “4”
B) “22”
C) 4
D) NaN
Answer: B) “22”
Explanation: The + operator concatenates the string ‘2’ with the number 2, resulting in the string “22”.
Question 41
Which method is used to return the primitive value of a string object? A) valueOf()
B) toString()
C) charAt()
D) concat()
Answer: A) valueOf()
Explanation: The valueOf() method returns the primitive value of a String object.
Question 42
What will be the output of the following code?
console.log([1, 2, 3].reverse());
A) [1, 2, 3]
B) [3, 2, 1]
C) “321”
D) undefined
Answer: B) [3, 2, 1]
Explanation: The reverse() method reverses the elements of an array in place and returns the array.
Question 43
What is the output of the following code?
console.log(typeof undefined);
A) “null”
B) “undefined”
C) “object”
D) “number”
Answer: B) “undefined”
Explanation: typeof undefined returns “undefined” as undefined is a primitive value in JavaScript.
Question 44
Which of the following is not a JavaScript framework? A) Angular
B) React
C) Vue
D) Django
Answer: D) Django
Explanation: Django is a Python-based web framework, not a JavaScript framework.
Question 45
What will the following code output?
console.log(1 / 0);
A) NaN
B) Infinity
C) 0
D) Error
Answer: B) Infinity
Explanation: In JavaScript, dividing by zero results in Infinity.
Question 46
Which method converts a JavaScript value to a JSON string? A) JSON.stringify()
B) JSON.parse()
C) JSON.convert()
D) JSON.encode()
Answer: A) JSON.stringify()
Explanation: JSON.stringify() converts a JavaScript value to a JSON string.
Question 47
What will be the output of the following code?
console.log(Boolean(0));
A) true
B) false
C) 0
D) undefined
Answer: B) false
Explanation: 0 is a falsy value in JavaScript, so Boolean(0) returns false.
Question 48
Which method is used to merge the properties of two or more objects together into the first object? A) Object.assign()
B) Object.merge()
C) Object.concat()
D) Object.combine()
Answer: A) Object.assign()
Explanation: The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object.
Question 49
What will be the output of the following code?
console.log(’10’ + 10 – 10);
A) 0
B) 10
C) 100
D) NaN
Answer: B) 10
Explanation: ’10’ + 10 results in “1010”, then “1010” – 10 converts “1010” to 1010 and performs the subtraction, resulting in 1000.
Question 50
Which method is used to split a string into an array of substrings? A) split()
B) slice()
C) substr()
D) toArray()
Answer: A) split()
Explanation: The split() method splits a String object into an array of strings by separating the string into substrings.
These questions cover a wide range of JavaScript concepts and help reinforce your understanding of the language. Happy coding!