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 the purpose of an `else if` 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
An `else if` statement is used to control the flow of code based on multiple conditions.
2. What will be the result of the following code?
var x = 2;
if (x > 3)
{
console.log("Hello, world!");
} else if (x < 1)
{
console.log("Goodbye, world!");
} else {
console.log("Hi there, world!");
}
var x = 2;
if (x > 3)
{
console.log("Hello, world!");
} else if (x < 1)
{
console.log("Goodbye, world!");
} else {
console.log("Hi there, world!");
}
- "Hello, world!" will be logged
- "Goodbye, world!" will be logged
- "Hi there, world!" will be logged
- None of the above
Since neither the first `if` nor the `else if` condition is true, the `else` block will be executed, logging "Hi there, world!"
3. Which loop in JavaScript is used to iterate over the properties of an object?
- for loop
- while loop
- for...of loop
- for...in loop
The `for...in` loop is used to iterate over the properties of an object.
4. What does the `break` statement do in JavaScript?
- It continues to the next iteration of a loop
- It exits the current loop or switch statement
- It defines a function
- It creates a new object
The `break` statement exits the current loop or switch statement.
5. What is the purpose of a `for` loop in JavaScript?
- To declare a variable
- To create a function
- To control the flow of code based on a condition
- To repeatedly execute a block of code a specific number of times
A `for` loop is used to repeatedly execute a block of code a specific number of times.
6. Which keyword is used to declare a loop in JavaScript?
- loop
- repeat
- for
- iterate
The `for` keyword is used to declare a loop in JavaScript.
7. What will be the result of the following `for` loop?
for (var i = 0; i < 5; i++)
{
console.log(i);
}
for (var i = 0; i < 5; i++)
{
console.log(i);
}
- 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 `for` loop will iterate from 0 to 4 and log each value.
8. What is the purpose of a `while` loop in JavaScript?
- To declare a variable
- To create a function
- To control the flow of code based on a condition
- To repeatedly execute a block of code as long as a condition is true
A `while` loop is used to repeatedly execute a block of code as long as a condition is true.
9. Which keyword is used to start a `while` loop in JavaScript?
- loop
- start
- while
- repeat
The `while` keyword is used to start a `while` loop in JavaScript.
10. What does the `continue` statement do in JavaScript?
- It exits the current loop or switch statement
- It continues to the next iteration of a loop
- It defines a function
- It creates a new object
The `continue` statement continues to the next iteration of a loop.
11. What is the purpose of a `do...while` loop in JavaScript?
- To declare a variable
- To create a function
- To control the flow of code based on a condition
- To repeatedly execute a block of code at least once and then as long as a condition is true
A `do...while` loop is used to repeatedly execute a block of code at least once and then as long as a condition is true.
12. Which keyword is used to start a `do...while` loop in JavaScript?
- loop
- start
- do
- while
The `do` keyword is used to start a `do...while` loop in JavaScript.
13. What will be the result of the following `do...while` loop?
var i = 0;
do {
console.log(i);i++;
} while (i < 5);
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.
14. 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.
15. 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.
16. 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.
17. 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.");
}
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."
18. 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.
19. 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.");
}
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."
20. 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.
21. What is the purpose of an `if` statement in JavaScript?
- To declare a variable
- To create a function
- To control the flow of code based on a condition
- To define an object
An `if` statement is used to control the flow of code based on a condition.
22. Which keyword is used to start an `if` statement in JavaScript?
- then
- do
- if
- start
The `if` keyword is used to start an `if` statement.
23. What will be the result of the following code?\
var x = 5;
if (x > 3)
{
console.log("Hello, world!");
}
var x = 5;
if (x > 3)
{
console.log("Hello, world!");
}
- "Hello, world!" will be logged
- "Hello, world!" will not be logged
- An error will occur
- x will be set to 3
Since `x` is greater than 3, "Hello, world!" will be logged.
24. What is the purpose of an `else` statement in JavaScript?
- To declare a variable
- To create a function
- To define an object
- To specify code to run when the `if` condition is false
An `else` statement specifies code to run when the `if` condition is false.
25. What will be the result of the following code?
var x = 2;
if (x > 3)
{
console.log("Hello, world!");
} else {
console.log("Goodbye, world!");
}
var x = 2;
if (x > 3)
{
console.log("Hello, world!");
} else {
console.log("Goodbye, world!");
}
- "Hello, world!" will be logged
- "Goodbye, world!" will be logged
- Both "Hello, world!" and "Goodbye, world!" will be logged
- An error will occur
Since `x` is not greater than 3, "Goodbye, world!" will be logged.