wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Mastering JavaScript DOM and Error Handling

Total questions: 10

Worksheet time: 8mins

Name
Class
Date
1.

What method is used to select an element by its ID in the DOM?

a)

selectById

b)

getElement

c)

findElementById

d)

getElementById

2.

How do you add an event listener to a button in JavaScript?

a)

button.addEvent('click', function() { /* your code here */ });

b)

button.addEventListener('click', function() { /* your code here */ });

c)

button.attachEvent('onclick', function() { /* your code here */ });

d)

button.onClick(function() { /* your code here */ });

3.

What is the purpose of the preventDefault() method in event handling?

a)

To trigger a different event instead of the default.

b)

To enhance the default action of an event.

c)

To log the event details to the console.

d)

To prevent the default action of an event from occurring.

4.

Which method can be used to validate a form before submission?

a)

CSS styling

b)

Server-side validation

c)

HTML form attributes

d)

JavaScript validation

5.

How can you access all elements of a specific class in the DOM?

a)

Use document.querySelector('className') to access elements.

b)

Use document.getElementsByTagName('className') to access elements.

c)

Use document.getElementById('className') to access elements.

d)

Use document.getElementsByClassName('className') or document.querySelectorAll('.className') to access elements of a specific class.

6.

What is the correct syntax to change the background color of an element using JavaScript?

a)

document.getElementById('elementId').setBackgroundColor('red');

b)

document.getElementById('elementId').style.bgColor = 'red';

c)

document.getElementById('elementId').backgroundColor = 'red';

d)

document.getElementById('elementId').style.backgroundColor = 'red';

7.

How do you handle errors in JavaScript using try-catch?

a)

Use if (error) { /* handle error */ }

b)

Use try { /* code */ } catch (error) { /* handle error */ }

c)

console.log('Error occurred');

d)

throw new Error('An error occurred');

8.

What is the difference between innerHTML and textContent?

a)

innerHTML includes HTML tags; textContent only includes plain text.

b)

textContent can include HTML tags.

c)

innerHTML is faster than textContent.

d)

innerHTML only works with plain text.

9.

How can you remove an element from the DOM?

a)

Call removeElement(element) function.

b)

Use element.remove() or parentNode.removeChild(element).

c)

Set element.style.display to 'none' to hide it.

d)

Use element.delete() to remove an element.

10.

What is the purpose of the finally block in a try-catch statement?

a)

The finally block is optional and can be omitted without consequences.

b)

The finally block is used to catch exceptions only.

c)

The finally block is executed only if an exception occurs.

d)

The purpose of the finally block is to execute code after try and catch, ensuring cleanup actions are performed regardless of exceptions.