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!

Q31

Q31 What is the output of this code if the HTML contains an element with id demo and text 'Hello'?
document.getElementById('demo').textContent = 'Hi'; console.log(document.getElementById('demo').textContent);

A

'Hello'

B

'Hi'

C

undefined

D

Error

Q32

Q32 What will be the result of the following code if the HTML has multiple p tags?
let paragraphs = document.getElementsByTagName('p');
for(let i = 0; i < paragraphs.length; i++) {
paragraphs[i].style.color = 'blue';
}

A

All p tags turn blue

B

Only the first p tag turns blue

C

A TypeError occurs

D

No change in p tags

Q33

Q33 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

Q34

Q34 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

Q35

Q35 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

Q36

Q36 How can you stop event propagation in JavaScript?

A

event.stop()

B

event.preventDefault()

C

event.stopPropagation()

D

event.pause()

Q37

Q37 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

Q38

Q38 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

Q39

Q39 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

Q40

Q40 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

Q41

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

Q42

Q42 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

Q43

Q43 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

Q44

Q44 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

Q45

Q45 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

Q46

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

A

SyntaxError

B

TypeError

C

ReferenceError

D

RangeError

Q47

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

A

throw

B

error

C

exception

D

raise

Q48

Q48 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

Q49

Q49 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

Q50

Q50 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

Q51

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

A

String interpolation

B

Template literals

C

Concatenation

D

Variable injection

Q52

Q52 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

Q53

Q53 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

Q54

Q54 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

Q55

Q55 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

Q56

Q56 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

Q57

Q57 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

Q58

Q58 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

Q59

Q59 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

Q60

Q60 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