MCQs of Day 4: Operators in JavaScript
1. What is the result of the expression 5 + 3 in JavaScript?
A) 53
B) 8
C) 5+3
D) 0
Answer: B) 8
Explanation: + is the addition operator, so 5 + 3 results in 8.
2. Which operator is used to check equality of both value and type in JavaScript?
A) ==
B) ===
C) !=
D) !==
Answer: B) ===
Explanation: === checks both value and type equality, whereas == only checks the value.
3. What does the operator !== do in JavaScript?
A) Compares values and types, returns true if they are different
B) Checks if two values are strictly equal
C) Checks if two values are equal
D) Checks for type equality only
Answer: A) Compares values and types, returns true if they are different
Explanation: !== checks if both the value and the type are different.
4. What is the result of the expression 3 * 2 in JavaScript?
A) 6
B) 32
C) NaN
D) 3*2
Answer: A) 6
Explanation: * is the multiplication operator, so 3 * 2 results in 6.
5. Which operator is used for modulo (remainder of division) in JavaScript?
A) /
B) *
C) %
D) //
Answer: C) %
Explanation: The % operator returns the remainder when one number is divided by another.
6. What is the result of the expression 10 / 2?
A) 5
B) 12
C) 2
D) 10/2
Answer: A) 5
Explanation: The / operator divides the two numbers, so 10 / 2 results in 5.
7. Which operator is used to assign a value to a variable in JavaScript?
A) ==
B) :=
C) =
D) ===
Answer: C) =
Explanation: = is the assignment operator, used to assign a value to a variable.
8. What will be the result of the expression 10 > 5?
A) true
B) false
C) 1
D) 0
Answer: A) true
Explanation: 10 > 5 is a comparison that returns true because 10 is greater than 5.
9. What is the output of the expression '5' + 5?
A) 10
B) '55'
C) NaN
D) '5'5
Answer: B) '55'
Explanation: The + operator, when used with a string, performs concatenation. '5' + 5 results in '55'.
10. What does the && (AND) operator do in JavaScript?
A) Returns true if at least one condition is true
B) Returns true if both conditions are true
C) Checks if two values are equal
D) Inverts a boolean value
Answer: B) Returns true if both conditions are true
Explanation: && returns true only if both conditions on either side of it are true.
11. What does the || (OR) operator do in JavaScript?
A) Returns true if both conditions are true
B) Returns true if at least one condition is true
C) Compares the values of two variables
D) Inverts the boolean value of a condition
Answer: B) Returns true if at least one condition is true
Explanation: || returns true if either condition on either side of it is true.
12. What does the ! (NOT) operator do in JavaScript?
A) Reverses the boolean value
B) Returns true if both conditions are true
C) Inverts the value of two variables
D) Checks if a value is undefined
Answer: A) Reverses the boolean value
Explanation: ! inverts a boolean value: true becomes false and vice versa.
13. What does typeof return in JavaScript?
A) The type of a variable
B) The value of a variable
C) A boolean indicating whether the variable exists
D) The sum of a variable
Answer: A) The type of a variable
Explanation: typeof is used to check the type of a variable or expression.
14. What will the following code output? console.log(5 === '5')
A) true
B) false
C) 5
D) '5'
Answer: B) false
Explanation: === checks both the type and the value, so 5 (number) is not equal to '5' (string).
15. What is the output of console.log(10 % 3)?
A) 1
B) 3
C) 0
D) 10
Answer: A) 1
Explanation: 10 % 3 gives the remainder when 10 is divided by 3, which is 1.
16. Which of the following is not a valid arithmetic operator in JavaScript?
A) +
B) *
C) &&
D) /
Answer: C) &&
Explanation: && is a logical operator, not an arithmetic operator.
17. What is the result of 0 === false in JavaScript?
A) true
B) false
C) 0
D) undefined
Answer: B) false
Explanation: === checks both value and type, so 0 (number) is not equal to false (boolean).
18. What is the purpose of the += operator in JavaScript?
A) Adds and assigns the value
B) Subtracts and assigns the value
C) Multiplies and assigns the value
D) Divides and assigns the value
Answer: A) Adds and assigns the value
Explanation: The += operator adds the value to the variable and then assigns the result back to the variable.
19. What does the following code return? console.log(10 / '2')
A) 5
B) NaN
C) '5'
D) 10
Answer: A) 5
Explanation: JavaScript attempts to convert the string '2' to a number for the division, resulting in 5.
20. Which of these comparisons will return true?
A) 3 != 3
B) 3 == '3'
C) 3 === '3'
D) 3 !== 3
Answer: B) 3 == '3'
Explanation: == performs type coercion and compares values, so 3 == '3' is true. === does not perform type coercion, so 3 === '3' is false.
21. What is the result of the expression "abc" - 1?
A) NaN
B) 'abc'
C) -1
D) 'abc1'
Answer: A) NaN
Explanation: JavaScript cannot subtract from a string, so it returns NaN (Not-a-Number).
22. Which of these operators is used to concatenate strings in JavaScript?
A) +
B) -
C) *
D) /
Answer: A) +
Explanation: The + operator is used for both addition (numbers) and concatenation (strings).
23. What does the expression 5 != '5' return in JavaScript?
A) true
B) false
C) NaN
D) 5
Answer: A) true
Explanation: != compares values after type coercion, so 5 != '5' returns true.
24. Which operator is used for checking inequality in JavaScript?
A) !=
B) !==
C) <
D) >
Answer: A) !=
Explanation: != is used to check if two values are not equal.
25. Which operator checks if two values are strictly not equal (both value and type)?
A) !=
B) ===
C) !==
D) ==
Answer: C) !==
Explanation: !== checks whether both the value and the type are different.
26. Which of these will return false?
A) 5 === 5
B) '5' == 5
C) '5' === 5
D) 0 === false
Answer: C) '5' === 5
Explanation: === checks for both value and type, so '5' (string) is not strictly equal to 5 (number).
27. What does the expression 5 > 3 && 2 < 4 return?
A) true
B) false
C) NaN
D) undefined
Answer: A) true
Explanation: Both conditions are true, so the && operator returns true.
28. What is the result of true || false in JavaScript?
A) true
B) false
C) undefined
D) NaN
Answer: A) true
Explanation: The || operator returns true if either condition is true, so true || false returns true.
29. What does x++ do in JavaScript?
A) Decrements x by 1
B) Increments x by 1
C) Assigns 1 to x
D) Sets x to 1
Answer: B) Increments x by 1
Explanation: x++ is the post-increment operator, which increments x by 1 after its current value is used.
30. What is the purpose of the -- operator in JavaScript?
A) Increments a value by 1
B) Decrements a value by 1
C) Checks equality
D) Subtracts 1 from a value
Answer: B) Decrements a value by 1
Explanation: The -- operator decreases a variable's value by 1.
I hope these examples help in creating your set of MCQs! Would you like more to be added?
4o mini
You said:
yes
ChatGPT said:
ChatGPT
Here are more MCQs on the topic of Operators in JavaScript:
31. What will the following code output? console.log(2 + '2')
A) '4'
B) '22'
C) 4
D) NaN
Answer: B) '22'
Explanation: The + operator is used for both addition and string concatenation. Since one of the operands is a string, the number 2 is converted to a string, and they are concatenated to form '22'.
32. What will the expression 0 == false return?
A) true
B) false
C) 0
D) NaN
Answer: A) true
Explanation: The == operator performs type coercion, so 0 is considered equal to false.
33. What is the result of 5 / 0 in JavaScript?
A) Infinity
B) NaN
C) 0
D) Error
Answer: A) Infinity
Explanation: Dividing a number by 0 results in Infinity in JavaScript.
34. What does x -= 5 do in JavaScript?
A) Adds 5 to x
B) Subtracts 5 from x
C) Assigns x the value of 5
D) Divides x by 5
Answer: B) Subtracts 5 from x
Explanation: The -= operator subtracts the value from the variable and then assigns the result back to the variable.
35. What is the result of the expression 'true' == true?
A) true
B) false
C) NaN
D) 'true'
Answer: B) false
Explanation: 'true' is a string, and true is a boolean, so they are not equal when using ==.
36. What is the value of typeof null in JavaScript?
A) 'object'
B) 'null'
C) 'undefined'
D) 'boolean'
Answer: A) 'object'
Explanation: This is a quirk of JavaScript. typeof null returns 'object', even though null is not an object.
37. Which of these operators is used to perform a bitwise AND operation?
A) &
B) &&
C) ||
D) |
Answer: A) &
Explanation: & is used for the bitwise AND operation, which compares the binary representation of two values.
38. Which operator would you use to check whether a value is greater than or equal to another value?
A) <
B) <=
C) >=
D) ===
Answer: C) >=
Explanation: The >= operator checks if the left operand is greater than or equal to the right operand.
39. What is the result of typeof []?
A) 'array'
B) 'object'
C) 'undefined'
D) 'array object'
Answer: B) 'object'
Explanation: Arrays are technically objects in JavaScript, so typeof [] returns 'object'.
40. What does the === operator do in JavaScript?
A) Compares only the value of two variables
B) Compares both the value and the type of two variables
C) Assigns a value to a variable
D) Checks if two variables are unequal
Answer: B) Compares both the value and the type of two variables
Explanation: === checks for both value and type equality without performing type coercion.
41. Which of the following operators is used to assign the value of a variable after adding to it?
A) =
B) +=
C) -=
D) *=
Answer: B) +=
Explanation: The += operator adds a value to the variable and assigns the result to the variable.
42. What is the result of 2 === '2'?
A) true
B) false
C) NaN
D) undefined
Answer: B) false
Explanation: === checks both value and type. 2 (number) is not equal to '2' (string).
43. Which operator checks for inequality without type coercion in JavaScript?
A) ==
B) ===
C) !=
D) !==
Answer: D) !==
Explanation: !== checks both value and type inequality.
44. What is the output of console.log(5 + 3 + '2')?
A) 8
B) '82'
C) '52'
D) 10
Answer: B) '82'
Explanation: JavaScript first evaluates 5 + 3 (which gives 8), then concatenates the string '2', resulting in '82'.
45. What is the output of the expression '10' > 5?
A) true
B) false
C) NaN
D) 10
Answer: A) true
Explanation: The string '10' is coerced to the number 10 in the comparison, and 10 > 5 is true.
46. What is the result of 5 === 5?
A) true
B) false
C) 5
D) undefined
Answer: A) true
Explanation: Both the value and type are equal, so 5 === 5 is true.
47. Which operator is used to divide two numbers in JavaScript?
A) /
B) *
C) +
D) -
Answer: A) /
Explanation: The / operator is used to divide two numbers.
48. What is the result of 10 == '10'?
A) true
B) false
C) NaN
D) 10
Answer: A) true
Explanation: == performs type coercion, so '10' is converted to the number 10, and the comparison returns true.
49. What is the result of false + 1 in JavaScript?
A) true
B) 1
C) NaN
D) 0
Answer: B) 1
Explanation: false is coerced to 0, and 0 + 1 results in 1.
50. Which operator is used to negate a boolean value?
A) !
B) &&
C) ||
D) ==
Answer: A) !
Explanation: The ! operator negates the boolean value.
51. What will the following expression return? false && true
A) true
B) false
C) NaN
D) undefined
Answer: B) false
Explanation: && returns true only if both sides are true. Since false && true is not true, it returns false.
52. What is the result of 5 != 5 in JavaScript?
A) true
B) false
C) 0
D) NaN
Answer: B) false
Explanation: 5 != 5 compares the values and returns false because the values are equal.
53. What does x++ do in JavaScript?
A) Decrements x by 1
B) Increments x by 1 after the value is used
C) Increments x by 1 before the value is used
D) Assigns x the value of 1
Answer: B) Increments x by 1 after the value is used
Explanation: x++ is a post-increment operator, so it increases x by 1 after the current value is used in the expression.
54. What is the result of 2 > 1 || 3 < 4?
A) true
B) false
C) 2
D) undefined
Answer: A) true
Explanation: The || (OR) operator returns true if at least one condition is true. Since 2 > 1 is true, the expression returns true.
55. What is the result of typeof NaN in JavaScript?
A) 'number'
B) 'NaN'
C) 'undefined'
D) 'object'
Answer: A) 'number'
Explanation: NaN is of type number in JavaScript, despite its name indicating "Not-a-Number".
56. What is the result of null == undefined?
A) true
B) false
C) NaN
D) undefined
Answer: A) true
Explanation: == performs type coercion, and null and undefined are considered equal in this context.
57. What does the += operator do in JavaScript?
A) Adds the value on the left to the value on the right
B) Divides the value on the left by the value on the right
C) Subtracts the value on the right from the value on the left
D) Assigns the value on the right to the variable on the left
Answer: A) Adds the value on the left to the value on the right
Explanation: += adds the right-hand operand to the left-hand operand and assigns the result to the left operand.
Let me know if you'd like more!
4o mini
You said:
yes
ChatGPT said:
ChatGPT
Here are more MCQs on the topic of Operators in JavaScript:
58. What will the expression true || false return?
A) true
B) false
C) NaN
D) undefined
Answer: A) true
Explanation: The || (OR) operator returns true if at least one of the conditions is true. Since true is one of the operands, the result is true.
59. What is the result of 0 && 1 in JavaScript?
A) true
B) false
C) NaN
D) 0
Answer: B) false
Explanation: The && (AND) operator returns true only if both operands are true. Since 0 is falsy, the result is false.
60. What is the result of typeof "hello"?
A) 'string'
B) 'number'
C) 'undefined'
D) 'object'
Answer: A) 'string'
Explanation: The typeof operator returns the data type of the operand. A string, such as "hello", is of type 'string'.
61. What is the result of 2 ** 3 in JavaScript?
A) 5
B) 6
C) 8
D) 9
Answer: C) 8
Explanation: The ** operator is the exponentiation operator. 2 ** 3 means 2 raised to the power of 3, which equals 8.
62. What will 10 + '5' return?
A) '15'
B) 15
C) NaN
D) '105'
Answer: A) '15'
Explanation: The + operator between a number and a string performs string concatenation, resulting in '15' instead of a numerical sum.
63. What is the result of 10 != '10' in JavaScript?
A) true
B) false
C) NaN
D) undefined
Answer: B) false
Explanation: The != operator compares values without considering type, so '10' is coerced into a number, and 10 != 10 results in false.
64. What does the > operator do in JavaScript?
A) Greater than
B) Greater than or equal to
C) Less than
D) Not equal to
Answer: A) Greater than
Explanation: The > operator checks if the left operand is greater than the right operand.
65. Which of the following is NOT a comparison operator in JavaScript?
A) ==
B) ===
C) >
D) &
Answer: D) &
Explanation: & is a bitwise AND operator, not a comparison operator. The comparison operators are ==, ===, >, <, etc.
66. Which operator is used to check whether two values are strictly equal (both value and type)?
A) ==
B) ===
C) !=
D) !==
Answer: B) ===
Explanation: The === operator checks both value and type equality without type coercion.
67. What will x++ do in JavaScript?
A) Decrements x by 1
B) Increments x by 1 and returns the original value
C) Increments x by 1 and returns the new value
D) Assigns x the value of 1
Answer: B) Increments x by 1 and returns the original value
Explanation: x++ is the post-increment operator. It increments x but returns the original value of x before the increment.
68. What does 5 / '2' return in JavaScript?
A) 2
B) 2.5
C) NaN
D) Error
Answer: B) 2.5
Explanation: JavaScript performs type coercion, converting the string '2' to the number 2, and the result of 5 / 2 is 2.5.
69. What does the += operator do in JavaScript?
A) Adds the right operand to the left operand and assigns the result to the left operand
B) Subtracts the right operand from the left operand
C) Divides the left operand by the right operand
D) Multiplies the left operand by the right operand
Answer: A) Adds the right operand to the left operand and assigns the result to the left operand
Explanation: The += operator is shorthand for left operand = left operand + right operand.
70. What is the result of typeof [] in JavaScript?
A) 'array'
B) 'object'
C) 'undefined'
D) 'array object'
Answer: B) 'object'
Explanation: Arrays are technically a type of object in JavaScript, so typeof [] returns 'object'.
71. Which operator is used to perform a bitwise OR operation in JavaScript?
A) ||
B) &
C) |
D) &&
Answer: C) |
Explanation: The | operator is used for the bitwise OR operation, comparing binary representations of two values.
72. What is the result of 0 === '0'?
A) true
B) false
C) NaN
D) undefined
Answer: B) false
Explanation: === checks both value and type, and since 0 is a number and '0' is a string, they are not equal.
73. What will the expression 1 + 2 + '3' return?
A) '6'
B) 6
C) '23'
D) '3'
Answer: C) '23'
Explanation: The addition operator + first adds 1 + 2 (which results in 3), and then concatenates the string '3', resulting in '23'.
74. What is the value of 10 % 3 in JavaScript?
A) 1
B) 2
C) 3
D) 0
Answer: A) 1
Explanation: The % operator returns the remainder of a division. 10 % 3 equals 1 because 3 goes into 10 three times, leaving a remainder of 1.
75. Which operator is used to check for strict inequality?
A) !=
B) !==
C) =!
D) ===
Answer: B) !==
Explanation: The !== operator checks whether the values are not equal in both value and type.
76. What is the result of false || false in JavaScript?
A) true
B) false
C) NaN
D) undefined
Answer: B) false
Explanation: The || (OR) operator returns true only if at least one of the operands is true. Since both are false, the result is false.
77. What is the result of null + 5?
A) 'null5'
B) 5
C) 0
D) NaN
Answer: B) 5
Explanation: null is coerced to 0 when used with the + operator, so the result is 0 + 5 = 5.
78. Which operator performs logical negation in JavaScript?
A) !
B) &&
C) ||
D) ==
Answer: A) !
Explanation: The ! operator negates the boolean value of the operand. For example, !true returns false.
79. What is the result of '2' * 3?
A) '6'
B) 6
C) NaN
D) '23'
Answer: B) 6
Explanation: The * operator performs multiplication, and '2' is coerced to a number, resulting in 2 * 3 = 6.
80. What will 5 === '5' return?
A) true
B) false
C) NaN
D) undefined
Answer: B) false
Explanation: The === operator checks both value and type. 5 is a number, and '5' is a string, so they are not strictly equal.
81. What will 10 / 2 return?
A) 5
B) 2
C) NaN
D) 10
Answer: A) 5
Explanation: The / operator performs division. 10 / 2 equals 5.
82. What is the result of typeof NaN?
A) 'number'
B) 'NaN'
C) 'undefined'
D) 'object'
Answer: A) 'number'
Explanation: NaN stands for "Not-a-Number," but it is of type 'number' in JavaScript.
83. What is the result of 5 === 5?
A) true
B) false
C) NaN
D) undefined
Answer: A) true
Explanation: The === operator checks both value and type. Since both operands are numbers and equal, the result is true.
Let me know if you'd like more!
4o mini
You said:
yes
ChatGPT said:
ChatGPT
Here are more MCQs on the topic of Operators in JavaScript:
84. What will 5 > 3 return?
A) true
B) false
C) NaN
D) undefined
Answer: A) true
Explanation: The > operator checks if the left operand is greater than the right operand. Since 5 is greater than 3, the result is true.
85. What is the result of '5' - 2 in JavaScript?
A) '7'
B) 7
C) NaN
D) '52'
Answer: B) 7
Explanation: The - operator performs subtraction. JavaScript coerces the string '5' into the number 5, and 5 - 2 equals 7.
86. What is the result of 2 + '2'?
A) 4
B) '22'
C) NaN
D) 2
Answer: B) '22'
Explanation: The + operator, when used with a string, performs string concatenation. Therefore, 2 + '2' results in '22'.
87. What will x === 'x' return if x is a string with the value 'x'?
A) true
B) false
C) NaN
D) undefined
Answer: A) true
Explanation: The === operator checks both value and type. If x is a string with the value 'x', then x === 'x' will return true.
88. Which operator is used to perform a bitwise AND operation in JavaScript?
A) &&
B) |
C) &
D) ^
Answer: C) &
Explanation: The & operator performs a bitwise AND operation on two operands.
89. What is the result of false == 0?
A) true
B) false
C) NaN
D) undefined
Answer: A) true
Explanation: The == operator performs type coercion, and false is coerced to 0, so false == 0 returns true.
90. Which operator is used to check if two values are not equal in value or type?
A) !=
B) !==
C) ==
D) =
Answer: B) !==
Explanation: The !== operator checks both the value and the type for inequality. If either is different, it returns true.
91. What will 10 % 4 return?
A) 2
B) 4
C) 6
D) 1
Answer: D) 1
Explanation: The % (modulus) operator returns the remainder of the division. 10 % 4 is 2, leaving a remainder of 1.
92. What will '5' === 5 return?
A) true
B) false
C) NaN
D) undefined
Answer: B) false
Explanation: The === operator checks both value and type. Since '5' is a string and 5 is a number, they are not strictly equal.
93. What will 10 != '10' return?
A) true
B) false
C) NaN
D) undefined
Answer: B) false
Explanation: The != operator checks for inequality, and JavaScript performs type coercion. Since '10' is converted to 10, they are considered equal.
94. Which operator is used for strict equality comparison in JavaScript?
A) ==
B) ===
C) !=
D) !==
Answer: B) ===
Explanation: The === operator checks both value and type equality without performing type conversion.
95. What is the result of '10' + 5 in JavaScript?
A) '105'
B) 15
C) NaN
D) 5
Answer: A) '105'
Explanation: The + operator between a string and a number will convert the number to a string and concatenate the two, resulting in '105'.
96. What will the expression 5 && 0 return?
A) 5
B) 0
C) NaN
D) true
Answer: B) 0
Explanation: The && (AND) operator returns the first falsy value or the last operand if all are truthy. Since 0 is falsy, the result is 0.
97. What does typeof null return?
A) 'null'
B) 'object'
C) 'undefined'
D) 'NaN'
Answer: B) 'object'
Explanation: This is a historical bug in JavaScript. typeof null returns 'object' despite null not being an object.
98. What is the result of 3 * '4'?
A) '12'
B) 12
C) NaN
D) '34'
Answer: B) 12
Explanation: The * operator performs multiplication, and JavaScript coerces the string '4' into the number 4, resulting in 3 * 4 = 12.
99. What does the !== operator do in JavaScript?
A) Checks if values are strictly equal
B) Checks if values are equal without type coercion
C) Checks if values are not equal in both value and type
D) Checks if values are equal or not
Answer: C) Checks if values are not equal in both value and type
Explanation: The !== operator compares both the value and the type of two operands. If either is different, the result is true.
100. What will 5 + 5 + '5' return?
A) '10'
B) 15
C) '55'
D) 20
Answer: C) '55'
Explanation: JavaScript evaluates the expression from left to right. First, 5 + 5 results in 10, and then 10 + '5' results in the string '55'.
