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!

Q61

Q61 Identify the error in this code:
document.getElementByld('demo').innerText = 'Welcome';

A

Typo in method name

B

Missing element with id 'demo'

C

Incorrect property innerText

D

No error

Q62

Q62 Spot the mistake in this code:
let btn = document.getElementById('submit');
btn.onClick = function() {
console.log('Clicked');
};

A

Incorrect event handler

B

Missing element with id 'submit'

C

Syntax error in the function

D

No error

Q63

Q63 What is an event in the context of JavaScript?

A

A user action like a mouse click

B

A change in the state of the program

C

A server response

D

All of the above

Q64

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

A

addEventListener()

B

attachEvent()

C

onEvent()

D

bindEvent()

Q65

Q65 What is event bubbling in JavaScript?

A

When an event on a child element is also triggered on parent elements

B

When an event triggers a server request

C

When an event occurs repeatedly in a short period

D

When an event fails to trigger

Q66

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

A

onclick

B

onmouseover

C

onmousemove

D

onchange

Q67

Q67 How can you stop event propagation in JavaScript?

A

event.stop()

B

event.preventDefault()

C

event.stopPropagation()

D

event.pause()

Q68

Q68 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

Q69

Q69 Consider the following code snippet:
element.addEventListener('click', function() {
console.log('Element clicked');
});
What does this code do?

A

Logs a message when the element is double-clicked

B

Changes the style of the element when clicked

C

Logs a message when the element is clicked

D

Submits a form when the element is clicked

Q70

Q70 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

Q71

Q71 Identify the error in this code:
document.getElementById('myBtn').onClick = function() {
console.log('Button clicked');
};

A

Incorrect event property

B

Syntax error

C

Logic error

D

No error

Q72

Q72 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

Q73

Q73 What does AJAX stand for in web development?

A

Asynchronous JavaScript And XML

B

Automated JavaScript And XHTML

C

Advanced JavaScript And XML

D

Asynchronous Java And XML

Q74

Q74 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

Q75

Q75 What is the purpose of the Fetch API in JavaScript?

A

To send data to a server

B

To manipulate the DOM

C

To retrieve resources asynchronously over the network

D

To store data in the browser

Q76

Q76 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

Q77

Q77 What does the following JavaScript code do?
fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => console.log(data));

A

Sends data to 'https://api.example.com/data'

B

Requests data from 'https://api.example.com/data' and logs it

C

Creates a new resource at 'https://api.example.com/data'

D

Deletes data from 'https://api.example.com/data'

Q78

Q78 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

Q79

Q79 How does this code handle errors when using the Fetch API?
fetch('https://api.example.com/data')
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.catch(error => console.error('Fetch error:', error));

A

It logs errors to the console

B

It redirects the user to an error page

C

It retries the fetch request

D

It sends an error report to the server

Q80

Q80 Identify the issue in this fetch request:
fetch('https://api.example.com/data', {
method: 'GET',
body: JSON.stringify({id: 1})
});

A

GET requests should not have a body

B

The URL is incorrect

C

The method should be POST

D

There is a syntax error

Q81

Q81 Spot the error in this code:
fetch('https://api.example.com/data')
.then(response => console.log(response.json()));

A

Improper use of response.json()

B

Missing catch for error handling

C

The URL is incorrect

D

There is a syntax error

Q82

Q82 What is the purpose of the try...catch statement in JavaScript?

A

To test code for errors

B

To speed up code execution

C

To declare variables

D

To loop through arrays

Q83

Q83 What type of error is thrown when an undefined variable is used in JavaScript?

A

SyntaxError

B

TypeError

C

ReferenceError

D

RangeError

Q84

Q84 Which statement is used to manually throw an exception in JavaScript?

A

throw

B

error

C

exception

D

raise

Q85

Q85 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

Q86

Q86 What is the difference between a SyntaxError and a ReferenceError in JavaScript?

A

A SyntaxError occurs for mistakes in the code's syntax, while a ReferenceError occurs for illegal or invalid references to variables

B

A SyntaxError occurs when variables are not found, while a ReferenceError occurs for syntax mistakes

C

A SyntaxError is a runtime error, while a ReferenceError is a compile-time error

D

No difference

Q87

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

Q88

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

A

'undefined'

B

'object'

C

'string'

D

'error'

Q89

Q89 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

Q90

Q90 Identify the error in this code:
try {
let result = 'Result';
console.log(result);
} catch(e) {
console.log(e);
} finally {
console.log(reslt);
}

A

Misspelled variable in the finally block

B

Error in the try block

C

Error in the catch block

D

No error

ad verticalad vertical
ad