JavaScript Quiz - Multiple Choice Questions (MCQ)

In this blog post, we present a JavaScript quiz with 50+ multiple-choice questions. Each question is followed by its correct answer and an explanation. 

Let's dive in and see how well you know JavaScript! 

1. What is the correct way to declare a variable in JavaScript? 

a) 

variable x = 10;

b) 

var x = 10; 

c)

 x = 10; 

d) 

int x = 10; 

2. What is the correct JavaScript syntax to modify the content of the HTML element shown below?

<p id="demo">Welcome to JavaScript Quiz</p>
a) 
document.getElementById("demo").innerHTML = "Hello World!";
b) 
demo.innerHTML = "Hello World!"; 
c) 
document.getElement("p").innerHTML = "Hello World!";
d)
document.getElementByName("p").innerHTML = "Hello World!"; 

3. What will be the output of the following code snippet?

console.log(2 + "2");
a) 4 
b) 22 
c) "22" 
d) "4" 

4. How do you check if a variable is an array in JavaScript? 

a) isArray(x) 
b) x.isArray() 
c) x instanceof Array 
d) typeof Array 

5. How do you properly comment on a single line in JavaScript? 

a) # This is a comment. 
b) // This is a comment. 
c) /* This is a comment. */ 
d) <!-- This is a comment. --> 

6. Where is the correct place to insert JavaScript code in an HTML document?

a) Both the <head> section and the <body> section
b) The <head> section 
c) The <body> section 
d) Anywhere in the HTML file

7. What are the main types of loops available in JavaScript? 

a) for loop, do...while loop, while loop
b) for loop, do...until loop, while loop
c) for loop, while loop, repeat...until loop
d) for loop, do...while loop, repeat...until loop 

8. Which loop is guaranteed to execute the block of code at least once? 

a) for loop 
b) do...while loop 
c) while loop 
d) none

9. What is the purpose of the break statement in a loop? 

a) It stops the execution of the loop immediately. 
b) It skips the current iteration and moves to the next one. 
c) It restarts the loop from the beginning. 
d) It returns a value from the loop. 

10. Which loop is ideal when you want to iterate over an array in JavaScript? 

a) for loop 
b) do...while loop 
c) while loop 
d) repeat...until loop 

11. What is the key difference between the while loop and the do...while loop? 

a) The while loop executes the code block first, then checks the condition. The do...while loop checks the condition first, then executes the code block. 
b) The while loop always executes the code block at least once. The do...while loop checks the condition first and may skip the code block entirely. 
c) The while loop is more efficient for larger datasets. The do...while loop is more efficient for smaller datasets. 
d) There is no difference between the two; they can be used interchangeably. 

12. What is the purpose of the continue statement in a loop? 

a) To exit the loop and stop execution immediately. 
b) To skip the current iteration and proceed to the next one. 
c) To restart the loop from the beginning. 
d) To return a value from the loop. 

13. Which operator is used to combine two or more strings in JavaScript? 

a) && 
b) || 
c) + 
d) - 

14. Which comparison operator checks for both value and type equality in JavaScript? 

a) == 
b) === 
c) = 
d) <=> 

15. How do you create an empty array in JavaScript? 

a) 
let arr = new Array(); 
b) 
let arr = []; 
c) 
let arr = {}; 
d) 
let arr = null; 

16. What is the index of the first element in an array? 

a) 0 
b) 1 
c) -1 
d) 10 

17. Which method do you use to add elements to the end of an array? 

a) add() 
b) push() 
c) append() 
d) concat() 

18. How do you remove the last element from an array? 

a) removeLast() 
b) deleteLast() 
c) pop() 
d) splice() 

19. What will be the output of the following code snippet?

let fruits = ['apple', 'banana', 'orange'];
fruits.pop();
console.log(fruits.length);
a) 0 
b) 1 
c) 2 
d) 3 

20. How do you check if an element exists in an array? 

a) Using the check() method 
b) Using the search() method 
c) Using the includes() method 
d) Using the exists() method 

21. What method do you use to join all elements of an array into a single string? 

a) join() 
b) concat() 
c) merge() 
d) combine() 

22. How do you remove elements from an array, starting from a specific index? 

a) removeFromIndex() 
b) splice() 
c) cut() 
d) deleteFromIndex() 

23. How do you find the index of a specific element in an array? 

a) findIndex() 
b) indexOf() 
c) searchIndex() 
d) getElementIndex() 

24. What method do you use to create a new array by applying a function to each element in the existing array?

a) apply() 
b) map() 
c) transform() 
d) forEach()

25. How do you define a function in JavaScript? 

a) 
function myFunction() {} 
b) 
let myFunction = function() {};
c) 
const myFunction = () => {};
d) All of the above 

26. What keyword is used to return a value from a JavaScript function? 

a) value 
b) result 
c) return 
d) output 

27. How do you call a JavaScript function named myFunction? 

a) call myFunction(); 
b) run myFunction(); 
c) myFunction(); 
d) execute myFunction(); 

28. What is a callback function in JavaScript? 

a) A function that performs asynchronous tasks. 
b) A function that is called at the end of the program's execution. 
c) A function that is passed as an argument to another function and is executed inside that function. 
d) A function that is used for error handling. 

29. What is the primary goal of Object-Oriented Programming (OOP) in JavaScript? 

a) To make the code shorter and more concise. 
b) Organize code into classes and objects. 
c) To execute code faster than procedural programming. 
d) To eliminate the need for functions. 

30. What is a class in JavaScript? 

a) A built-in object provided by the JavaScript runtime. 
b) A blueprint or template for creating objects with shared properties and methods. 
c) A single function used to define the behavior of an object. 
d) A reserved keyword used to declare variables. 

31. How do you create an object from a class in JavaScript? 

a) 
new object(myClass);
b) 
create object myClass;
c)
let obj = new MyClass();  
d) 
let obj = create(myClass);

32. Which keyword is used to refer to the current instance of a class inside its methods? 

a) self 
b) this 
c) it 
d) current 

33. Which keyword is used to call a method defined in the parent class from a child class in JavaScript? 

a) parent 
b) this 
c) base 
d) super 

34. How do you implement inheritance in JavaScript classes? 

a) Using the extends keyword and specifying the parent class. 
b) Using the inherits keyword and specifying the parent class. 
c) Using the super() method to inherit properties from the parent class. 
d) Using the inheritFrom keyword and specifying the parent class. 

35. How do you declare a string variable in JavaScript? 

a)
let str = 'Hello';
b) 
var str = "Hello";
c) 
const str = 'Hello'; 
d) All of the above 

36. What will be the length of the following string?

let myString = "Hello, World!";
a) 11 
b) 12 
c) 13 
d) 14 

37. What method do you use to convert a string to all lowercase letters? 

a) toLowerCase() 
b) toLower() 
c) lowerCase() 
d) convertToLower() 

38. How do you remove leading and trailing whitespace from a string? 

a) trim() 
b) removeWhitespace() 
c) strip() 
d) deleteWhitespace() 

39. Which keyword is used to declare block-scoped variables in ES6? 

a) var 
b) let 
c) const 
d) variable 

40. What is the difference between let and const in JavaScript? 

a) let is used for constant values, while const is used for variables. 
b) let can be reassigned, while const is read-only and cannot be reassigned. 
c) let is block-scoped, while const is function-scoped. 
d) There is no difference; let and const are interchangeable. 

41. How do arrow functions differ from regular functions in ES6? 

a) Arrow functions have shorter syntax than regular functions. 
b) Arrow functions do not have their own this value. 
c) Arrow functions cannot be used as methods in objects. 
d) All of the above 

42. What is the purpose of the ... operator (spread operator) in ES6? 

a) To concatenate arrays. 
b) To destructure objects. 
c) To access the properties of an object. 
d) To spread elements in arrays or objects. 

43. What is the purpose of destructuring assignment in ES6? 

a) To assign a value to a variable. 
b) To split an array or object into individual variables. 
c) To access the properties of an object. 
d) To define default values for variables. 

44. How do you add an event listener to an HTML element in JavaScript? 

a) 
element.addListener("click", myFunction); 
b) 
element.event("click", myFunction); 
c) 
element.addEventListener("click", myFunction); 
d) 
element.click(myFunction); 

45. What is the purpose of the JSON.stringify() method in JavaScript? 

a) To parse a JSON string and convert it into a JavaScript object. 
b) To convert a JavaScript object or value into a JSON string. 
c) To check if a value is a valid JSON object. 
d) To merge two JSON objects into one. 

46. What is the difference between let and const in ES6? 

a) let is used for constant values, while const is used for variables. 
b) let can be reassigned, while const is read-only and cannot be reassigned. 
c) let is block-scoped, while const is function-scoped. 
d) There is no difference; let and const are interchangeable. 

47. What is the purpose of the const keyword in JavaScript? 

a) To declare a constant value that cannot be changed after initialization. 
b) To declare a variable with block scope. 
c) To define a function in JavaScript. 
d) To store multiple values in an array. 

48. What is the difference between declaring a variable with var and let? 

a) var is block-scoped, while let is function-scoped. 
b) var variables can be reassigned, while let variables cannot. 
c) var variables are limited to the block in which they are defined, while let variables are accessible throughout the entire function. 
d) There is no difference; var and let can be used interchangeably. 

49. What is the purpose of the async keyword in JavaScript? 

a) To declare a function as asynchronous. 
b) To define an array of values. 
c) To create a new object. 
d) To fetch data from a remote server. 

50. What is the purpose of the localStorage object in JavaScript? 

a) To store sensitive user information on the client side. 
b) To fetch data from an external server. 
c) To manage cookies and sessions. 
d) To store data locally on the user's browser. 

Answer: 


I hope you found this quiz helpful and educational. Keep practicing to solidify your understanding of JavaScript programming concepts. Happy coding!

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare