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

Q32

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

A

push()

B

unshift()

C

pop()

D

shift()

Q33

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

A

array.size()

B

array.length

C

array.count()

D

length(array)

Q34

Q34 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

Q35

Q35 In JavaScript, how can you check if a variable is an array?

A

typeof variable

B

variable.isArray()

C

Array.isArray(variable)

D

variable instanceof Array

Q36

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

A

sort()

B

splice()

C

forEach()

D

push()

Q37

Q37 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

Q38

Q38 What is the output of the following code?
let fruits = ['apple', 'banana', 'mango'];
console.log(fruits[1]);

A

apple

B

banana

C

mango

D

undefined

Q39

Q39 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

Q40

Q40 Consider the following code:
let arr = [1, 2, 3];
arr[5] = 5;
console.log(arr.filter(x => x === undefined).length);
What is the output?

A

0

B

2

C

3

D

5

Q41

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

A

Negative size

B

Syntax error

C

Logical error

D

No error

Q42

Q42 Spot the mistake in this code snippet:
let data = [1, 2, 3];
delete data[1];
console.log(data[1]);

A

undefined is logged instead of 2

B

2 is not deleted

C

Syntax error

D

No error

Q43

Q43 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

Q44

Q44 What will be the output of console.log(typeof {})?

A

'object'

B

'array'

C

'null'

D

'undefined'

Q45

Q45 In JavaScript, what is a method?

A

A predefined function

B

A loop inside an object

C

A function stored as an object property

D

An external library function

Q46

Q46 How do you create a new object in JavaScript?

A

Object.create()

B

new Object()

C

Both A and B

D

Neither A nor B

Q47

Q47 What is the purpose of the this keyword in JavaScript objects?

A

Refer to the global object

B

Refer to the current object

C

Create a new object

D

Duplicate an object

Q48

Q48 What is the result of accessing a property that doesn’t exist on an object?

A

null

B

undefined

C

0

D

Error

Q49

Q49 What is the output of the following code snippet?
let obj = { a: 1, b: 2 };
console.log(obj.a);

A

1

B

2

C

undefined

D

Error

Q50

Q50 Consider the code:
let person = { name: 'Alice', age: 25 };
delete person.age; console.log(person.age);
What is the output?

A

'Alice'

B

25

C

undefined

D

Error

Q51

Q51 Given the following code, what is printed to the console?
let obj1 = { a: 1 };
let obj2 = { a: 1 };
console.log(obj1 === obj2);

A

true

B

false

C

null

D

Error

Q52

Q52 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

Q53

Q53 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

Q54

Q54 What does DOM stand for in web development?

A

Document Object Model

B

Data Object Management

C

Direct Object Manipulation

D

Display Object Model

Q55

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

A

getElementById()

B

querySelector()

C

getElementsByClassName()

D

getElementsByTagName()

Q56

Q56 How can you change the text content of an HTML element in the DOM using JavaScript?

A

element.textContent = 'new text'

B

element.innerHTML = 'new text'

C

Both A and B

D

Neither A nor B

Q57

Q57 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

Q58

Q58 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

Q59

Q59 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

Q60

Q60 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

ad verticalad vertical
ad