Chapter 4
Chapter 4
MCQ 2:
Question: Where is JavaScript code typically executed?
A) On the server
B) In the user's browser
C) In the database
D) On a mobile app
Answer: B) In the user's browser
MCQ 3:
Question: Which tag is used to include external JavaScript files in HTML?
A) <script>
B) <link>
C) <style>
D) <js>
Answer: A) <script>
MCQ 4:
Question: What is the purpose of the document.write() function in JavaScript?
MCQ 5:
Question: How do you declare a variable in JavaScript?
A) var variableName;
B) let variableName;
C) const variableName;
D) All of the above
Answer: D) All of the above
MCQ 6:
Question: What is the purpose of the console.log() function in JavaScript?
MCQ 7:
Question: Which operator is used to assign a value to a variable in JavaScript?
A) =
B) ==
C) ===
D) !=
Answer: A) =
MCQ 8:
Question: What does the typeof operator return in JavaScript?
MCQ 9:
Question: Which loop is used for iterating over the properties of an object in
JavaScript?
A) for loop
B) for...in loop
C) while loop
D) do...while loop
Answer: B) for...in loop
MCQ 10:
Question: What is the purpose of the addEventListener() method in JavaScript?
MCQ 11:
Question: How do you create a function in JavaScript?
MCQ 12:
Question: What is the purpose of the return statement in a JavaScript function?
MCQ 13:
Question: Which method is used to access elements by their ID in JavaScript?
A) getElementById()
B) querySelector()
C) getElementsByClassName()
D) getElementsByTagName()
Answer: A) getElementById()
MCQ 14:
Question: What is the purpose of the this keyword in JavaScript?
MCQ 15:
Question: How do you create an array in JavaScript?
MCQ 16:
Question: Which method is used to add an element to the end of an array in
JavaScript?
A) push()
B) pop()
C) shift()
D) unshift()
Answer: A) push()
MCQ 17:
Question: What is the purpose of the JSON.parse() method in JavaScript?
MCQ 18:
Question: Which event occurs when the user clicks on an HTML element?
A) onmouseover
B) onclick
C) onload
D) onsubmit
Answer: B) onclick
MCQ 19:
Question: What is the purpose of the preventDefault() method in JavaScript?
MCQ 20:
Question: How do you create a new object in JavaScript?
MCQ 21:
Question: Which method is used to remove the last element from an array in
JavaScript?
A) pop()
B) push()
C) shift()
D) unshift()
Answer: A) pop()
MCQ 22:
Question: What is the purpose of the Math.random() function in JavaScript?
MCQ 23:
Question: How do you check if a variable is an array in JavaScript?
A) Array.isArray(variable)
B) typeof variable === 'array'
C) variable instanceof Array
D) Both A and C
Answer: D) Both A and C
MCQ 24:
Question: Which method is used to convert all the characters in a string to
uppercase in JavaScript?
A) toUpperCase()
B) toLowerCase()
C) charAt()
D) substr()
Answer: A) toUpperCase()
MCQ 25:
Question: What is the purpose of the setInterval() function in JavaScript?
MCQ 26:
Question: Which method is used to remove the first element from an array in
JavaScript?
A) shift()
B) unshift()
C) pop()
D) push()
Answer: A) shift()
MCQ 27:
Question: What is the purpose of the localStorage object in JavaScript?
MCQ 28:
Question: How do you create a promise in JavaScript?
MCQ 29:
Question: Which method is used to convert a string to an integer in JavaScript?
A) parseInt()
B) parseFloat()
C) toString()
D) Number()
Answer: A) parseInt()
MCQ 30:
Question: What is the purpose of the fetch() function in JavaScript?