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!

Q61

Q61 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

Q62

Q62 Which of the following is a correct syntax to display “Hello World” in an alert box using JavaScript?

A

alertBox('Hello World');

B

alert('Hello World');

C

msgAlert('Hello World');

D

displayAlert('Hello World');

Q63

Q63 Which of the following is an example of a loosely typed language?

A

Java

B

C++

C

JavaScript

D

Python

Q64

Q64 Which operator is used to check both the value and the type of a variable in JavaScript?

A

==

B

===

C

!=

D

!==

Q65

Q65 What is the output of the following code snippet?
let x = 'Hello';
let y = 'World';
console.log(x + ' ' + y);

A

HelloWorld

B

'Hello World'

C

'Hello' 'World'

D

Hello World

Q66

Q66 What is the output of this code snippet?
for (let i = 0; i < 3; i++) {
console.log(i);
}

A

012

B

123

C

0-1-2

D

1-2-3

Q67

Q67 Consider the following code:
let x = 5;
let result = (x > 3) ? 'Yes' : 'No';
console.log(result);
What is the output?

A

'Yes'

B

'No'

C

true

D

false

Q68

Q68 Find the error in the following code:
for (let i = 0; i <= 5; i++) {
if(i % 2 == 0) continue;
console.log(i);
}

A

It doesn't print any number

B

It only prints odd numbers

C

It only prints even numbers

D

Syntax error

Q69

Q69 What is the purpose of a function in JavaScript?

A

To store data

B

To repeat a task multiple times

C

To encapsulate code that performs a specific task

D

To create web pages

Q70

Q70 How do you define a function in JavaScript?

A

function = myFunc() {}

B

function: myFunc() {}

C

function myFunc() {}

D

myFunc() = function {}

Q71

Q71 What will be the output of this function call?
function sum(a, b) {
return a + b;
}
console.log(sum(3, 4));

A

3

B

4

C

7

D

Error

Q72

Q72 In JavaScript, what is a callback function?

A

A function that runs after the page loads

B

A function passed as an argument to another function

C

A function that calls itself

D

A function that performs an HTTP request

Q73

Q73 Which of the following is true about arrow functions in JavaScript?

A

They do not have their own this context

B

They can be used as constructors

C

They must have a return statement

D

They are the same as traditional functions

Q74

Q74 Consider the following code snippet:
function greet() {
return 'Hello World';
}
console.log(greet());
What is the output?

A

'greet'

B

'Hello World'

C

undefined

D

Error

Q75

Q75 Identify the error in this function:
function multiply(a, b) {
console.log(a * b);
}

A

It does not return any value

B

It returns the wrong value

C

Syntax error

D

No error

Q76

Q76 What is wrong with this function declaration?
function power(base, exponent) {
if (exponent == 0) return 1;
else return base * power(base, exponent - 1);
} console.log(power(2));

A

It doesn't handle the case when exponent is not provided

B

It returns the wrong value

C

It causes an infinite loop

D

Syntax error

Q77

Q77 Which method is used to add an element to the end of an array in JavaScript?

A

push()

B

unshift()

C

pop()

D

shift()

Q78

Q78 How do you find the length of an array in JavaScript?

A

array.size()

B

array.length

C

array.count()

D

length(array)

Q79

Q79 What does the splice method do in an array?

A

Copies a portion of an array

B

Concatenates arrays

C

Changes the content of an array

D

Finds an element in an array

Q80

Q80 Which of the following array methods in JavaScript does not change the original array?

A

sort()

B

splice()

C

forEach()

D

push()

Q81

Q81 What is the output of this code snippet?  let arr = [10, 20, 30, 40];  let newArr = arr.map(x => x / 10);  console.log(newArr);

A

[1, 2, 3, 4]

B

[10, 20, 30, 40]

C

[0.1, 0.2, 0.3, 0.4]

D

Error

Q82

Q82 What will be the output of this code snippet?
let numbers = [1, 2, 3];
numbers[10] = 11;
console.log(numbers.length);

A

3

B

4

C

11

D

10

Q83

Q83 Identify the issue in this array declaration:
let numbers = new Array(-5);

A

Negative size

B

Syntax error

C

Logical error

D

No error

Q84

Q84 How do you access the value of a property in a JavaScript object?

A

object{propertyName}

B

object[propertyName]

C

object.propertyName

D

Both B and C

Q85

Q85 Find the issue in this object definition:
let obj = { '1a': 10, b: 20 };
console.log(obj.1a);

A

Invalid property name

B

Syntax error in console.log

C

Incorrect value assignment

D

No error

Q86

Q86 Identify the error in this object method:
let obj = {
greet: function() {
return 'Hello, ' + name;
}
};
console.log(obj.greet());

A

name is not defined

B

greet is not a function

C

Syntax error

D

No error

Q87

Q87 Which JavaScript method is used to select an HTML element by its ID?

A

getElementById()

B

querySelector()

C

getElementsByClassName()

D

getElementsByTagName()

Q88

Q88 What is the difference between innerHTML and textContent properties in DOM manipulation?

A

innerHTML can include HTML tags; textContent cannot

B

textContent is faster than innerHTML

C

textContent can include HTML tags; innerHTML cannot

D

There is no difference

Q89

Q89 Consider this code:
let items = document.querySelectorAll('.item');
console.log(items.length);
What does it output if there are three elements with class item in the HTML?

A

3

B

'item'

C

undefined

D

Error

Q90

Q90 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