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!

Q1

Q1 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');

Q2

Q2 What is the purpose of JavaScript in web development?

A

To structure web pages

B

To style web pages

C

To add interactivity and dynamic content to web pages

D

To store data on the server

Q3

Q3 Which keyword is used for declaring a variable in JavaScript that can be reassigned?

A

const

B

var

C

let

D

static

Q4

Q4 In JavaScript, which of the following is a valid variable name?

A

2names

B

$name

C

-name

D

name2

Q5

Q5 Which data type in JavaScript is used to represent logical values?

A

String

B

Boolean

C

Number

D

Undefined

Q6

Q6 What does the undefined value in JavaScript represent?

A

An unassigned variable

B

A null value

C

A logical false

D

An error condition

Q7

Q7 What will be the output of the following code?
console.log(typeof null);

A

'object'

B

'null'

C

'undefined'

D

'number'

Q8

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

A

Java

B

C++

C

JavaScript

D

Python

Q9

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

A

==

B

===

C

!=

D

!==

Q10

Q10 What is the output of the following code snippet?
var a = 10;
console.log(a);

A

10

B

'10'

C

undefined

D

null

Q11

Q11 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

Q12

Q12 Which statement is used to execute a block of code multiple times in JavaScript?

A

for

B

if

C

return

D

break

Q13

Q13 What does the if statement in JavaScript do?

A

Declares a variable

B

Executes a block of code based on a condition

C

Prints a message to the console

D

Loops through a block of code

Q14

Q14 Which of the following is not a loop structure in JavaScript?

A

while

B

for

C

if

D

do-while

Q15

Q15 In a switch statement, what keyword is used to terminate a case in JavaScript?

A

end

B

break

C

stop

D

exit

Q16

Q16 What will be the output of the following code?
let a = 2;
if(a > 3) {
console.log('Yes');
} else {
console.log('No');
}

A

Yes

B

No

C

Undefined

D

Error

Q17

Q17 In a for loop, what are the three optional expressions, separated by semicolons?

A

Initializer, Condition, Incrementer

B

Condition, Incrementer, Initializer

C

Incrementer, Initializer, Condition

D

Condition, Initializer, Incrementer

Q18

Q18 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

Q19

Q19 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

Q20

Q20 Identify the problem in this code:
let i = 0;
while (i < 3) {
console.log(i);
}

A

Infinite loop

B

Syntax error

C

Logical error

D

No output

Q21

Q21 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

Q22

Q22 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

Q23

Q23 How do you define a function in JavaScript?

A

function = myFunc() {}

B

function: myFunc() {}

C

function myFunc() {}

D

myFunc() = function {}

Q24

Q24 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

Q25

Q25 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

Q26

Q26 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

Q27

Q27 What is the result of trying to extend the length of an array using a function in JavaScript?
function extendArray(arr) {
arr.push(5);
}
let myArr = [1, 2, 3];
extendArray(myArr);
console.log(myArr.length);

A

3

B

4

C

5

D

Error

Q28

Q28 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

Q29

Q29 What does the following function return?
function checkEven(number) {
return number % 2 === 0;
}
console.log(checkEven(3));

A

true

B

false

C

3

D

Error

Q30

Q30 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

...
ad verticalad vertical
ad