wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Javascript I

Total questions: 33

Worksheet time: 18mins

Name
Class
Date
1.

What keyword is used to declare a variable in JavaScript?

a)

let

b)

var

c)

const

2.

What is hoisting in JavaScript?

a)

It's a JavaScript engine's way of moving all variable declarations to the top of the function or global scope.

b)

It's a way to make variables float in the air.

c)

It's a form of error handling.

d)

It's related to the event handling mechanism.

3.

What is the key difference between using let and var to declare variables in JavaScript?

a)
  • let variables have block scope, while var variables have function scope.

b)

var variables have block scope, while let variables have function scope.

c)

There is no difference between let and var in terms of scoping.

d)

let variables are global, while var variables are local.

4.

Which JavaScript method is used to add a new HTML element to the DOM?

a)

appendNode()

b)

createElement()

c)

addHTML()

d)

appendChild()

5.

What is the purpose of the typeof operator in JavaScript?

a)

To check the type of a variable or expression.

b)

To perform mathematical operations.

c)

To create arrow functions.

d)

To compare two values for equality.

6.

Which JavaScript event occurs when a user clicks on an HTML element?

a)

hover

b)

load

c)

click

d)

submit

7.

What is a regular expression (RegEx) in JavaScript used for?

a)

To format design pattern.

b)

To create text.

c)

To search for and manipulate patterns in strings.

8.
A variable;
a)
Expresses a true false statement
b)
stores a string, number, or boolean, and gives it a specific, case-sensitive name
c)
takes in an input, does something with it, and then returns an output.
d)
Runs a boolean (true/false) statement based on data entered
9.
A boolean;
a)
Expresses a true false statement
b)
can contain letters, numbers, spaces, and symbols.
c)
takes in an input, does something with it, and then returns an output.
d)
Runs a boolean (true/false) statement based on data entered
10.

What will be the output of this code?

a)

Hi there
Goodbye

b)

Ciao!
Goodbye

c)

Ciao!
ReferenceError: wariable not defined.

11.

Which best defines a variable with block scope?

a)

A variable that is available throughout a program.

b)

A variable that is available outside a block.

c)

A variable that is available within a function.

d)

A variable that is defined within a block and only available insdie a block.

12.

What will be the output of this code?

a)

Next stop: destination

b)

Uncaught ReferenceError: destination is not defined

c)

Next stop: Create Lake, Oregon

13.

How many global variables are there in the following block of code?

a)

1

b)

2

c)

3

d)

4

14.

What is preferable: defining variables in the global scope or defining variables in the block scope?

a)

Defining variables in the global scope and the block scope are equally preferable.

b)

Defining variables in the block scope. Variables defined in the global scope can cause unexpected behavior in our code.

c)

Defining variables in the block scope. Variables defined at the block level can still be used at the global level.

d)

Defining variables in the global scope. Variables defined in the block scope are restrictive and often conflict with variables defined in the global scope.

15.

Which variables possess block scope?

a)

input, controlVal

b)

input, controlVal, multiplier

c)

val, number, controlVal, phase

d)

number, phase, val

16.

What is a globally scoped variable?

a)

A variable that is accessible in a block, and only inside a block.

b)

A variable that is accessible to any part of the program.

c)

A variable that is defined in a function.

d)

A variable that is also a parameter.

17.

What’s the purpose of a parameter?

a)

To specify actual values passed to a function.

b)

To call a function.

c)

To allow a function to accept data.

18.

This code prints: I ran 3 miles at an average of undefined per mile. Why does the text include undefined?

a)

The miles parameter is not declared.

b)

The call to workoutJournal is missing a second argument.

c)

workoutJournal does not print the value of both arguments.

d)

workoutJournal is not defined.

19.

What will be printed to the console?

a)

This fruit is delicious!

b)

This is delicious!

c)

This apple is delicious!

d)

This undefined is delicious!

20.

Which of the following expressions can be used to select the element with the HTML attribute id="first"?

a)

document.getElementById('first')

b)

document.querySelector('first')

21.

Suppose that ele is an element node. Select all the expressions below which can be used to select the parent node of ele

a)

ele.parentElement

b)

ele.parentNode

c)

ele.TextContainer

d)

ele.getParent()

22.

<div id="t1"> <p>A paragraph</p> </div>
What will document.getElementById('t1').childNodes[0] return?

a)

text node

b)

An element node

c)

A comment node

d)

None of the above

23.
a)

Changes the text color of the element with ID "myElement" to red.

b)

Changes the background color of the element with ID "myElement" to red.

c)

Appends the text "red" to the element with ID "myElement".

d)

Selects the first element with the class "myElement" and changes its text color to red.

24.

What does the querySelector method do in JavaScript ?

a)

Selects the first element that matches a specified CSS selector.

b)

Selects all elements that match a specified CSS selector.

c)

Removes the first element that matches a specified CSS selector.

d)

Adds a new element with a specified CSS selector.

25.

How can you attach a click event listener to a button with the id "myButton" using JavaScript?

a)

querySelector("#myButton").addEventListener("click", myFunction);

b)

getElementById("myButton").addEventListener("click", myFunction);

c)

querySelector(".myButton").addEventListener("click", myFunction);

d)

getElementsByTagName("myButton")[0].addEventListener("click", myFunction);

26.

What does the innerHTML property do in JavaScript when used with an HTML element?

a)

Retrieves or sets the content of an element, including HTML markup.

b)

Retrieves or sets the value of an element's attribute.

c)

Adds a new element inside the target element.

d)

Removes the target element from the DOM.

27.

Which method is used to prevent a form from being submitted until it passes validation in JavaScript?

a)

event.stopSubmit()

b)

event.preventSubmit()

c)
event.stop()
d)
event.preventDefault()
28.

how can you pass a parameter to the handleClick function when it is used as an event listener for a button click event?

a)

You cannot pass a parameter to an event listener function.

b)

Use an anonymous function to wrap handleClick and pass the parameter within the wrapper function.

c)

Include the parameter as an argument when calling addEventListener like handleClick(param)

d)

Modify the handleClick function to accept the parameter directly from the event object.

29.

In the following code, what does the "this" keyword refer to when the button is clicked?


a)

It refers to the event object that triggered the click event.

b)

It refers to the global window object.

c)

It refers to the button element with the ID "myButton."

d)

It refers to the text content of the alert message.

30.

In the following code, what does the "this" keyword refer to when the button is clicked?


a)

It refers to the event object that triggered the click event.

b)

It refers to the global window object.

c)

It refers to the button element with the ID "myButton."

d)

It refers to the text content of the alert message.

31.

What is the function that stops the event from further propagation to higher-level parent elements.




(a)  

32.

If I click on the button 3 times, the message "Button Clicked" will be shown :

a)

1 time

b)

2 times

c)

3 times

d)

0 times

33.

How many scopes are there in this codes?

a)

3

b)

2

c)

1

d)

4