MCQs Of Day 3: Data Types in JavaScript
1. What is the data type of null in JavaScript?
a) Object
b) Number
c) Null
d) Undefined
Answer: a) Object
Explanation: In JavaScript, null is considered an object, which is a historical bug in the language.
2. Which of the following is not a primitive data type in JavaScript?
a) String
b) Object
c) Boolean
d) Number
Answer: b) Object
Explanation: Objects are reference types, not primitive data types. Primitive data types include String, Number, Boolean, Undefined, Null, and Symbol.
3. What type of value is returned by the typeof operator when used on a number?
a) string
b) number
c) boolean
d) object
Answer: b) number
Explanation: The typeof operator returns "number" when used on a numeric value.
4. Which of the following is a reference type?
a) Number
b) String
c) Object
d) Boolean
Answer: c) Object
Explanation: Objects are reference types, while other options are primitive types.
5. What will be the output of the following code?
javascriptCopy codelet a = "hello";let b = "world";console.log(a + b);a) hello world
b) hello
c) world
d) hello + world
Answer: a) hello world
Explanation: The + operator is used for string concatenation. Therefore, "hello" + "world" results in "hello world".
6. What is the type of typeof 5 in JavaScript?
a) string
b) number
c) undefined
d) object
Answer: a) string
Explanation: The typeof operator returns a string that represents the type of the operand, so typeof 5 returns "number" which is a string.
7. What does NaN stand for in JavaScript?
a) Not a Number
b) Not a Null
c) No a Number
d) None of the above
Answer: a) Not a Number
Explanation: NaN stands for "Not a Number" and is a special value representing an invalid or unrepresentable number result.
8. Which of the following methods converts a string to a number in JavaScript?
a) String()
b) Number()
c) Boolean()
d) parseInt()
Answer: b) Number()
Explanation: Number() is used to convert a string to a number.
9. What will be the output of the following code?
javascriptCopy codelet x = 10;let y = "20";console.log(x + y);a) 1020
b) 30
c) "1020"
d) 10 + 20
Answer: c) "1020"
Explanation: The + operator concatenates strings. In this case, 10 is converted to a string and concatenated with "20", resulting in "1020".
10. What is the correct syntax for defining an array in JavaScript?
a) let arr = (1, 2, 3);
b) let arr = {1, 2, 3};
c) let arr = [1, 2, 3];
d) let arr = <1, 2, 3>;
Answer: c) let arr = [1, 2, 3];
Explanation: Arrays are defined using square brackets [] in JavaScript.
11. Which of the following will return true for the expression typeof null?
a) "object"
b) "null"
c) "undefined"
d) "boolean"
Answer: a) "object"
Explanation: The typeof null returns "object" due to a historical bug in JavaScript.
12. What is the value of undefined == null?
a) true
b) false
c) NaN
d) undefined
Answer: a) true
Explanation: In JavaScript, undefined and null are loosely equal to each other, meaning undefined == null is true.
13. What is the correct way to declare a constant in JavaScript?
a) const name = "John";
b) constant name = "John";
c) let name = "John";
d) var name = "John";
Answer: a) const name = "John";
Explanation: The const keyword is used to declare a constant whose value cannot be changed.
14. Which of the following is true about the value of typeof for an array?
a) It returns "array".
b) It returns "object".
c) It returns "arraytype".
d) It returns "list".
Answer: b) It returns "object".
Explanation: Arrays are objects in JavaScript, so typeof returns "object" for arrays.
15. What will be the output of the following code?
javascriptCopy codelet obj = {};console.log(obj === {});a) true
b) false
c) undefined
d) null
Answer: b) false
Explanation: Even though both objects are empty, they are different objects in memory, so the comparison returns false.
16. Which of the following is NOT a falsy value in JavaScript?
a) 0
b) NaN
c) false
d) "hello"
Answer: d) "hello"
Explanation: The string "hello" is truthy. The other values (0, NaN, false) are falsy.
17. What is the output of the following code?
javascriptCopy codelet num = "5";console.log(num * 2);a) "10"
b) 10
c) NaN
d) "NaN"
Answer: b) 10
Explanation: JavaScript automatically converts the string "5" to a number during the multiplication operation, resulting in 10.
18. What is the output of typeof "5"?
a) number
b) string
c) boolean
d) undefined
Answer: b) string
Explanation: The typeof operator returns "string" for string values.
19. Which of the following is the correct way to create a function in JavaScript?
a) function = myFunction() {};
b) function myFunction() {};
c) create function myFunction() {};
d) function: myFunction() {};
Answer: b) function myFunction() {};
Explanation: The correct syntax to define a function in JavaScript is function functionName() {}.
20. What is the default value of an uninitialized variable?
a) undefined
b) null
c) NaN
d) false
Answer: a) undefined
Explanation: An uninitialized variable in JavaScript automatically has the value undefined.
21. Which of the following values will return false when checked with Boolean()?
a) 0
b) "hello"
c) true
d) 1
Answer: a) 0
Explanation: Boolean(0) returns false, as 0 is a falsy value.
22. Which of the following will return true for the expression typeof Symbol()?
a) "symbol"
b) "undefined"
c) "object"
d) "function"
Answer: a) "symbol"
Explanation: typeof Symbol() returns "symbol", as Symbol is a unique data type introduced in ES6.
23. What will be the output of typeof NaN?
a) "number"
b) "NaN"
c) "undefined"
d) "object"
Answer: a) "number"
Explanation: Even though NaN represents an invalid number, it is still considered a number type in JavaScript.
24. Which of the following is NOT a method to convert a string to a number?
a) parseInt()
b) parseFloat()
c) Number()
d) toString()
Answer: d) toString()
Explanation: toString() converts a number to a string, not the other way around.
25. What is the value of undefined === null?
a) true
b) false
c) NaN
d) undefined
Answer: b) false
Explanation: undefined and null are strictly not equal in JavaScript, so undefined === null returns false.
I'll continue to provide the next set of questions. Would you like to see more?
4o mini
You said:
give more
ChatGPT said:
ChatGPT
Here are additional 25 MCQs on JavaScript data types with explanations:
26. Which of the following data types is mutable in JavaScript?
a) String
b) Number
c) Array
d) Undefined
Answer: c) Array
Explanation: Arrays are reference types, meaning their contents can be changed. Strings and Numbers are primitive and immutable in JavaScript.
27. What is the result of typeof [] in JavaScript?
a) "array"
b) "object"
c) "arraytype"
d) "undefined"
Answer: b) "object"
Explanation: In JavaScript, arrays are technically objects, so typeof [] returns "object".
28. What does NaN === NaN return in JavaScript?
a) true
b) false
c) NaN
d) undefined
Answer: b) false
Explanation: NaN is not equal to any value, including itself. Therefore, NaN === NaN returns false.
29. What is the value of typeof function() {}?
a) "function"
b) "object"
c) "undefined"
d) "boolean"
Answer: a) "function"
Explanation: Functions in JavaScript are of the type "function", distinct from objects.
30. Which of the following is NOT a falsy value in JavaScript?
a) undefined
b) false
c) ""
d) "false"
Answer: d) "false"
Explanation: The string "false" is a truthy value because all non-empty strings are truthy, whereas undefined, false, and "" (empty string) are falsy values.
31. What will the following code output?
javascriptCopy codelet x = null;console.log(typeof x);a) "null"
b) "object"
c) "undefined"
d) "boolean"
Answer: b) "object"
Explanation: Although null represents the absence of a value, the typeof operator returns "object" for null due to a bug in JavaScript's implementation.
32. Which of the following methods is used to convert a string into a floating-point number?
a) parseFloat()
b) parseInt()
c) Number()
d) Boolean()
Answer: a) parseFloat()
Explanation: parseFloat() converts a string into a floating-point number.
33. What is the output of the following code?
javascriptCopy codelet y = 5;console.log(y === "5");a) true
b) false
c) undefined
d) NaN
Answer: b) false
Explanation: The === operator checks for strict equality, so it checks both value and type. Since y is a number and "5" is a string, they are not strictly equal.
34. What will be the output of the following code?
javascriptCopy codeconsole.log(5 + "5");a) 55
b) 10
c) "55"
d) NaN
Answer: c) "55"
Explanation: The + operator concatenates strings. When you add a number (5) to a string ("5"), JavaScript converts the number to a string, resulting in the concatenation "55".
35. Which of the following represents a Boolean data type in JavaScript?
a) 1
b) "true"
c) true
d) "false"
Answer: c) true
Explanation: In JavaScript, true and false are Boolean values, representing truth and falsity, respectively.
36. What is the value of typeof "123"?
a) "number"
b) "string"
c) "boolean"
d) "undefined"
Answer: b) "string"
Explanation: typeof returns "string" for string values, regardless of the content inside the string.
37. What is the output of typeof (typeof 5)?
a) "string"
b) "number"
c) "boolean"
d) "undefined"
Answer: a) "string"
Explanation: typeof 5 returns "number", and then typeof "number" returns "string".
38. Which of the following methods is used to convert a string to an integer in JavaScript?
a) parseInt()
b) parseFloat()
c) Number()
d) toString()
Answer: a) parseInt()
Explanation: parseInt() is used to convert a string to an integer.
39. What is the result of typeof [] in JavaScript?
a) "object"
b) "array"
c) "arraytype"
d) "list"
Answer: a) "object"
Explanation: In JavaScript, arrays are considered objects, so typeof [] returns "object".
40. Which of the following is not a primitive data type in JavaScript?
a) Number
b) String
c) Object
d) Symbol
Answer: c) Object
Explanation: Objects are reference types, not primitive types. The primitive data types include Number, String, Boolean, Null, Undefined, and Symbol.
41. What will be the output of the following code?
javascriptCopy codelet str = "hello";console.log(str[0]);a) "h"
b) "hello"
c) undefined
d) "e"
Answer: a) "h"
Explanation: Strings in JavaScript are zero-indexed, so str[0] refers to the first character of the string, "h".
42. What is the type of NaN in JavaScript?
a) "number"
b) "undefined"
c) "object"
d) "NaN"
Answer: a) "number"
Explanation: Even though NaN stands for "Not a Number", it is technically of the type number in JavaScript.
43. What is the result of typeof null?
a) "object"
b) "null"
c) "undefined"
d) "NaN"
Answer: a) "object"
Explanation: This is a historical bug in JavaScript where typeof null returns "object" instead of "null".
44. Which of the following methods can be used to convert a string to a number?
a) parseInt()
b) parseFloat()
c) Number()
d) All of the above
Answer: d) All of the above
Explanation: All the listed methods (parseInt(), parseFloat(), and Number()) can be used to convert a string to a number.
45. What is the result of typeof {}?
a) "object"
b) "null"
c) "objecttype"
d) "function"
Answer: a) "object"
Explanation: An object is a reference type, and typeof {} returns "object".
46. What is the result of typeof "false" in JavaScript?
a) "boolean"
b) "string"
c) "number"
d) "undefined"
Answer: b) "string"
Explanation: The value "false" is a string, so typeof "false" returns "string".
47. What does undefined mean in JavaScript?
a) Variable declared but not assigned any value
b) A type of object
c) A boolean value
d) A special object used in arrays
Answer: a) Variable declared but not assigned any value
Explanation: undefined indicates that a variable has been declared but has not yet been assigned a value.
48. What is the result of typeof Symbol()?
a) "object"
b) "symbol"
c) "undefined"
d) "function"
Answer: b) "symbol"
Explanation: Symbol() is a unique data type, and typeof Symbol() returns "symbol".
49. Which of the following is NOT a falsy value in JavaScript?
a) ""
b) 0
c) "false"
d) null
Answer: c) "false"
Explanation: "false" (a non-empty string) is a truthy value, while "", 0, and null are falsy values.
50. What is the type of undefined in JavaScript?
a) "undefined"
b) "null"
c) "object"
d) "undefinedtype"
Answer: a) "undefined"
Explanation: The typeof operator returns "undefined" for a variable that has not been assigned a value.
Would you like to continue with more questions or any other part of this concept?
4o mini
You said:
more
ChatGPT said:
ChatGPT
Here are 25 more MCQs on JavaScript data types with explanations:
51. What is the result of typeof 10 in JavaScript?
a) "number"
b) "string"
c) "undefined"
d) "object"
Answer: a) "number"
Explanation: The typeof operator returns "number" for numeric values.
52. Which of the following is a correct way to declare a variable that cannot be reassigned?
a) var a = 10;
b) const a = 10;
c) let a = 10;
d) const a = "hello"; let a = 10;
Answer: b) const a = 10;
Explanation: const is used to declare variables that cannot be reassigned after initialization.
53. Which of the following is NOT a valid way to create a new array in JavaScript?
a) let arr = [];
b) let arr = new Array();
c) let arr = Array();
d) let arr = new Array[];
Answer: d) let arr = new Array[];
Explanation: new Array[] is not a valid syntax. The correct ways are let arr = [], let arr = new Array(), or let arr = Array().
54. What will typeof {} return?
a) "object"
b) "null"
c) "array"
d) "undefined"
Answer: a) "object"
Explanation: In JavaScript, an object is a reference type, and typeof {} returns "object".
55. Which of the following values is a falsy value in JavaScript?
a) "0"
b) []
c) NaN
d) "hello"
Answer: c) NaN
Explanation: NaN (Not a Number) is considered a falsy value in JavaScript.
56. What does typeof null === "object" return in JavaScript?
a) true
b) false
c) null
d) TypeError
Answer: a) true
Explanation: This is a known quirk in JavaScript: typeof null returns "object".
57. Which of the following is a correct way to check if a variable is a string in JavaScript?
a) typeof variable === "string"
b) variable instanceof String
c) variable.toString()
d) All of the above
Answer: d) All of the above
Explanation: All the listed methods can be used to check if a variable is a string in JavaScript.
58. What is the output of typeof [1, 2, 3] in JavaScript?
a) "array"
b) "object"
c) "list"
d) "arraytype"
Answer: b) "object"
Explanation: In JavaScript, arrays are a special type of object, so typeof [1, 2, 3] returns "object".
59. Which of the following will convert a string to a number?
a) parseFloat("3.14")
b) parseInt("5")
c) Number("123")
d) All of the above
Answer: d) All of the above
Explanation: parseFloat(), parseInt(), and Number() can all convert strings to numbers.
60. What is the result of typeof Symbol() in JavaScript?
a) "symbol"
b) "object"
c) "undefined"
d) "function"
Answer: a) "symbol"
Explanation: The Symbol function creates a unique, immutable value, and typeof Symbol() returns "symbol".
61. What will be the output of the following code?
javascriptCopy codelet a = 0;console.log(Boolean(a));a) true
b) false
c) 0
d) NaN
Answer: b) false
Explanation: Boolean(0) returns false, since 0 is a falsy value in JavaScript.
62. What does the typeof operator return for an array in JavaScript?
a) "array"
b) "list"
c) "object"
d) "arraytype"
Answer: c) "object"
Explanation: Arrays are technically objects in JavaScript, so typeof will return "object".
63. What is the value of typeof "123" == typeof 123 in JavaScript?
a) true
b) false
c) undefined
d) TypeError
Answer: b) false
Explanation: "123" is a string, while 123 is a number. Therefore, their types are different.
64. Which of the following is a falsy value in JavaScript?
a) "false"
b) 1
c) null
d) "hello"
Answer: c) null
Explanation: null is a falsy value in JavaScript, whereas "false", 1, and "hello" are truthy values.
65. What will be the result of typeof undefined?
a) "undefined"
b) "null"
c) "object"
d) "undefinedtype"
Answer: a) "undefined"
Explanation: The typeof operator returns "undefined" when applied to a variable that hasn't been assigned a value.
66. What does Boolean([]) return in JavaScript?
a) true
b) false
c) undefined
d) NaN
Answer: a) true
Explanation: An empty array [] is considered truthy in JavaScript.
67. What is the output of typeof "5" - 5?
a) "0"
b) 0
c) NaN
d) "5"
Answer: b) 0
Explanation: "5" is a string, but when used in arithmetic operations like - 5, JavaScript coerces it to a number and performs the subtraction.
68. Which of the following data types is mutable?
a) String
b) Number
c) Array
d) Symbol
Answer: c) Array
Explanation: Arrays are mutable, meaning their contents can be modified. Primitive types like String and Number are immutable.
69. What does typeof typeof 1 return in JavaScript?
a) "number"
b) "string"
c) "boolean"
d) "undefined"
Answer: b) "string"
Explanation: typeof 1 returns "number", and typeof "number" returns "string".
70. What is the output of the following code?
javascriptCopy codelet num = "5" + 3;console.log(num);a) "53"
b) 8
c) "8"
d) NaN
Answer: a) "53"
Explanation: The + operator concatenates strings, so "5" + 3 results in the string "53".
71. Which of the following is a correct way to create a new object in JavaScript?
a) let obj = {};
b) let obj = new Object();
c) let obj = Object.create();
d) All of the above
Answer: d) All of the above
Explanation: All listed methods can be used to create a new object in JavaScript.
72. Which of the following is the proper way to declare a function in JavaScript?
a) function myFunction() {}
b) myFunction() => {}
c) def myFunction() {}
d) function = myFunction() {}
Answer: a) function myFunction() {}
Explanation: The correct syntax for declaring a function in JavaScript is function myFunction() {}.
73. Which of the following expressions is used to check whether a variable is a number in JavaScript?
a) Number.isFinite(variable)
b) typeof variable === "number"
c) !isNaN(variable)
d) All of the above
Answer: d) All of the above
Explanation: Each of the listed expressions can be used to check if a variable is a number.
74. What does Number("123") return in JavaScript?
a) 123
b) "123"
c) NaN
d) undefined
Answer: a) 123
Explanation: Number("123") converts the string "123" into the number 123.
75. Which of the following is an example of a primitive data type in JavaScript?
a) Object
b) Array
c) String
d) Function
Answer: c) String
Explanation: String is a primitive data type. Objects, Arrays, and Functions are reference types in JavaScript.
These 25 questions further explore JavaScript data types, variables, and type checking, offering a well-rounded set for learning and testing your understanding of JavaScript concepts.
4o mini
You said:
more
ChatGPT said:
ChatGPT
Here are 25 more MCQs on JavaScript data types, with explanations:
76. What will be the result of typeof NaN in JavaScript?
a) "undefined"
b) "object"
c) "number"
d) "NaN"
Answer: c) "number"
Explanation: Even though NaN stands for "Not a Number," it is of type "number" in JavaScript.
77. Which of the following is NOT a primitive data type in JavaScript?
a) String
b) Number
c) Object
d) Boolean
Answer: c) Object
Explanation: Objects are reference types, while String, Number, and Boolean are primitive types.
78. What is the result of typeof []?
a) "array"
b) "list"
c) "object"
d) "undefined"
Answer: c) "object"
Explanation: Arrays are technically objects in JavaScript, so typeof [] returns "object".
79. Which of the following values will return false when converted to a boolean in JavaScript?
a) "0"
b) 0
c) "false"
d) [] (empty array)
Answer: b) 0
Explanation: The number 0 is falsy in JavaScript. Other values like "0" and "false" are truthy.
80. What will the following code return?
javascriptCopy codelet a = null;console.log(typeof a);a) "null"
b) "object"
c) "undefined"
d) "none"
Answer: b) "object"
Explanation: This is a well-known JavaScript quirk. typeof null returns "object".
81. Which of the following data types are reference types in JavaScript?
a) String
b) Object
c) Number
d) Boolean
Answer: b) Object
Explanation: Objects, Arrays, and Functions are reference types, while String, Number, and Boolean are primitive types.
82. Which of the following would return true?
javascriptCopy codelet a = "5";let b = 5;a) a == b
b) a === b
c) typeof a === typeof b
d) None of the above
Answer: a) a == b
Explanation: == allows type coercion, so "5" and 5 are considered equal, while === would return false because it checks both value and type.
83. What is the result of typeof function() {} in JavaScript?
a) "object"
b) "function"
c) "undefined"
d) "null"
Answer: b) "function"
Explanation: Functions are a special type of object, and typeof function() {} returns "function".
84. Which of the following values is NOT falsy in JavaScript?
a) 0
b) "" (empty string)
c) NaN
d) "false"
Answer: d) "false"
Explanation: "false" is a non-empty string and is truthy. All the other values are falsy.
85. Which statement correctly creates an object in JavaScript?
a) let obj = {} b) let obj = []
c) let obj = Object.create();
d) let obj = new Object();
Answer: a) let obj = {}
Explanation: {} creates a new object. The other options are either for arrays or not proper object creation.
86. What does the isNaN() function do in JavaScript?
a) Converts a value to a number
b) Checks if a value is NaN
c) Checks if a value is a number
d) Converts a number to NaN
Answer: b) Checks if a value is NaN
Explanation: isNaN() checks if a value is NaN (Not a Number). It returns true if the value is NaN, and false otherwise.
87. Which of the following will check if a variable is null in JavaScript?
a) variable === null
b) typeof variable === "null"
c) variable == null
d) All of the above
Answer: a) variable === null
Explanation: === checks for both value and type, ensuring the variable is exactly null. == also works but is more permissive.
88. Which of the following is true about the typeof operator?
a) It returns "null" for null
b) It can check all data types
c) It always returns the correct data type
d) It cannot detect undefined variables
Answer: b) It can check all data types
Explanation: typeof checks a variable's type, but typeof null returns "object" as a known JavaScript quirk.
89. What is the result of typeof "false" in JavaScript?
a) "boolean"
b) "string"
c) "undefined"
d) "object"
Answer: b) "string"
Explanation: "false" is a string value, so typeof "false" returns "string".
90. What is the value of typeof (1 / "string")?
a) "number"
b) "undefined"
c) "NaN"
d) "string"
Answer: a) "number"
Explanation: Dividing a number by a non-numeric string results in NaN, which is a type of number.
91. Which of the following values is considered a "truthy" value in JavaScript?
a) ""
b) 0
c) false
d) "hello"
Answer: d) "hello"
Explanation: Any non-empty string, including "hello", is considered a truthy value in JavaScript.
92. What is the result of typeof [] === typeof {}?
a) true
b) false
c) undefined
d) TypeError
Answer: b) false
Explanation: typeof [] returns "object", and typeof {} also returns "object", but they are different types (array vs. general object), so they are not the same.
93. What is the default value of an uninitialized variable in JavaScript?
a) undefined
b) null
c) NaN
d) false
Answer: a) undefined
Explanation: When a variable is declared but not initialized, its default value is undefined.
94. Which of the following is the correct way to check if a variable is an array in JavaScript?
a) Array.isArray(variable)
b) typeof variable === "array"
c) variable instanceof Array
d) Both a and c
Answer: d) Both a and c
Explanation: Both Array.isArray(variable) and variable instanceof Array can be used to check if a variable is an array.
95. What is the output of Number("5.5")?
a) 5.5
b) "5.5"
c) NaN
d) 5
Answer: a) 5.5
Explanation: Number("5.5") converts the string "5.5" to the number 5.5.
96. Which of the following will return a Boolean value in JavaScript?
a) Boolean(0)
b) Boolean("false")
c) Boolean(null)
d) All of the above
Answer: d) All of the above
Explanation: All the options return a Boolean value: Boolean(0) is false, Boolean("false") is true, and Boolean(null) is false.
97. What is the result of typeof "123" - 1 in JavaScript?
a) 122
b) "122"
c) NaN
d) "1231"
Answer: a) 122
Explanation: "123" - 1 coerces the string to a number, resulting in 123 - 1, which equals 122.
98. Which operator is used for type checking in JavaScript?
a) ==
b) ===
c) typeof
d) instanceof
Answer: c) typeof
Explanation: typeof is used to check the data type of a variable in JavaScript.
99. What does parseInt("10px") return in JavaScript?
a) 10
b) "10px"
c) NaN
d) undefined
Answer: a) 10
Explanation: parseInt() converts the leading numeric characters of a string to an integer, so "10px" becomes 10.
100. Which of the following will create an object in JavaScript?
a) var obj = {};
b) var obj = new Object();
c) var obj = Object.create(null);
d) All of the above
Answer: d) All of the above
Explanation: All three methods create an empty object in JavaScript.
