Loops and Iteration MCQs

1. for Loop

The for loop is ideal when the number of iterations is known ahead of time. It has three parts: initialization, condition, and increment.

for (let i = 0; i < 5; i++) {
  console.log("Iteration number: " + i);
}
  • Initialization: runs once before the loop starts (let i = 0)
  • Condition: checked before every iteration (i < 5)
  • Increment: updates the counter after each loop (i++)

Use Case: Counting loops, working with arrays by index.


2. while Loop

The while loop is used when the number of iterations is not known and depends on a dynamic condition. The loop runs as long as the condition is true.

let i = 0;
while (i < 5) {
  console.log("Value: " + i);
  i++;
}

Important: Make sure to update variables inside the loop to avoid infinite loops.

Use Case: Waiting for a condition to become false (e.g., user input, sensor data).


3. do...while Loop

The do...while loop runs the code block once before checking the condition. It’s guaranteed to execute at least one time.

let i = 10;
do {
  console.log("i is: " + i);
  i++;
} while (i < 5);

Use Case: When the block must run at least once, such as showing a menu until the user chooses to exit.


4. for...of Loop

for...of is used to iterate over iterable objects like arrays, strings, maps, sets, etc.

let colors = ["red", "green", "blue"];
for (let color of colors) {
  console.log(color);
}

Use Case: Cleaner syntax for iterating over elements without needing an index.


5. for...in Loop

for...in is used to iterate over the enumerable properties of an object (keys).

let person = { name: "John", age: 30 };
for (let key in person) {
  console.log(key + ": " + person[key]);
}

Use Case: Useful for looping through object properties, but not recommended for arrays.


6. break and continue

  • break: exits the loop immediately.
  • continue: skips the current iteration and moves to the next.
for (let i = 0; i < 10; i++) {
  if (i === 5) break;        // loop ends at 5
  if (i % 2 === 0) continue; // skip even numbers
  console.log(i);
}

7. Looping through Arrays

Common ways to loop through arrays:

Using for:

let nums = [10, 20, 30];
for (let i = 0; i < nums.length; i++) {
  console.log(nums[i]);
}

Using for...of:

for (let num of nums) {
  console.log(num);
}

Using forEach():

nums.forEach(function(num) {
  console.log(num);
});

Note: forEach is a method, not a loop keyword, but is commonly used for iteration.


Summary Table

Loop Type When to Use Can Use break/continue?
for Known number of iterations Yes
while Unknown iteration count Yes
do...while Always run once Yes
for...of Iterating over values in iterable Yes
for...in Iterating over object keys Yes

Loops and Iteration

1. Which loop is best suited for iterating a known number of times?

2. What will be the output of the following code?


    for (let i = 1; i <= 5; i++) {
        console.log(i);
    }
    

3. What is the key difference between a 'while' loop and a 'do-while' loop?

4. What will be the output of the following code?


    const arr = [1, 2, 3];
    for (const num of arr) {
        console.log(num);
    }
    

5. What is the purpose of the 'for...in' loop in JavaScript?

6. What will be the output of the following code?


    let i = 0;
    while (i < 5) {
        console.log(i);
        i++;
    }
    

7. Which loop is generally considered more versatile for iterating over arrays?

8. What will be the output of the following code?


    const fruits = ['apple', 'banana', 'cherry'];
    for (let i in fruits) {
        console.log(i);
    }
    

9. What does the 'break' statement do in a loop?

10. What will be the output of the following code?


    let sum = 0;
    for (let i = 0; i <= 10; i++) {
        if (i % 2 === 0) {
            sum += i;
        }
    }
    console.log(sum);
    

11. What does the 'continue' statement do in a loop?

12. What will be the output of the following code?


    for (let i = 0; i <= 10; i++) {
        if (i % 2 === 1) continue;
        console.log(i);
    }
    

13. Which loop is primarily used for iterating over iterable objects such as arrays?

14. What will be the output of the following code?


    const obj = {a: 1, b: 2, c: 3};
    for (let key in obj) {
        console.log(key);
    }
    

15. Which of the following is true about the 'for...of' loop?

16. What will be the output of the following code?


    let count = 0;
    do {
        console.log(count);
        count++;
    } while (count < 5);
    

17. What is the primary purpose of using loops in programming?

18. What will be the output of the following code?


    const nums = [10];
    for (let num of nums) {
        console.log(num);
    }
    

19. What does the 'for...in' loop iterate over in JavaScript?

20. What will be the output of the following code?


    const arr = [1, 2, 3];
    for (let value of arr) {
        console.log(value);
    }
    

Post a Comment

Your comment will be visible after approval.
© TechTestLog. All rights reserved. Premium By Raushan Design
//My script