MCQs on Loops and Iterations | JavaScript

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

  1. 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++)
  2. How many times will the following loop execute?




  1. for (let i = 0; i < 5; i++) { console.log(i); }
    • A) 6 times
    • B) 5 times
    • C) 4 times
    • D) Infinite times
  2. 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
  3. Which statement will exit a for loop immediately?
    • A) skip
    • B) exit
    • C) continue
    • D) break
  4. 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
  5. In a nested for loop, which loop runs first?
    • A) Outer loop
    • B) Inner loop
    • C) Both simultaneously
    • D) Neither, they run independently
  6. 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
  7. 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

  1. 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
  2. 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); }
  3. 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
  4. 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
  5. Which statement will exit a while loop immediately?
    • A) skip
    • B) exit
    • C) continue
    • D) break
  6. 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
  7. 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
  8. 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

  1. 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
  2. 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
  3. 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
  4. 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;
  5. If the do...while condition is false initially, how many times will the loop run?
    • A) Zero
    • B) Infinite
    • C) Once
    • D) Twice
  6. 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
  7. 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
  8. 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

  1. 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
  2. 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); }
  3. 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
  4. Which data structure cannot be directly iterated using a for...of loop?
    • A) Set
    • B) Map
    • C) String
    • D) Object
  5. 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
  6. 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

QnoAnswer
1C) for (let i = 0; i < 10; i++)
2B) 5 times
3C) Increment the loop variable
4D) break
5B) Counts down from 10 to 0
6A) Outer loop
7B) It runs until manually stopped
8B) Initialize the loop variable
9C) The condition must be checked before each iteration
10B) while (i < 10) { console.log(i); }
11C) Does not execute at all
12A) Run forever
13D) break
14B) Skips the remaining code in the current iteration
15B) Define the stopping point
16C) break
17B) Condition placement
18C) After each iteration
19B) The loop will execute at least once
20B) do { console.log(i); } while (i < 10);
21C) Once
22B) When you need the loop to run at least once
23B) Exits the loop immediately
24C) The loop could run forever
25C) Iterate over array elements
26A) for (let i of array) { console.log(i); }
27A) Iterating over strings and arrays
28D) Object
29C) The value of each element
30B) Each character in the string

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top