an HCL GUVI product

javascript programming banner

JavaScript Multiple Choice Questions (MCQs) and Answers

Master JavaScript with Practice MCQs. Explore our curated collection of Multiple Choice Questions. Ideal for placement and interview preparation, our questions range from basic to advanced, ensuring comprehensive coverage of JavaScript. Begin your placement preparation journey now!

Q91

Q91 Which method is commonly used to attach an event handler to an element in JavaScript?

A

addEventListener()

B

attachEvent()

C

onEvent()

D

bindEvent()

Q92

Q92 Which of the following is not a mouse event in JavaScript?

A

onclick

B

onmouseover

C

onmousemove

D

onchange

Q93

Q93 What does the following JavaScript code do?
document.getElementById('btn').onclick = function() {
alert('Button clicked');
};

A

Displays an alert when a button is clicked

B

Changes the text of a button

C

Reloads the page

D

Does nothing

Q94

Q94 What will be the result of this code if div1 is inside div2 in the HTML structure?
document.getElementById('div1').addEventListener('click', function(event) {
console.log('div1 clicked');
event.stopPropagation();
});
document.getElementById('div2').addEventListener('click', function() {
console.log('div2 clicked');
});

A

Only 'div1 clicked' will be logged

B

Only 'div2 clicked' will be logged

C

Both 'div1 clicked' and 'div2 clicked' will be logged

D

No message will be logged

Q95

Q95 What is the issue with this code?
button.addEventListener('click', doSomething); function doSomething() {
alert('Clicked');
}

A

Missing event listener

B

doSomething is not defined

C

Incorrect event type

D

No error

Q96

Q96 What is the main advantage of using AJAX in a web application?

A

Faster server response time

B

Increased security

C

Reduced server load and asynchronous requests

D

Simpler coding requirements

Q97

Q97 What is the difference between the Fetch API and XMLHttpRequest?

A

Fetch can only send asynchronous requests

B

Fetch returns a promise, making it easier to work with asynchronous operations

C

XMLHttpRequest is faster than Fetch

D

Fetch cannot handle responses in JSON format

Q98

Q98 What will this Fetch API code snippet do? fetch('https://api.example.com/update', { method: 'POST', body: JSON.stringify({name: 'Alice'})}) .then(response => response.json()).then(data => console.log(data));

A

Retrieves data from the URL

B

Updates data at the URL and logs the response

C

Deletes data at the URL

D

Submits data to the URL and logs the response

Q99

Q99 What does the finally block in a try...catch statement do?

A

It runs only if no errors occur

B

It runs after the try and catch blocks, regardless of the result

C

It runs as a cleanup process

D

It checks for any remaining errors

Q100

Q100 What is the output of this code?
try {
console.log('Start');
throw new Error('An error occurred');
console.log('End');
} catch(e) {
console.log('Caught an error');
}

A

'Start' 'End' 'Caught an error'

B

'Start' 'Caught an error'

C

'Caught an error'

D

'Start' 'An error occurred'

Q101

Q101 What will this code output?
try {
let x = y;
} catch(e) {
console.log(typeof e);
}

A

'undefined'

B

'object'

C

'string'

D

'error'

Q102

Q102 What will happen when this code is executed?
try {
null.foo();
}
catch(e) {
console.log(e.name);
} finally {
console.log('Done');
}

A

Logs 'TypeError' and 'Done'

B

Logs 'ReferenceError' and 'Done'

C

Only logs 'Done'

D

Causes a fatal error

Q103

Q103 Find the mistake in this code:
try {
let x = 10;
if(x > 5) {
throw 'Too high';
}
} catch(e) {
console.log('Error: ', e);
}

A

Incorrect use of throw

B

x > 5 should be x < 5

C

Error in the catch block

D

No error

Q104

Q104 Spot the error in this code:
try {
let obj = JSON.parse('{"name":"Alice"');
} catch(e) {
console.log('Parsing error:', e);
}

A

Missing closing brace in JSON string

B

Incorrect use of JSON.parse

C

Error in the catch block

D

No error

Q105

Q105 What is an arrow function in ES6?

A

A function that points to another function

B

A shorter syntax for writing function expressions

C

A function used for one-way data binding

D

A special function for asynchronous programming

Q106

Q106 What does the following ES6 code output?
let [a, b] = [1, 2];
console.log(a);

A

1

B

2

C

[1, 2]

D

undefined

Q107

Q107 What will this ES6 code output?
function multiply(a, b = 2) {
return a * b;
}
console.log(multiply(3));

A

3

B

6

C

undefined

D

NaN

Q108

Q108 What is a callback function in JavaScript?

A

A function that is scheduled to run after a set interval

B

A function that calls itself

C

A function passed into another function as an argument to be executed later

D

A function that runs immediately after declaration

Q109

Q109 What is "Callback Hell" in JavaScript?

A

A large number of nested callback functions that make the code hard to read and maintain

B

An error thrown by a failed callback function

C

A callback function that never gets executed

D

A standard method for handling asynchronous operations

Q110

Q110 What does the following code do?
function processData(callback) {
if (data) {
callback(null, data);
} else {
callback(new Error('No data'), null);
}
}

A

Throws an error if there's no data

B

Processes data asynchronously and executes the callback function

C

Logs data to the console

D

Creates a new data object

Q111

Q111 What is the output of this code? function doAsyncTask(cb) { setTimeout(() => { console.log('Async Task Calling Callback'); cb(); }, 1000); } doAsyncTask(() => console.log('Callback Called'));

A

'Async Task Calling Callback' then 'Callback Called' after 1 second

B

'Callback Called' then 'Async Task Calling Callback' after 1 second

C

'Async Task Calling Callback' and 'Callback Called' simultaneously

D

Nothing, it's an infinite loop

Q112

Q112 What is a Promise in JavaScript?

A

A function that executes asynchronously

B

A callback function that executes immediately

C

An object representing the eventual completion or failure of an asynchronous operation

D

A data structure for storing multiple callbacks

Q113

Q113 How does promise chaining work in JavaScript?

A

By returning a new promise from the callback of another promise

B

By executing multiple callbacks in parallel

C

By repeatedly calling the same promise

D

By automatically rejecting a promise after a set timeout

Q114

Q114 What is the output of this promise chain?
Promise.resolve(1)
.then((x) => x + 1)
.then((x) => {
throw new Error('My Error');
})
.catch(() => console.log('Caught'))
.then(() => console.log('Done'));

A

'Caught' then 'Done'

B

'My Error' then 'Done'

C

Just 'Caught'

D

Just 'Done'

Q115

Q115 Spot the error in this promise code:
let p = new Promise((resolve, reject) => {
resolve('Success');
reject('Failed');
}).then(result => console.log(result)).catch(error => console.log(error));

A

The promise should not call both resolve and reject

B

The .then() method is misplaced

C

The .catch() block is unnecessary

D

The promise does not handle asynchronous operations

Q116

Q116 What is the purpose of the async keyword in JavaScript?

A

To pause the execution of a function

B

To declare a function as asynchronous

C

To speed up a function

D

To handle multiple promises simultaneously

Q117

Q117 How does await work inside an async function?

A

It stops the entire program until the awaited promise resolves

B

It waits for a promise to resolve before continuing the execution of the async function

C

It sends a request to the server

D

It creates a new thread for asynchronous execution

Q118

Q118 What will the following async function output? async function fetchData() { let response = await fetch('https://api.example.com/data'); let data = await response.json(); console.log(data); } fetchData();

A

The contents of 'https://api.example.com/data'

B

A JavaScript object

C

An error message

D

Nothing

Q119

Q119 Consider this code: async function checkData() { try { let response = await fetch('https://api.example.com/data'); if (!response.ok) throw new Error('Error fetching data'); let data = await response.json(); return data; } catch (error) { console.error(error); } } What happens when fetch fails?

A

It logs 'Error fetching data'

B

It returns 'Error fetching data'

C

It throws an error but doesn't catch it

D

It returns undefined

Q120

Q120 Identify the issue in this async function: async function loadData() { let data = await fetch('https://api.example.com').json(); console.log(data); }

A

Incorrect use of await

B

Syntax error in fetch

C

No error handling for fetch failure

D

Missing async keyword