Here are Top 25 multiple-choice questions (MCQs) focused on the HTML5 features and elements in JavaScript MCQs, along with their answers and explanations.
1. What is a JavaScript function?
- A way to declare a variable
- A data type for storing text
- A reusable block of code that performs a specific task
- A way to define a loop
A JavaScript function is a reusable block of code that performs a specific task.
2. Which keyword is used to declare a function in JavaScript?
- func
- method
- define
- function
The function keyword
3. What is a parameter in a JavaScript function?
- A type of object
- A variable that holds a reference to a function
- A named variable passed to a function as an input
- A variable used for counting iterations in a loop
A parameter in a JavaScript function is a named variable passed to the function as an input, allowing data to be passed into the function.
4. What is a JavaScript function's return statement used for?
- To declare a variable
- To define a loop
- To specify code to run when a specific condition is met
- To specify the value that the function should return
The return statement in a JavaScript function is used to specify the value that the function should return when it is called.
5. What is the result of the following JavaScript function? function add(a, b) { return a + b;}var result = add(3, 5);
- result will be assigned the value 8.
- An error will occur.
- result will be assigned the function add.
- The function add will log the result.
The add function returns the sum of its two parameters, and when called with add(3, 5), it returns 8, which is assigned to result.
6. What is a JavaScript object?
- A way to declare a variable
- A function that performs a specific task
- A collection of key-value pairs
- A way to define a loop
A JavaScript object is a collection of key-value pairs, where the keys are strings (or symbols) and the values can be any data type.
7. How do you access a property of a JavaScript object?
- By using the dot notation (e.g., object.property)
- By using square brackets (e.g., object['property'])
- By using parentheses (e.g., object('property'))
- By using backticks (e.g., objectproperty`)
You access a property of a JavaScript object using the dot notation, like object.property.
8. What is a JavaScript constructor function used for?
- To perform mathematical operations
- To create multiple objects with similar properties and methods
- To specify code to run when a specific condition is met
- To define a loop
A constructor function in JavaScript is used to create multiple objects with similar properties and methods.
9. Which keyword is used to create a new instance of an object in JavaScript?
- create
- instance
- new
- object
The new keyword is used to create a new instance of an object in JavaScript.
10. What is the result of the following JavaScript code? function Person(name) { this.name = name;}var person1 = new Person("Alice");var person2 = new Person("Bob");console.log(person1.name);
- "Alice"
- "Bob"
- An error will occur.
- Both "Alice" and "Bob" will be logged.
The person1 object is created with the name "Alice," and console.log(person1.name) logs "Alice."
11. What is a JavaScript method?
- A way to declare a variable
- A data type for storing text
- A function that is a property of an object
- A way to define a loop
A JavaScript method is a function that is a property of an object.
12. How do you call a method of a JavaScript object?
- By using the method keyword
- By using square brackets
- By using parentheses after the method name
- By using backticks
You call a method of a JavaScript object by using parentheses after the method name, like object.method().
13. What is a JavaScript prototype?
- A way to declare a variable
- A built-in function in JavaScript
- An object from which other objects inherit properties
- A way to define a loop
In JavaScript, a prototype is an object from which other objects inherit properties.
14. How do you add a method to a JavaScript object's prototype?
- By using the addMethod keyword
- By defining the method directly within the object
- By using the prototype property of the constructor function
- By using the Object.create() method
You add a method to a JavaScript object's prototype by using the prototype property of the constructor function.
15. What is a closure in JavaScript?
- A way to declare a variable
- A way to define a loop
- A function that is nested within another function and has access to the outer function's variables
- A data type for storing text
A closure in JavaScript is a function that is nested within another function and has access to the outer function's variables.
16. What is the result of the following JavaScript code? function outer() { var x = 10;function inner() {console.log(x); }return inner;}var closure = outer();closure();
- 10 will be logged.
- An error will occur.
- "x is not defined" will be logged.
- "inner" will be logged.
The inner function forms a closure and has access to the x variable from the outer function, so it logs 10.
17. What is the purpose of the call() method in JavaScript?
- To define a loop
- To create a new object
- To execute a function with a specified this value and arguments provided individually
- To create a closure
The call() method is used to execute a function with a specified this value and arguments provided individually.
18. What will be logged to the console in the following JavaScript code? var i = 0; do { console.log(i); i++;} while (i < 5);
- 0, 1, 2, 3, 4 will be logged
- 1, 2, 3, 4, 5 will be logged
- 5, 4, 3, 2, 1 will be logged
- An error will occur
The do...while loop will iterate from 0 to 4 and log each value.
19. What is the purpose of a switch statement in JavaScript?
- To declare a variable
- To create a function
- To control the flow of code based on multiple conditions
- To define an object
A switch statement is used to control the flow of code based on multiple conditions.
20. Which keyword is used to start a switch statement in JavaScript?
- switch
- case
- select
- condition
The switch keyword is used to start a switch statement in JavaScript.
21. What is the purpose of a case statement in a switch statement in JavaScript?
- To declare a variable
- To create a function
- To define an object
- To specify code to run when a specific condition is met
A case statement specifies code to run when a specific condition is met in a switch statement.
22. What will be the result of the following switch statement? var day = "Monday";switch (day) { case "Monday".console.log("It's the start of the week."); break; case "Friday". console.log("It's the end of the week."); break; default. console.log("It's a regular day.");}
- "It's the start of the week." will be logged
- "It's the end of the week." will be logged
- "It's a regular day." will be logged
- All of the above will be logged
The switch statement matches the day variable with the "Monday" case and logs "It's the start of the week."
23. What is the purpose of the default case in a switch statement?
- To declare a variable
- To create a function
- To define an object
- To specify code to run when no other cases match
The default case in a switch statement specifies code to run when no other cases match.
24. What will be the result of the following switch statement? var day = "Wednesday";switch (day) { case "Monday". console.log("It's the start of the week."); break; case "Friday". console.log("It's the end of the week."); break; default. console.log("It's a regular day.");}
- "It's the start of the week." will be logged
- "It's the end of the week." will be logged
- "It's a regular day." will be logged
- None of the above
Since the day variable does not match any case, the default case will be executed, logging "It's a regular day."
25. What is the purpose of the break statement in a switch statement in JavaScript?
- To continue to the next case
- To exit the current loop
- To specify the default case
- To exit the switch statement
The break statement in a switch statement is used to exit the switch statement.