20. Which method is used to add an event listener to an element in JavaScript?
- attachEvent()
- addEventListener()
- bindEvent()
- bindListener()
21. What is the purpose of the preventDefault() method in JavaScript?
- It is used to prevent the default behavior of an event.
- It is used to prevent the execution of a function.
- It is used to prevent the propagation of an event.
- It is used to prevent the bubbling of an event.
22. Which statement is used to define a JavaScript function?
- createFunction()
- defineFunction()
- function
- newFunction()
23. What is the scope of a variable declared inside a function in JavaScript?
- Global scope
- Function scope
- Block scope
- Window scope
24. What is the purpose of the return statement in a JavaScript function?
- It is used to terminate the execution of a function and return a value.
- It is used to define the input parameters of a function.
- It is used to assign a value to a variable within a function.
- It is used to catch and handle errors or exceptions in a function.
25. What is the purpose of the getElementById() method in JavaScript?
- It is used to get the value of an input element.
- It is used to get the text content of an HTML element.
- It is used to get a reference to an HTML element based on its ID.
- It is used to get the class name of an HTML element.
26. What is the purpose of the innerHTML property in JavaScript?
- It is used to retrieve the CSS styles applied to an element.
- It is used to modify the ID of an HTML element.
- It is used to get or set the HTML content within an element.
- It is used to retrieve the value of an input element.
27. What is the purpose of the catch block in a try-catch-finally statement?
- It defines the code to be executed if the try block encounters an error or exception.
- It defines the code to be executed if the finally block encounters an error or exception.
- It defines the code to be executed unconditionally after the try block.
- It defines the code to be executed before the try block.
28. Which method is used to add a CSS class to an HTML element using JavaScript?
- addStyleClass()
- setClass()
- modifyClass()
- classList.add()
29. What is the purpose of the throw statement in JavaScript?
- It is used to terminate the execution of a function or loop.
- It is used to define a custom error message.
- It is used to catch and handle errors or exceptions.
- It is used to throw a user-defined error or exception.