Loops and iterations are fundamental concepts in programming, allowing repetitive tasks to be automated efficiently. This chapter explores various loop constructs, including for, while, do...while, and for...of loops. Test your understanding with these targeted multiple-choice questions (MCQs) covering each loop type in detail.
Multiple Choice Questions
1. For Loop
Which of the following is a correct syntax for a for loop in JavaScript?
A) for { i=0; i < 10; i++ }
B) for i = 0 to 10
C) for (let i = 0; i < 10; i++)
D) for (i < 10; i++)
How many times will the following loop execute?
for (let i = 0; i < 5; i++) { console.log(i); }
A) 6 times
B) 5 times
C) 4 times
D) Infinite times
In a for loop, the expression i++ is typically used to:
A) Reset the loop variable
B) Skip to the next iteration
C) Increment the loop variable
D) Decrement the loop variable
Which statement will exit a for loop immediately?
A) skip
B) exit
C) continue
D) break
If a loop is written as for (let i = 10; i >= 0; i--), it:
A) Counts up from 0 to 10
B) Counts down from 10 to 0
C) Runs forever
D) Counts up to a negative value
In a nested for loop, which loop runs first?
A) Outer loop
B) Inner loop
C) Both simultaneously
D) Neither, they run independently
What will happen if the condition in a for loop is always true?
A) It runs only once
B) It runs until manually stopped
C) It skips all iterations
D) It terminates immediately
The initial expression in a for loop is used to:
A) Set a condition
B) Initialize the loop variable
C) Control iteration speed
D) Define the stopping condition
2. While Loop
The while loop is used when:
A) You know the exact number of iterations
B) The loop must run at least once
C) The condition must be checked before each iteration
D) The condition is unknown
Which syntax correctly implements a while loop in JavaScript?
A) while i < 10 { console.log(i); }
B) while (i < 10) { console.log(i); }
C) while i = 10: console.log(i);
D) while { i < 10; console.log(i); }
If the condition in a while loop is always false, the loop:
A) Executes at least once
B) Runs indefinitely
C) Does not execute at all
D) Executes only once
What will while (true) do if there’s no break statement in the loop?
A) Run forever
B) Run once
C) Stop immediately
D) Throw an error
Which statement will exit a while loop immediately?
A) skip
B) exit
C) continue
D) break
The continue statement in a while loop:
A) Stops the loop
B) Skips the remaining code in the current iteration
C) Repeats the current iteration
D) Terminates the entire loop
The purpose of a while loop’s condition is to:
A) Control the number of iterations
B) Define the stopping point
C) Initialize the loop variable
D) Restart the loop
Which keyword is necessary to ensure that a while loop stops at a certain condition?
A) continue
B) return
C) break
D) pause
3. Do…While Loop
The main difference between a while loop and a do...while loop is:
A) Initialization requirements
B) Condition placement
C) Syntax structure
D) Execution frequency
In a do...while loop, the condition is checked:
A) Before executing the loop
B) Only once
C) After each iteration
D) Every two iterations
What is guaranteed in a do...while loop?
A) The loop will not execute if the condition is false
B) The loop will execute at least once
C) The loop will execute only once
D) The loop will run twice
Choose the correct syntax for a do...while loop in JavaScript:
A) do { console.log(i); } until (i < 10);
B) do { console.log(i); } while (i < 10);
C) do (console.log(i)) while (i < 10);
D) do console.log(i); while i < 10;
If the do...while condition is false initially, how many times will the loop run?
A) Zero
B) Infinite
C) Once
D) Twice
What is a valid purpose for using a do...while loop over a while loop?
A) When you need a flexible loop condition
B) When you need the loop to run at least once
C) When you know the exact number of iterations
D) When you want the condition checked before the first iteration
In a do...while loop, the break statement:
A) Skips the next iteration
B) Exits the loop immediately
C) Restarts the loop
D) Modifies the loop condition
If a do...while loop is set to run until i > 10, what is true?
A) The loop will run 10 times
B) The loop will run until i reaches 10
C) The loop could run forever
D) The loop condition is always false
4. For…of Loop
A for...of loop is mainly used to:
A) Iterate over numbers
B) Iterate over object properties
C) Iterate over array elements
D) Iterate without conditions
Which of the following is a correct syntax for a for...of loop?
A) for (let i of array) { console.log(i); }
B) for (array of i) { console.log(i); }
C) for (let i in array) { console.log(i); }
D) for (array in i) { console.log(i); }
The for...of loop is best suited for:
A) Iterating over strings and arrays
B) Iterating over object keys
C) Conditional iteration
D) Infinite iteration
Which data structure cannot be directly iterated using a for...of loop?
A) Set
B) Map
C) String
D) Object
What does the for...of loop return for each iteration?
A) The index of each element
B) The key of each element
C) The value of each element
D) A boolean for each element
If you use for...of to loop over a string, what will be iterated?
A) Each word in the string
B) Each character in the string
C) Each sentence in the string
D) Each paragraph in the string
Answer Key
Qno
Answer
1
C) for (let i = 0; i < 10; i++)
2
B) 5 times
3
C) Increment the loop variable
4
D) break
5
B) Counts down from 10 to 0
6
A) Outer loop
7
B) It runs until manually stopped
8
B) Initialize the loop variable
9
C) The condition must be checked before each iteration
10
B) while (i < 10) { console.log(i); }
11
C) Does not execute at all
12
A) Run forever
13
D) break
14
B) Skips the remaining code in the current iteration