wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

JavaScript Day-2

Total questions: 25

Worksheet time: 16mins

Name
Class
Date
1.

What is a Map in JavaScript?

a)

A collection of unique values

b)

A collection of key-value pairs

c)

A type of array

d)

A function

2.

Which method adds a key-value pair to a Map?

a)

push()

b)

add()

c)

set()

d)

append()

3.

Which method retrieves a value by key from a Map?

a)

get()

b)

find()

c)

lookup()

d)

value()

4.

Which method removes a key-value pair by key from a Map?

a)

delete()

b)

remove()

c)

clear()

d)

pop()

5.

Which method checks if a key exists in a Map?

a)

has()

b)

contains()

c)

exists()

d)

check()

6.

Which method iterates over Map entries?

a)

forEach()

b)

map()

c)

filter()

d)

filter()

7.

A Map stores data in (a)   pairs

8.

What is the purpose of typeof in JavaScript?

a)

To assign values

b)

To check the data type of a variable

c)

To convert data types

d)

To declare variables

9.

What is the output of typeof 42?

a)

number

b)

string

c)

integer

d)

numeric

10.

Which method extracts only the integer part from a string like "10px"?

a)
  • Number()

b)
  • parseInt()

c)

parseFloat()

d)

Integer()

11.

const map = new Map();

map.set('name', 'Alice');

map.set('age', 30);

for (let [key, value] of map) {

key === 'name'

? console.log(key)

: console.log(value);

}

a)

age

30

b)

name Alice

age 30

c)

name Alice

d)

name

30

12.

typeof null returns (a)   .

13.

What is the primary purpose of typeof?

a)

Convert a variable

b)

Check the type of a variable

c)

Declare a variable

d)

Assign a variable

14.

function add(val){

return val+val+5

}

function processInput(input) {

if (typeof input === "string") {

return input.toUpperCase();

} else if (typeof input === "number") {

return add(input);

}

return input;

}

console.log(processInput(6));

console.log(processInput("4"));

console.log(processInput(3))

console.log(processInput(parseInt("5"))) what output return ?

(a)  

15.

What is the output of console.log("5" - 2)

(a)  

16.

typeof {a:1} returns __________.

a)

object

b)

map

c)

array

d)

dict

17.

let myMap = new Map();

let myObj = {fname: "John", lname: "Doe"};

myMap.set(myObj, "player");

for (let [key, value] of myMap) {

console.log(value)

} what is the Output in console ?

(a)  

18.

Which statement about Map insertion order is true?

a)

It is random.

b)

It is sorted alphabetically by keys.

c)

maintains the exact order in which pairs were added

d)

Numeric keys always come first.

19.

typeof is useful in __________ statements.

a)

loop

b)

conditional

c)

function declaration

d)

object creation

20.

What is type conversion in JavaScript?

a)

Changing the variable name

b)

Changing a value’s data type

c)

Declaring a variable

d)

Creating a function

21.

_____ Type Conversion : Manually performed by the developer.

____ Type Conversion : Automatically performed by JavaScript.

(a)  

22.

const map = new Map();

map.set('name', 'Alice');

map.set('age', 30);

map.set('city', 'chennai');

for (let [key, value] of Array.from(map)) {

key === 'name' ? map.delete(key) : console.log(value);

key === 'city' ? map.set('name', 'Alice') : map.set('age', 20);

}

console.log( Array.from(map));

a)

30

chennai

[ [ 'age', 30], [ 'city', 'chennai' ], [ 'name', 'Alice' ] ]

b)

Alice

20

chennai

[ [ 'age', 30], [ 'city', 'chennai' ], [ 'name', 'Alice' ] ]

c)

30

chennai

[ [ 'age', 20 ], [ 'city', 'chennai' ], [ 'name', 'Alice' ] ]

d)

20

chennai

[ [ 'age', 20 ], [ 'city', 'chennai' ], [ 'name', 'Alice' ] ]

23.

In a Map, if you use an object as a key and then change a property inside that object, can you still retrieve the value?

a)

No, the key is lost.

b)
  • Yes, because the object reference remains the same.

c)

Only if the object is frozen.

d)

Maps only support string keys.

24.

const arr = [3, 6, 12, 15];

function add(a, b = 1) {

if (typeof a === "number" && typeof b === "number") {

return a + b;

}

}

function decrement(a, b = 1) {

if (typeof a === "number" && typeof b === "number") {

return a - b;

}

}

for (let i = 0; i < arr.length; i++) {

let addedData = add(arr[i], i+2);

let decrementedData = decrement(arr[i], i+3);

console.log(`Original: ${arr[i]} | Added: ${addedData} | Decremented: ${decrementedData}`);

}

a)

Original: 3 | Added: 12 | Decremented: 0

Original: 6 | Added: 9 | Decremented: 8

Original: 12 | Added: 16 | Decremented: 7

Original: 15 | Added: 18 | Decremented: 9

b)

Original: 3 | Added: 5 | Decremented: 0

Original: 6 | Added: 9 | Decremented: 2

Original: 12 | Added: 12 | Decremented: 7

Original: 15 | Added: 10 | Decremented: 3

c)

Original: 3 | Added: 5 | Decremented: 0

Original: 6 | Added: 9 | Decremented: 2

Original: 12 | Added: 16 | Decremented: 7

Original: 15 | Added: 20 | Decremented: 9

d)

Original: 3 | Added:2 | Decremented: 0

Original: 6 | Added: 9 | Decremented: 2

Original: 12 | Added: 16 | Decremented: 8

Original: 15 | Added: 20 | Decremented: 19

25.

let str = "name";

console.log(Boolean(str));

console.log(!!str && str);

(a)