JavaScript Basics - variables, loops, arrays, functions, events, DOM interactions, alert-confirm-prompt, rollover images - Question Bank

1. What is the primary advantage of using `let` and `const` over `var` in modern JavaScript?
A) They are significantly faster.
B) They provide block-scoping, reducing potential errors.
C) They allow for the declaration of global variables more easily.
D) They are required for all older browsers.
2. Which statement best describes the `prompt()` function's return value when the user clicks 'Cancel'?
A) An empty string
B) null
C) undefined
D) false
3. What is the `event.preventDefault()` method used for?
A) To stop an event from bubbling up the DOM tree
B) To prevent the default action of an element (e.g., following a link)
C) To stop the execution of JavaScript code
D) To make an event handler execute multiple times
4. To remove an HTML element using JavaScript, you can use:
A) element.remove()
B) element.delete()
C) element.hide()
D) element.destroy()
5. Which DOM method returns an HTMLCollection of all elements with a given class name?
A) getElementsByTagName()
B) querySelector()
C) getElementById()
D) getElementsByClassName()
6. What event occurs when the value of an input element changes and the element loses focus?
A) onchange
B) oninput
C) onclick
D) onblur
7. What is a closure in JavaScript (related to functions)?
A) A way to declare variables outside functions
B) A function's ability to access variables from its outer scope even after the outer function has finished executing
C) A method to close a web page
D) A type of loop that terminates immediately
8. Which loop is commonly used for iterating over the properties of an object?
A) for loop
B) while loop
C) for...in loop
D) forEach loop
9. What is the output of `console.log(2 + 3 + '5')`?
A) 10
B) '55'
C) '235'
D) Error
10. Which statement about JavaScript variables is true?
A) Variables declared with `const` can be reassigned.
B) Variables declared with `let` have global scope.
C) Variables declared with `var` are hoisted.
D) All variables must be declared before use.
11. What is the primary use of `alert()`, `confirm()`, and `prompt()`?
A) Styling web page elements
B) Manipulating the DOM structure
C) Providing simple user interaction and feedback
D) Performing complex calculations
12. How do you add a new attribute to an HTML element using JavaScript?
A) element.setAttribute('attributeName', 'value')
B) element.addAttribute('attributeName', 'value')
C) element.newAttribute('attributeName', 'value')
D) element.attr = 'value'
13. Which DOM method is used to select all elements with a specific tag name?
A) getElementById()
B) getElementsByClassName()
C) getElementsByTagName()
D) querySelector()
14. What is the event triggered when a user presses a key on the keyboard?
A) onclick
B) onchange
C) onkeydown
D) onfocus
15. What is an anonymous function in JavaScript?
A) A function that has no parameters
B) A function that is not declared with a name
C) A function that returns no value
D) A function that can only be called once
16. Which of the following is a way to declare a function expression in JavaScript?
A) function myFunction() {}
B) const myFunction = function() {}
C) define function myFunction {}
D) new Function('return true;')
17. What is the scope of a variable declared with `var` inside a function?
A) Block scope
B) Global scope
C) Function scope
D) Local to the block
18. What is the value of `fruits.length` for the array `let fruits = ['apple', 'banana', 'cherry'];`?
A) 2
B) 3
C) 4
D) undefined
19. Consider the array `let fruits = ['apple', 'banana', 'cherry'];`. What will `fruits[1]` return?
A) 'apple'
B) 'banana'
C) 'cherry'
D) undefined
20. Which loop is guaranteed to execute at least once?
A) for loop
B) while loop
C) do...while loop
D) forEach loop
21. What is the purpose of the `while` loop in JavaScript?
A) To execute a block of code a specific number of times
B) To execute a block of code as long as a specified condition is true
C) To execute a block of code at least once, and then repeat as long as a condition is true
D) To iterate over object properties
22. To implement a rollover image, you typically need to:
A) Use only CSS
B) Use JavaScript to change the `src` attribute of the `<img>` tag on mouse events
C) Use the `<canvas>` element
D) Embed a video file
23. What is a 'rollover image' technique in web development?
A) An image that automatically scrolls down the page
B) An image that changes when the mouse pointer hovers over it
C) An image that loads slowly
D) An image that is displayed in a circular shape
24. What does `document.querySelector('.my-class')` do?
A) Selects all elements with the class 'my-class'
B) Selects the first element with the ID 'my-class'
C) Selects the first element with the class 'my-class'
D) Selects the first element with the tag name 'my-class'
25. How can you change the text content of an HTML element using JavaScript?
A) element.innerHTML = 'New Text'
B) element.textContent = 'New Text'
C) element.innerText = 'New Text'
D) All of the above
26. Which DOM method is used to select an HTML element by its ID?
A) getElementsByTagName()
B) getElementsByClassName()
C) querySelector()
D) getElementById()
27. What is the purpose of `addEventListener()`?
A) To remove an event listener from an element
B) To attach an event handler to an element
C) To define a new HTML element
D) To create a new JavaScript variable
28. Which event listener is triggered when a user clicks on an HTML element?
A) onmouseover
B) onclick
C) onkeydown
D) onsubmit
29. What is an argument in a JavaScript function?
A) The name of the function
B) A variable declared inside the function
C) The actual value passed to the function when it is called
D) The keyword used to define the function
30. What is a parameter in a JavaScript function?
A) The value returned by the function
B) A variable listed inside the parentheses in the function definition
C) A variable declared outside the function
D) The name of the function
31. In JavaScript, a function can:
A) Only return a number
B) Only perform an action, not return a value
C) Return any data type, including other functions
D) Only be called once
32. What is the `unshift()` array method used for?
A) Removing the first element
B) Adding elements to the end
C) Adding elements to the beginning
D) Removing the last element
33. What does the `shift()` array method do?
A) Removes the last element and returns it
B) Adds an element to the end of the array
C) Removes the first element and returns it
D) Adds an element to the beginning of the array
34. What is the JavaScript method to remove the last element from an array?
A) pop()
B) shift()
C) removeLast()
D) deleteLast()
35. How do you add an element to the end of a JavaScript array?
A) array.unshift(element)
B) array.push(element)
C) array.add(element)
D) array.append(element)
36. What is the result of `console.log(typeof 'Hello')`?
A) 'Hello'
B) 'string'
C) 'undefined'
D) 'object'
37. Which of the following is NOT a valid JavaScript data type?
A) String
B) Number
C) Boolean
D) Character
38. What does the `const` keyword do in JavaScript?
A) Declares a variable that can be reassigned
B) Declares a variable that cannot be reassigned
C) Declares a constant function
D) Declares a global variable
39. What is the difference between `var` and `let` when declaring variables?
A) `let` variables are global, `var` variables are local
B) `var` has function scope or global scope, `let` has block scope
C) `let` variables cannot be reassigned, `var` variables can
D) There is no functional difference
40. Which loop is most suitable for iterating over the elements of an array when the number of iterations is known?
A) while loop
B) do...while loop
C) for loop
D) for...in loop
41. What is the basic syntax for accessing an element in a JavaScript array by its index?
A) arrayName.getElement(index)
B) arrayName[index]
C) arrayName.value(index)
D) arrayName.at(index)
42. The `prompt()` function in JavaScript is used to:
A) Display a message and ask for user confirmation
B) Display a message and get text input from the user
C) Display a simple informational message
D) Execute a piece of code based on a condition
43. What is the purpose of the `confirm()` function in JavaScript?
A) To display a message and ask for user input
B) To display a message with OK and Cancel buttons
C) To display a simple message to the user
D) To log messages to the browser console
44. Which JavaScript function displays a message box with an OK button?
A) confirm()
B) prompt()
C) alert()
D) log()
45. What does 'DOM' stand for in web development?
A) Document Object Model
B) Data Object Model
C) Document Oriented Markup
D) Digital Object Management
46. What is an 'event' in the context of JavaScript and web development?
A) A predefined JavaScript function
B) A user action or system occurrence that can be detected by JavaScript
C) A variable used to store data
D) A block of code that executes repeatedly
47. Which keyword is used to define a function in JavaScript?
A) fun
B) function
C) define
D) method
48. In JavaScript, what is an array?
A) A single value of any data type
B) A collection of related variables
C) A special type of variable that can hold multiple values under a single name
D) A function that performs a specific task
49. What is the primary purpose of a `for` loop in JavaScript?
A) To execute a block of code once if a condition is true
B) To repeat a block of code until a condition is met
C) To iterate over a sequence of elements a specified number of times
D) To exit a loop prematurely
50. Which keyword is used to declare a variable in JavaScript that can be reassigned?
A) const
B) let
C) var
D) static