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 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

Q92

Q92 What is the problem with this code?
try { // code } catch(e) { console.log(e); }

A

There's no error to catch

B

The try block is empty

C

The catch block should have more logic

D

No error

Q93

Q93 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

Q94

Q94 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

Q95

Q95 What ES6 feature allows us to combine multiple strings and variables?

A

String interpolation

B

Template literals

C

Concatenation

D

Variable injection

Q96

Q96 What is the purpose of the let and const keywords in ES6?

A

To create global variables

B

To declare variables with block scope

C

To declare constants

D

Both B and C

Q97

Q97 How does the spread operator (...) work in ES6?

A

It divides an array into individual elements

B

It merges arrays or object properties

C

It spreads an object into multiple arrays

D

It is used for error handling

Q98

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

A

1

B

2

C

[1, 2]

D

undefined

Q99

Q99 Consider the following ES6 code:
const square = x => x * x; console.log(square(4));
What is the output?

A

4

B

8

C

16

D

Syntax error

Q100

Q100 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

Q101

Q101 Find the mistake in this ES6 code:
let obj = { a: 1, b: 2, a: 3 }; console.log(obj.a);

A

Repeated property name in object literal

B

Syntax error in object declaration

C

Error in console.log statement

D

No error

Q102

Q102 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

Q103

Q103 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

Q104

Q104 How can "Callback Hell" be avoided in JavaScript?

A

Using promises and async/await

B

Increasing the response time of servers

C

Writing all code in a single callback function

D

Avoiding the use of callback functions altogether

Q105

Q105 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

Q106

Q106 Consider this code:
function firstFunction(params, callback) {
// Do something, then callback()
}
function secondFunction(params) {
firstFunction(params, function(err, data) {
if (err) {
console.log('Error:', err);
} else {
console.log('Data:', data);
}
}); }
What pattern does this represent?

A

Callback pattern

B

Module pattern

C

Observer pattern

D

Singleton pattern

Q107

Q107 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

Q108

Q108 Spot the error in this callback usage:
function fetchData(callback) {
if (error) {
callback(error);
} return callback(null, data);
}

A

The callback is called twice

B

The data is returned before the callback is called

C

The error is not properly handled

D

There is no error

Q109

Q109 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

Q110

Q110 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

Q111

Q111 What happens if a promise is rejected and there is no .catch() block?

A

The error is silently ignored

B

The promise is fulfilled with an undefined value

C

A TypeError is thrown

D

An unhandled promise rejection occurs

Q112

Q112 What does the following code do?
new Promise((resolve, reject) => {
setTimeout(() => resolve('Result'), 2000);
})
.then(result => console.log(result));

A

Executes a function after 2 seconds

B

Logs 'Result' immediately

C

Creates a promise that resolves with 'Result' after 2 seconds, then logs it

D

Repeatedly logs 'Result' every 2 seconds

Q113

Q113 Consider this code:
new Promise((resolve, reject) => {
throw new Error('Error');
})
.catch(error => console.log(error.message))
.then(() => console.log('Completed'));
What will be the output?

A

'Error' then 'Completed'

B

Just 'Error'

C

Just 'Completed'

D

A runtime error

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

ad verticalad vertical
ad