MCQs On Day 2
JavaScript Syntax and Basic Output
1. Which of the following is used to declare a variable in JavaScript?
a) var
b) let
c) const
d) All of the above
Answer: d) All of the above
var, let, and const for declaring variables. var is used in older JavaScript, while let and const are more modern options.2. Which keyword is used to declare a constant variable in JavaScript?
a) let
b) const
c) var
d) constant
Answer: b) const
Explanation: const is used to declare a constant variable, which cannot be reassigned once its value is set.
3. What does the following code output?
let name = "John";console.log(name);a) John
b) "John"
c) name
d) Undefined
Answer: a) John
Explanation: The code declares a variable name and assigns it the value "John", which is then logged to the console.
4. Which of the following methods is used to display an alert box in JavaScript?
a) alert()
b) prompt()
c) console.log()
d) document.write()
Answer: a) alert()
Explanation: alert() is used to display a popup message on the browser window.
5. Which of the following is the correct syntax for creating a single-line comment in JavaScript?
a) <!-- This is a comment -->
b) /* This is a comment */
c) // This is a comment
d) ' This is a comment
Answer: c) // This is a comment
Explanation: Single-line comments in JavaScript are created using //.
6. Which of the following is used to print the output to the HTML document?
a) console.log()
b) alert()
c) document.write()
d) prompt()
Answer: c) document.write()
Explanation: document.write() is used to display content directly in the HTML document.
7. What will the following code output?
const x = 10;console.log(x);a) 10
b) x
c) "x"
d) Undefined
Answer: a) 10
Explanation: The constant x is assigned the value 10, and it is logged to the console.
8. Which of the following is NOT a valid way to declare a variable in JavaScript?
a) var age;
b) let name;
c) const value = 5;
d) variable price;
Answer: d) variable price;
Explanation: variable is not a valid keyword in JavaScript. You can use var, let, or const to declare variables.
9. What is the correct syntax to declare a variable using let in JavaScript?
a) let 10 = age;
b) let age;
c) let age = 10;
d) age let = 10;
Answer: c) let age = 10;
Explanation: The correct syntax for declaring a variable using let is let variableName = value;.
10. Which method is used to print a message to the browser's console?
a) alert()
b) document.write()
c) console.log()
d) print()
Answer: c) console.log()
Explanation: console.log() is used to output messages to the browser's console.
11. Which of the following is the correct way to declare a variable that cannot be reassigned?
a) var x = 5;
b) let x = 5;
c) const x = 5;
d) variable x = 5;
Answer: c) const x = 5;
Explanation: A const variable cannot be reassigned after it is initialized.
12. What will be the output of the following code?
let message = "Hello, World!";alert(message);a) Hello, World!
b) message
c) "Hello, World!"
d) Undefined
Answer: a) Hello, World!
Explanation: The alert() function displays the content of the message variable in a popup box.
13. Which of the following will display the current date and time in the browser's console?
a) console.log(Date.now());
b) console.log(new Date());
c) alert(Date());
d) console.log(currentDate);
Answer: b) console.log(new Date());
Explanation: new Date() will create a new Date object that contains the current date and time.
14. Which is the correct way to declare a variable with a string value in JavaScript?
a) let name = "John";
b) string name = "John";
c) var name => "John";
d) let name => John;
Answer: a) let name = "John";
Explanation: In JavaScript, a string value is declared using let or const, with the value enclosed in double or single quotes.
15. Which of the following is used to display multi-line comments in JavaScript?
a) // comment
b) # comment
c) /* comment */
d) -- comment --
Answer: c) /* comment */
Explanation: Multi-line comments in JavaScript are enclosed with /* and */.
16. What is the output of the following code?
var x = 5;var y = 10;console.log(x + y);a) 15
b) 510
c) x + y
d) undefined
Answer: a) 15
Explanation: The addition of x and y results in 15, which is then logged to the console.
17. Which method is commonly used to stop execution of JavaScript and view the values of variables?
a) alert()
b) document.write()
c) console.log()
d) break()
Answer: c) console.log()
Explanation: console.log() is commonly used to output values and debug JavaScript code.
18. Which of the following is true about let and const?
a) let allows you to reassign the value, while const does not.
b) Both let and const allow reassignment of values.
c) let and const are used interchangeably.
d) const allows reassignment, while let does not.
Answer: a) let allows you to reassign the value, while const does not.
Explanation: let allows you to reassign the value, while const makes the variable's value constant after initialization.
19. What will the following code output?
const name = "Alice";name = "Bob";console.log(name);a) Alice
b) Bob
c) Error
d) undefined
Answer: c) Error
Explanation: const variables cannot be reassigned, so attempting to assign a new value to name results in an error.
20. What does the following code do?
let x = 5;let y = 10;console.log(x * y);a) Adds x and y
b) Multiplies x and y
c) Divides x by y
d) Displays the value of x
Answer: b) Multiplies x and y
Explanation: The code multiplies the values of x and y, resulting in 50.
21. Which of the following is the correct way to declare 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: a) let arr = [1, 2, 3];
Explanation: Arrays are declared using square brackets [] and values are separated by commas.
22. How do you add a comment that spans multiple lines in JavaScript?
a) // This is a comment
b) <!-- This is a comment -->
c) /* This is a comment */
d) -- This is a comment --
Answer: c) /* This is a comment */
Explanation: Multi-line comments are enclosed in /* and */.
23. Which of the following is used to display a prompt dialog box in JavaScript?
a) prompt()
b) alert()
c) confirm()
d) console.log()
Answer: a) prompt()
Explanation: The prompt() method is used to display a dialog box where the user can input a value.
24. What will the following code output?
console.log(2 + '2');a) 22
b) 4
c) NaN
d) 2 2
Answer: a) 22
Explanation: In JavaScript, adding a number to a string results in string concatenation. The number 2 is converted to a string, and "2" + "2" results in "22".
25. Which of the following operators is used to assign a value to a variable in JavaScript?
a) +
b) =
c) ==
d) ===
Answer: b) =
Explanation: The = operator is used for assignment in JavaScript.
26. Which of the following is used to check equality in JavaScript?
a) =
b) ==
c) ===
d) !=
Answer: b) ==
Explanation: The == operator checks if two values are equal, performing type coercion if necessary. For strict equality without type coercion, use ===.
27. What will the following code output?
let a = 5;let b = "5";console.log(a == b);a) true
b) false
c) "true"
d) NaN
Answer: a) true
Explanation: The == operator performs type coercion, so the number 5 and the string "5" are considered equal.
28. Which method would you use to print a message in the browser's developer console?
a) document.write()
b) alert()
c) console.log()
d) print()
Answer: c) console.log()
Explanation: console.log() is used to print messages to the developer console in a web browser.
29. Which of the following variables cannot be reassigned?
a) var
b) let
c) const
d) all of the above
Answer: c) const
Explanation: Variables declared with const cannot be reassigned. let and var allow reassignment.
30. What will the following code output?
let num = 10;num++;console.log(num);a) 10
b) 11
c) undefined
d) NaN
Answer: b) 11
Explanation: The num++ operator increments the value of num by 1, so the output is 11.
31. Which of the following is the correct syntax for a multi-line comment in JavaScript?
a) // This is a comment
b) <!-- This is a comment -->
c) /* This is a comment */
d) -- This is a comment --
Answer: c) /* This is a comment */
Explanation: Multi-line comments are enclosed between /* and */.
32. Which of the following is used to check if two variables are strictly equal in JavaScript?
a) ==
b) ===
c) =
d) !==
Answer: b) ===
Explanation: The === operator checks if two values are strictly equal, without type coercion.
33. What will the following code output?
let a = "Hello";let b = "World";console.log(a + " " + b);a) HelloWorld
b) Hello World
c) "Hello" "World"
d) "Hello World"
Answer: b) Hello World
Explanation: The code concatenates the values of a and b with a space in between.
34. What does console.log() do in JavaScript?
a) It displays a dialog box
b) It writes to the web page
c) It prints output to the console
d) It assigns a value to a variable
Answer: c) It prints output to the console
Explanation: console.log() is used to print output to the browser's developer console.
35. Which keyword is used to declare a block-scoped variable in JavaScript?
a) var
b) let
c) const
d) Both b and c
Answer: d) Both b and c
Explanation: Both let and const declare block-scoped variables in JavaScript, meaning they are limited to the block in which they are defined.
36. What will the following code output?
const x = 10;x = 20;console.log(x);a) 10
b) 20
c) Error
d) Undefined
Answer: c) Error
Explanation: You cannot reassign a value to a const variable once it has been initialized.
37. What is the correct syntax for declaring a variable in JavaScript that can be reassigned?
a) let x = 10;
b) const x = 10;
c) var x = 10;
d) all of the above
Answer: a) let x = 10;
Explanation: Both let and var allow reassigning values, but const does not.
38. Which of the following outputs the string "Hello, JavaScript!" to the console?
a) console.print("Hello, JavaScript!");
b) print("Hello, JavaScript!");
c) console.log("Hello, JavaScript!");
d) alert("Hello, JavaScript!");
Answer: c) console.log("Hello, JavaScript!");
Explanation: console.log() is used to print to the console.
39. Which of the following operators is used to compare both the value and type of two operands in JavaScript?
a) ==
b) !=
c) ===
d) <>
Answer: c) ===
Explanation: The === operator compares both value and type without type coercion.
40. What will the following code output?
let message = "JavaScript";console.log(message.length);a) 10
b) 11
c) "JavaScript"
d) undefined
Answer: b) 11
Explanation: The length property returns the number of characters in the string.
41. Which of the following is the correct way to declare a constant variable in JavaScript?
a) let x = 10;
b) const x = 10;
c) variable x = 10;
d) var x = 10;
Answer: b) const x = 10;
Explanation: const is used to declare a constant variable.
42. What will the following code output?
let a = 2;let b = "2";console.log(a === b);a) true
b) false
c) "true"
d) "false"
Answer: b) false
Explanation: The === operator checks both the value and type, so 2 (number) is not strictly equal to "2" (string).
43. Which of the following is the correct syntax for declaring a function in JavaScript?
a) function myFunction() {}
b) function = myFunction() {}
c) function: myFunction() {}
d) def myFunction() {}
Answer: a) function myFunction() {}
Explanation: The correct syntax to declare a function is function functionName() {}.
44. Which of the following is used to check whether a variable is an array in JavaScript?
a) isArray()
b) Array.isArray()
c) typeof()
d) instanceof Array
Answer: b) Array.isArray()
Explanation: Array.isArray() is used to check if a value is an array.
45. Which of the following is used to stop code execution and view values for debugging in JavaScript?
a) alert()
b) document.write()
c) console.log()
d) break()
Answer: c) console.log()
Explanation: console.log() is used to output values for debugging purposes.
46. What will the following code output?
let a = 10;let b = 5;let result = a - b;console.log(result);a) 10
b) 5
c) 15
d) 50
Answer: b) 5
Explanation: The code subtracts b from a, resulting in 5.
47. Which of the following will correctly declare an empty array in JavaScript?
a) let arr = ();
b) let arr = {};
c) let arr = [];
d) let arr = new array();
Answer: c) let arr = [];
Explanation: An empty array is declared using square brackets [].
48. Which of the following is used to prompt the user for input in JavaScript?
a) prompt()
b) alert()
c) console.log()
d) confirm()
Answer: a) prompt()
Explanation: The prompt() method is used to display a dialog box that prompts the user for input.
49. Which of the following is used to create a block-scoped variable in JavaScript?
a) var
b) let
c) const
d) both b and c
Answer: d) both b and c
Explanation: Both let and const are used to create block-scoped variables in JavaScript.
50. What does the document.write() method do in JavaScript?
a) Writes to the browser's console
b) Writes text to the webpage
c) Writes data to a file
d) Writes data to the network
Answer: b) Writes text to the webpage
Explanation: document.write() writes directly to the webpage's HTML document.
51. Which of the following is used to create a constant variable in JavaScript?
a) var
b) let
c) const
d) both a and b
Answer: c) const
Explanation: const is used to declare a constant variable in JavaScript.
52. What will the following code output?
let str = "JavaScript";console.log(str[4]);a) J
b) a
c) v
d) S
Answer: c) v
Explanation: In JavaScript, string indexing starts from 0, so str[4] gives the 5th character of the string, which is "v".
53. Which of the following operators is used for assignment in JavaScript?
a) ==
b) ===
c) =
d) +=
Answer: c) =
Explanation: The = operator is used for assignment in JavaScript.
54. What does the following code output?
let x = "5";let y = 5;console.log(x == y);a) true
b) false
c) undefined
d) NaN
Answer: a) true
Explanation: The == operator performs type coercion, so the string "5" is equal to the number 5.
55. What is the correct syntax for a single-line comment in JavaScript?
a) <!-- comment -->
b) // comment
c) /* comment */
d) -- comment --
Answer: b) // comment
Explanation: Single-line comments are written with //.
56. Which of the following is used to declare a block-scoped variable that cannot be reassigned?
a) var
b) let
c) const
d) both b and c
Answer: c) const
Explanation: const is used to declare a constant variable, which cannot be reassigned.
57. What is the output of the following code?
let a = "5";let b = "5";console.log(a === b);a) true
b) false
c) undefined
d) NaN
Answer: a) true
Explanation: The === operator checks for strict equality, and both values are of the same type (string) and value.
58. What will the following code output?
let num = 10;num++;console.log(num);a) 11
b) 10
c) 1
d) NaN
Answer: a) 11
Explanation: The num++ expression increments the value of num by 1, so the output will be 11.
59. Which of the following methods is used to display an alert box in JavaScript?
a) prompt()
b) console.log()
c) alert()
d) confirm()
Answer: c) alert()
Explanation: The alert() method displays an alert dialog box with a specified message.
60. What will the following code output?
let x = 5;let y = 10;console.log(x + y);a) 5
b) 10
c) 15
d) "x + y"
Answer: c) 15
Explanation: The code adds the two numbers x and y, resulting in 15.
61. What is the default value of a variable declared with let in JavaScript if not initialized?
a) null
b) undefined
c) 0
d) NaN
Answer: b) undefined
Explanation: If a variable is declared with let but not initialized, it is assigned the default value of undefined.
62. What will the following code output?
javascriptCopy codeconst a = 5;const b = "5";console.log(a == b);a) true
b) false
c) NaN
d) Error
Answer: a) true
Explanation: The == operator performs type coercion, so the number 5 and the string "5" are considered equal.
63. Which of the following is used to check if an object is an array in JavaScript?
a) isArray()
b) Array.isArray()
c) arrayCheck()
d) typeOfArray()
Answer: b) Array.isArray()
Explanation: Array.isArray() is used to check if a given value is an array.
64. Which of the following is used to declare a variable in JavaScript that is hoisted to the top of its scope?
a) var
b) let
c) const
d) all of the above
Answer: a) var
Explanation: Variables declared with var are hoisted to the top of their scope, meaning they can be accessed before their declaration.
65. What is the output of the following code?
let x = 10;x += 5;console.log(x);a) 5
b) 10
c) 15
d) 50
Answer: c) 15
Explanation: The += operator adds the right operand (5) to the left operand (10), so the result is 15.
66. Which method is used to round a number to the nearest integer in JavaScript?
a) Math.floor()
b) Math.round()
c) Math.ceil()
d) Math.random()
Answer: b) Math.round()
Explanation: The Math.round() method rounds a number to the nearest integer.
67. Which method would you use to find the length of a string in JavaScript?
a) length()
b) str.length
c) string.length()
d) getLength()
Answer: b) str.length
Explanation: The length property is used to get the number of characters in a string.
68. Which of the following is not a valid variable declaration in JavaScript?
a) var myVar;
b) let myVar;
c) const myVar;
d) variable myVar;
Answer: d) variable myVar;
Explanation: The correct keywords for declaring variables are var, let, and const, not variable.
69. What is the correct syntax for calling a function named greet in JavaScript?
a) greet();
b) greet{};
c) call greet();
d) greet[];
Answer: a) greet();
Explanation: The correct syntax for calling a function is functionName();.
70. What does the following code output?
let name = "Alice";console.log(`Hello, ${name}`);a) Hello
b) Hello, Alice
c) "Hello, Alice"
d) Hello {name}
Answer: b) Hello, Alice
Explanation: Template literals (using ${}) allow for embedding expressions inside string literals.
71. What will the following code output?
let a = "5";let b = 5;console.log(a + b);a) 10
b) 55
c) "55"
d) NaN
Answer: c) "55"
Explanation: Since a is a string and b is a number, JavaScript performs string concatenation rather than addition, resulting in "55".
72. What does the typeof operator return?
a) The data type of a variable
b) The value of a variable
c) The length of a variable
d) None of the above
Answer: a) The data type of a variable
Explanation: The typeof operator is used to return the type of a given variable.
73. What will the following code output?
let arr = [1, 2, 3];arr.push(4);console.log(arr);a) [1, 2, 3]
b) [1, 2, 3, 4]
c) 1, 2, 3, 4
d) Error
Answer: b) [1, 2, 3, 4]
Explanation: The push() method adds a new element (4) to the end of the array.
74. What is the correct syntax to create a function in JavaScript?
a) function = myFunction() {}
b) function myFunction() {}
c) function: myFunction() {}
d) function => myFunction() {}
Answer: b) function myFunction() {}
Explanation: The correct syntax to declare a function is function functionName() {}.
75. What will the following code output?
let x = "hello";console.log(x[0]);a) h
b) e
c) l
d) o
Answer: a) h
Explanation: String indexing starts at 0, so x[0] gives the first character, "h".
76. Which of the following is used to execute a block of code if a condition is true in JavaScript?
a) if statement
b) for loop
c) while loop
d) switch statement
Answer: a) if statement
Explanation: The if statement is used to execute a block of code if a specified condition is true.
77. What will the following code output?
let num = 100;console.log(num / 0);a) Infinity
b) NaN
c) 0
d) Error
Answer: a) Infinity
Explanation: Dividing a number by zero results in Infinity in JavaScript.
78. Which of the following methods is used to add an element to the end of an array in JavaScript?
a) push()
b) pop()
c) shift()
d) unshift()
Answer: a) push()
Explanation: The push() method is used to add an element to the end of an array.
79. What is the correct syntax for a multi-line comment in JavaScript?
a) // comment
b) /* comment */
c) <!-- comment -->
d) -- comment --
Answer: b) /* comment */
Explanation: Multi-line comments are written with /* comment */.
80. What will the following code output?
let x = 5;let y = 2;console.log(x % y);a) 2
b) 1
c) 5
d) 7
Answer: b) 1
Explanation: The modulus operator (%) returns the remainder of dividing x by y, which is 1 (5 % 2 = 1).
81. What does the confirm() method do in JavaScript?
a) Returns a user prompt
b) Displays a dialog box with OK and Cancel buttons
c) Displays an alert box
d) Accepts a boolean value as input
Answer: b) Displays a dialog box with OK and Cancel buttons
Explanation: The confirm() method displays a dialog box with OK and Cancel buttons, returning true if OK is clicked and false if Cancel is clicked.
82. Which of the following statements is correct about let and const in JavaScript?
a) Variables declared with let can be reassigned, but variables declared with const cannot be reassigned.
b) Both let and const can be reassigned.
c) let variables are hoisted, while const variables are not.
d) Variables declared with let cannot be reassigned.
Answer: a) Variables declared with let can be reassigned, but variables declared with const cannot be reassigned.
Explanation: Variables declared with let can be reassigned, but variables declared with const are constant and cannot be reassigned.
83. What will the following code output?
let greeting = "Hello, ";greeting += "world!";console.log(greeting);a) Hello,
b) Hello, world!
c) world!
d) "Hello, world!"
Answer: b) Hello, world!
Explanation: The += operator appends the string "world!" to the existing greeting string.
84. Which of the following statements is correct about null in JavaScript?
a) null is a number.
b) null is a boolean.
c) null is an object type.
d) null is undefined.
Answer: c) null is an object type.
Explanation: In JavaScript, null is considered an object type, which is a historical bug in the language.
85. Which method is used to remove the last element of an array in JavaScript?
a) pop()
b) push()
c) shift()
d) unshift()
Answer: a) pop()
Explanation: The pop() method removes the last element from an array.
86. What will the following code output?
let a = 3;let b = "3";console.log(a === b);a) true
b) false
c) undefined
d) NaN
Answer: b) false
Explanation: The === operator checks both value and type, and 3 (number) is not strictly equal to "3" (string).
87. Which method is used to join all elements of an array into a string in JavaScript?
a) join()
b) split()
c) push()
d) concat()
Answer: a) join()
Explanation: The join() method joins all elements of an array into a single string.
88. Which operator is used to compare both value and type in JavaScript?
a) ==
b) ===
c) !=
d) =
Answer: b) ===
Explanation: The === operator checks both the value and type for equality.
89. What will the following code output?
let x = 5;console.log(x--);console.log(x);a) 5 4
b) 5 5
c) 4 5
d) 4 4
Answer: a) 5 4
Explanation: The x-- operator prints the current value (5), and then x is decremented to 4.
90. What will the following code output?
let num = 10;num += "20";console.log(num);a) 30
b) "1020"
c) 10
d) "20"
Answer: b) "1020"
Explanation: Since num is a number and the other operand is a string, JavaScript performs string concatenation, resulting in "1020".
91. Which of the following methods is used to convert a string to a number in JavaScript?
a) Number()
b) parseInt()
c) parseFloat()
d) All of the above
Answer: d) All of the above
Explanation: All of these methods (Number(), parseInt(), parseFloat()) can convert a string to a number, but with different behaviors regarding the type and precision.
92. What is the correct syntax for a ternary operator in JavaScript?
a) condition ? expression1 : expression2
b) condition ? expression1 : expression1;
c) if(condition) { expression1 } else { expression2 }
d) expression1 ? condition : expression2
Answer: a) condition ? expression1 : expression2
Explanation: The ternary operator evaluates a condition and returns expression1 if true or expression2 if false.
93. Which method is used to find the smallest integer greater than or equal to a given number in JavaScript?
a) Math.floor()
b) Math.round()
c) Math.ceil()
d) Math.abs()
Answer: c) Math.ceil()
Explanation: The Math.ceil() method returns the smallest integer greater than or equal to a given number.
94. What will the following code output?
let name = "John";console.log(name.length);a) John
b) 4
c) "John"
d) NaN
Answer: b) 4
Explanation: The length property of a string returns the number of characters in the string, which is 4 for "John".
95. What is the output of this expression?
5 + "5" - 5a) 50
b) 10
c) 5
d) NaN
Answer: b) 10
Explanation: The + operator performs string concatenation, resulting in "55", and then the - operator converts the string back to a number and subtracts 5, resulting in 10.
96. Which of the following is not a valid JavaScript data type?
a) String
b) Boolean
c) Integer
d) Object
Answer: c) Integer
Explanation: In JavaScript, there is no specific Integer data type; numbers are represented as floating-point values.
97. What is the default value of a variable declared with let in JavaScript if not initialized?
a) undefined
b) null
c) 0
d) NaN
Answer: a) undefined
Explanation: A variable declared with let is automatically initialized with the value undefined if no value is assigned.
98. Which of the following methods is used to check the data type of a variable in JavaScript?
a) type()
b) typeof()
c) instanceOf()
d) dataType()
Answer: b) typeof()
Explanation: The typeof operator returns a string indicating the type of a variable.
99. Which of the following will not cause an error in JavaScript?
a) let 3x = 10;
b) let x-3 = 10;
c) let $x = 10;
d) let x#3 = 10;
Answer: c) let $x = 10;
Explanation: In JavaScript, variables can begin with a $, so let $x = 10; is a valid declaration.
100. What is the purpose of the debugger statement in JavaScript?
a) To pause code execution and start the debugger
b) To display a debugging message
c) To remove a breakpoint
d) To log the value of a variable
Answer: a) To pause code execution and start the debugger
Explanation: The debugger statement is used to pause code execution at that point and activate the browser's debugger if available.
