JavaScript MCQ:-
- What does JavaScript use to indicate the start and end of a block of code?
- A) Curly braces
{ }
- B) Parentheses
( )
- C) Square brackets
[ ]
- D) Angle brackets
< >
- A) Curly braces
- Which of the following is the correct syntax to create a function in JavaScript?
- A)
function myFunction[] {}
- B)
function myFunction() {}
- C)
function myFunction() () {}
- D)
function myFunction{ }
- A)
- How do you declare a variable in JavaScript?
- A)
var variableName;
- B)
variableName = var;
- C)
variableName: var;
- D)
declare variableName;
- A)
- Which operator is used to assign a value to a variable in JavaScript?
- A)
==
- B)
!=
- C)
=
- D)
===
- A)
- How do you write 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
- A)
- Which of the following is not a valid JavaScript data type?
- A)
String
- B)
Number
- C)
Boolean
- D)
Character
- A)
- What will the following code output:
console.log(typeof 'Hello');
?- A)
string
- B)
String
- C)
text
- D)
char
- A)
- Which method is used to remove whitespace from both ends of a string?
- A)
trim()
- B)
strip()
- C)
remove()
- D)
cut()
- A)
- How can you convert a string to an integer in JavaScript?
- A)
parseInt()
- B)
parseString()
- C)
toInteger()
- D)
convert()
- A)
- What does the
push()
method do in JavaScript arrays?- A) Removes the last element
- B) Adds an element to the end
- C) Adds an element to the beginning
- D) Removes the first element
- Which keyword is used to create a new object in JavaScript?
- A)
object
- B)
new
- C)
create
- D)
this
- A)
- How do you check if two values are equal in JavaScript, considering type?
- A)
==
- B)
===
- C)
!=
- D)
!==
- A)
- Which function is used to parse a JSON string into an object in JavaScript?
- A)
JSON.parse()
- B)
JSON.stringify()
- C)
parseJSON()
- D)
stringifyJSON()
- A)
- What will
console.log(0.1 + 0.2 === 0.3);
output?- A)
true
- B)
false
- C)
undefined
- D)
NaN
- A)
- How can you access the first element of an array named
arr
?- A)
arr[0]
- B)
arr.first()
- C)
arr.begin()
- D)
arr.get(1)
- A)
- Which statement is used to exit a loop in JavaScript?
- A)
stop
- B)
exit
- C)
break
- D)
continue
- A)
- What will the following code output:
console.log(1 + '1');
?- A)
2
- B)
11
- C)
NaN
- D)
undefined
- A)
- How do you define a constant in JavaScript?
- A)
const variableName;
- B)
constant variableName;
- C)
let variableName;
- D)
const variableName = value;
- A)
- Which method is used to find the length of an array in JavaScript?
- A)
length()
- B)
size()
- C)
count()
- D)
length
- A)
- What does the
return
statement do in a function?- A) Exits the function
- B) Returns a value from the function
- C) Ends the loop
- D) Terminates the program
- How can you check if a variable
x
is an instance of a particular objectY
?- A)
x.instanceof(Y)
- B)
x.isInstance(Y)
- C)
x instanceof Y
- D)
instanceof(x, Y)
- A)
- What is the default value of an uninitialized variable in JavaScript?
- A)
0
- B)
null
- C)
undefined
- D)
NaN
- A)
- How do you create an empty object in JavaScript?
- A)
var obj = {};
- B)
var obj = [];
- C)
var obj = ();
- D)
var obj = new Object();
- A)
- Which method is used to add an item to the beginning of an array?
- A)
push()
- B)
pop()
- C)
shift()
- D)
unshift()
- A)
- How do you create a new array with elements added to an existing array?
- A)
arr.concat(newElements)
- B)
arr.add(newElements)
- C)
arr.merge(newElements)
- D)
arr.append(newElements)
- A)
- Which keyword is used to define a block scope variable in JavaScript?
- A)
var
- B)
let
- C)
const
- D)
block
- A)
- What is the result of
Boolean(0)
in JavaScript?- A)
true
- B)
false
- C)
undefined
- D)
null
- A)
- Which method converts an object to a JSON string?
- A)
JSON.parse()
- B)
JSON.stringify()
- C)
toJSON()
- D)
convertToJSON()
- A)
- How do you execute a function every 5 seconds?
- A)
setTimeout(myFunction, 5000);
- B)
setInterval(myFunction, 5000);
- C)
setDelay(myFunction, 5000);
- D)
setExecute(myFunction, 5000);
- A)
- Which function is used to evaluate an expression in JavaScript?
- A)
eval()
- B)
calculate()
- C)
compute()
- D)
evaluate()
- A)
- What does the
this
keyword refer to in JavaScript?- A) The global object
- B) The object that is currently executing the code
- C) The function that is currently executing
- D) The window object
- How do you check if a number is NaN?
- A)
isNaN()
- B)
checkNaN()
- C)
isNotNumber()
- D)
NaN()
- A)
- Which property of the
window
object provides information about the browser’s user agent?- A)
window.location
- B)
window.navigator
- C)
window.screen
- D)
window.document
- A)
- What does
JSON.stringify()
do?- A) Converts a JavaScript object into a JSON string
- B) Parses a JSON string into a JavaScript object
- C) Evaluates a JSON string
- D) Formats a JSON string
- Which of the following will correctly check if a variable is an array?
- A)
Array.isArray(variable)
- B)
variable.isArray()
- C)
typeof variable === 'array'
- D)
variable instanceof Array
- A)
- What is the result of
3 + '3'
in JavaScript?- A)
6
- B)
33
- C)
NaN
- D)
undefined
- A)
- Which statement will output the largest number in an array
arr
?- A)
Math.max(arr)
- B)
arr.max()
- C)
Math.max(...arr)
- D)
arr.reduce(max)
- A)
- How can you stop a loop from executing further in JavaScript?
- A)
stop
- B)
halt
- C)
break
- D)
return
- A)
- Which method will remove the last item from an array?
- A)
pop()
- B)
shift()
- C)
push()
- D)
unshift()
- A)
- How do you access a property of an object in JavaScript?
- A)
object.property
- B)
object->property
- C)
object[property]
- D)
object.property()
- A)
- What will
console.log([1] == [1]);
output?- A)
true
- B)
false
- C)
undefined
- D)
NaN
- A)
- Which operator is used to check for inequality?
- A)
!=
- B)
!==
- C)
<>
- D)
!=!
- A)
- How do you define a class in JavaScript?
- A)
class MyClass {}
- B)
define MyClass {}
- C)
function MyClass() {}
- D)
new class MyClass {}
- A)
- Which keyword is used to handle errors in JavaScript?
- A)
error
- B)
catch
- C)
try
- D)
throw
- A)
- What is the result of
10 % 3
in JavaScript?- A)
3
- B)
1
- C)
10
- D)
0
- A)
- How do you create a new instance of a class in JavaScript?
- A)
new ClassName()
- B)
ClassName.new()
- C)
ClassName.create()
- D)
ClassName()
- A)
- Which function is used to read a value from a text input field?
- A)
getValue()
- B)
readValue()
- C)
value()
- D)
input.value
- A)
- How do you compare two objects in JavaScript?
- A)
==
- B)
===
- C)
JSON.stringify()
- D)
Object.equals()
- A)
- What is the result of
null == undefined
?- A)
true
- B)
false
- C)
undefined
- D)
NaN
- A)
- How do you define a method within an object?
- A)
object.method = function() {}
- B)
object.method = () => {}
- C)
object.method() {}
- D)
object.method() = function() {}
- A)
- Which method adds one or more elements to the end of an array and returns the new length of the array?
- A)
push()
- B)
pop()
- C)
shift()
- D)
unshift()
- A)
- What will
console.log('5' - 2);
output?- A)
3
- B)
52
- C)
NaN
- D)
undefined
- A)
- How do you check if a string contains a certain substring?
- A)
str.includes(substring)
- B)
str.has(substring)
- C)
str.contains(substring)
- D)
str.indexOf(substring)
- A)
- Which method is used to join all elements of an array into a string?
- A)
join()
- B)
combine()
- C)
concat()
- D)
merge()
- A)
- How do you create a promise in JavaScript?
- A)
new Promise()
- B)
Promise.create()
- C)
Promise.new()
- D)
new Promise(function(resolve, reject) {})
- A)
- What will
console.log(1 < 2 < 3);
output?- A)
true
- B)
false
- C)
undefined
- D)
NaN
- A)
- How do you call a function that is stored in a variable?
- A)
variable()
- B)
call(variable)
- C)
variable.call()
- D)
variable.invoke()
- A)
- Which method removes whitespace from the start of a string?
- A)
trimStart()
- B)
trimLeft()
- C)
stripStart()
- D)
removeWhitespace()
- A)
- How do you set a timeout to execute a function after 2 seconds?
- A)
setTimeout(function, 2000)
- B)
delay(function, 2000)
- C)
setInterval(function, 2000)
- D)
schedule(function, 2000)
- A)
- Which of the following is used to create a new array in JavaScript?
- A)
Array.create()
- B)
new Array()
- C)
Array.new()
- D)
Array.build()
- A)
- How do you remove an element from a specific index in an array?
- A)
splice(index, 1)
- B)
remove(index)
- C)
delete(index)
- D)
slice(index, 1)
- A)
- Which method returns the index of the first occurrence of a specified value in a string?
- A)
indexOf()
- B)
find()
- C)
search()
- D)
positionOf()
- A)
- How do you get a substring from a string?
- A)
substring(start, end)
- B)
slice(start, end)
- C)
substr(start, length)
- D) All of the above
- A)
- What will
console.log('hello'.toUpperCase());
output?- A)
HELLO
- B)
hello
- C)
Hello
- D)
HELLO
- A)
- How do you define a default parameter value in a function?
- A)
function myFunction(param = defaultValue) {}
- B)
function myFunction(param, defaultValue) {}
- C)
function myFunction(param, defaultValue = param) {}
- D)
function myFunction(param: defaultValue) {}
- A)
- What will
console.log(2 + '2' - 1);
output?- A)
21
- B)
22
- C)
20
- D)
1
- A)
- Which method is used to sort the elements of an array?
- A)
sort()
- B)
order()
- C)
arrange()
- D)
sequence()
- A)
- How do you create a new RegExp object?
- A)
new RegExp(pattern)
- B)
RegExp.create(pattern)
- C)
RegExp(pattern)
- D)
new RegExp(pattern, flags)
- A)
- Which keyword is used to define a class method in JavaScript?
- A)
class
- B)
method
- C)
function
- D)
static
- A)
- How do you determine if an object is a function?
- A)
typeof object === 'function'
- B)
object.isFunction()
- C)
object instanceof Function
- D)
object.type()
- A)
- What does the
filter()
method do in JavaScript arrays?- A) Returns a new array with elements that pass a test
- B) Sorts the array elements
- C) Finds the first element that passes a test
- D) Modifies the original array
- How can you delay the execution of a function by 2 seconds?
- A)
setTimeout(myFunction, 2000)
- B)
setDelay(myFunction, 2000)
- C)
schedule(myFunction, 2000)
- D)
delay(myFunction, 2000)
- A)
- Which method is used to find the largest value in an array of numbers?
- A)
Math.max()
- B)
arr.max()
- C)
arr.reduce(max)
- D)
arr.largest()
- A)
- How do you access an HTML element with a specific ID using JavaScript?
- A)
document.getElementById('id')
- B)
document.querySelector('#id')
- C)
document.select('#id')
- D)
document.getElement('id')
- A)
- What will
console.log(typeof []);
output?- A)
array
- B)
object
- C)
list
- D)
undefined
- A)
- Which property of the
navigator
object is used to detect the browser version?- A)
navigator.version
- B)
navigator.appVersion
- C)
navigator.userAgent
- D)
navigator.browserVersion
- A)
- What does
localStorage
provide in JavaScript?- A) Temporary storage of key-value pairs
- B) Persistent storage of key-value pairs
- C) Session-based storage of key-value pairs
- D) Network-based storage
- How can you execute a function once after a delay in JavaScript?
- A)
setTimeout()
- B)
setInterval()
- C)
requestAnimationFrame()
- D)
queueMicrotask()
- A)
- Which method is used to merge two or more arrays into a single array?
- A)
concat()
- B)
merge()
- C)
combine()
- D)
join()
- A)
- How do you update the text content of an HTML element?
- A)
element.textContent = 'new text';
- B)
element.innerHTML = 'new text';
- C)
element.setText('new text');
- D)
element.updateText('new text');
- A)
- What does the
catch
block in a try-catch statement do?- A) Catches exceptions thrown in the try block
- B) Executes code after the try block
- C) Executes code regardless of success or failure
- D) Initializes variables used in the try block
- Which method removes all elements from an array?
- A)
clear()
- B)
empty()
- C)
splice(0, arr.length)
- D)
delete()
- A)
- How do you define a variable that is immutable (cannot be changed)?
- A)
const
- B)
let
- C)
var
- D)
immutable
- A)
- Which operator is used to perform a bitwise AND operation in JavaScript?
- A)
&
- B)
&&
- C)
|
- D)
|
- A)
- How do you create a new date object in JavaScript?
- A)
new Date()
- B)
Date.create()
- C)
new DateTime()
- D)
Date.new()
- A)
- What does the
toFixed()
method do?- A) Formats a number to a specified number of decimal places
- B) Converts a string to a number
- C) Rounds a number to the nearest integer
- D) Fixes the value of a number
- How do you create a new array from an existing one with some elements removed?
- A)
filter()
- B)
slice()
- C)
splice()
- D)
reduce()
- A)
- Which method is used to iterate over the properties of an object?
- A)
forEach()
- B)
map()
- C)
Object.keys()
- D)
Object.values()
- A)
- How do you check if an object has a specific property?
- A)
object.hasOwnProperty(property)
- B)
property in object
- C)
object.contains(property)
- D)
object.includes(property)
- A)
- What does
Object.assign()
do?- A) Copies properties from one or more objects to a target object
- B) Merges multiple objects into one
- C) Creates a new object with the properties of the source object
- D) Deletes properties from an object
- Which method will return the first index of a value in an array?
- A)
indexOf()
- B)
findIndex()
- C)
search()
- D)
getIndex()
- A)
- What does the
Array.from()
method do?- A) Creates a new array from an array-like or iterable object
- B) Converts an array into a string
- C) Merges multiple arrays into one
- D) Filters elements in an array
- How do you create a promise that resolves immediately with a value?
- A)
Promise.resolve(value)
- B)
new Promise.resolve(value)
- C)
Promise(value)
- D)
new Promise(value)
- A)
- Which method will add a new element to the end of an array and return the new length?
- A)
push()
- B)
pop()
- C)
shift()
- D)
unshift()
- A)
- How do you define a method inside a class in JavaScript?
- A)
method() {}
- B)
function method() {}
- C)
class.method() {}
- D)
static method() {}
- A)
- Which method will return a new array with elements that meet a condition?
- A)
filter()
- B)
map()
- C)
reduce()
- D)
forEach()
- A)
- How do you create an empty array in JavaScript?
- A)
var arr = [];
- B)
var arr = {};
- C)
var arr = () => {};
- D)
var arr = new Array();
- A)
- What does the
toString()
method do?- A) Converts a value to a string
- B) Extracts a substring from a string
- C) Formats a number as a string
- D) Parses a string into an integer
- Which method is used to get the last element of an array?
- A)
pop()
- B)
slice(-1)
- C)
last()
- D)
peek()
- A)
- How do you check if a value is a number? – A)
typeof value === 'number'
– B)isNumber(value)
– C)value.isNumber()
– D)Number.isInteger(value)
Here are the answers to the JavaScript multiple-choice questions:
- A) Curly braces
{ }
- B)
function myFunction() {}
- A)
var variableName;
- C)
=
- C)
// This is a comment
- D)
Character
- A)
string
- A)
trim()
- A)
parseInt()
- B) Adds an element to the end
- B)
new
- B)
===
- A)
JSON.parse()
- B)
false
- A)
arr[0]
- C)
break
- B)
11
- D)
const variableName = value;
- D)
length
- B) Returns a value from the function
- C)
x instanceof Y
- C)
undefined
- A)
var obj = {};
- D)
unshift()
- A)
arr.concat(newElements)
- B)
let
- B)
false
- B)
JSON.stringify()
- B)
setInterval(myFunction, 5000);
- A)
eval()
- B) The object that is currently executing the code
- A)
isNaN()
- B)
window.navigator
- A) Converts a JavaScript object into a JSON string
- A)
Array.isArray(variable)
- B)
33
- C)
Math.max(...arr)
- C)
break
- A)
pop()
- A)
object.property
- B)
false
- A)
!=
- A)
class MyClass {}
- D)
throw
- B)
1
- A)
new ClassName()
- D)
input.value
- C)
JSON.stringify()
- A)
true
- A)
object.method = function() {}
- A)
push()
- A)
3
- A)
str.includes(substring)
- A)
join()
- D)
new Promise(function(resolve, reject) {})
- B)
false
- A)
variable()
- B)
trimLeft()
- A)
setTimeout(myFunction, 2000)
- B)
new Array()
- A)
splice(index, 1)
- A)
indexOf()
- D) All of the above
- A)
HELLO
- A)
function myFunction(param = defaultValue) {}
- C)
20
- A)
sort()
- A)
new RegExp(pattern)
- D)
static
- A)
typeof object === 'function'
- A) Returns a new array with elements that pass a test
- A)
setTimeout(myFunction, 2000)
- A)
Math.max()
- A)
document.getElementById('id')
- B)
object
- B)
navigator.appVersion
- B) Persistent storage of key-value pairs
- A)
setTimeout()
- A)
concat()
- A)
element.textContent = 'new text';
- A) Catches exceptions thrown in the try block
- C)
splice(0, arr.length)
- A)
const
- A)
&
- A)
new Date()
- A) Formats a number to a specified number of decimal places
- B)
slice()
- C)
Object.keys()
- A)
object.hasOwnProperty(property)
- A) Copies properties from one or more objects to a target object
- A)
indexOf()
- A) Creates a new array from an array-like or iterable object
- A)
Promise.resolve(value)
- A)
push()
- A)
method() {}
- A)
filter()
- A)
var arr = [];
- A) Converts a value to a string
- A)
pop()
- **A)
typeof value === 'number'
Post Views: 74
0 Comments