MCQs Of Day 6: Loops in JavaScript

Rashmi Mishra
0

 MCQs Of Day 6: Loops in JavaScript

Here are 100 Multiple Choice Questions (MCQs) on the concept of loops in JavaScript, with explanations for each correct answer:


1. Which loop is guaranteed to execute at least once in JavaScript?

  • a) for loop
  • b) while loop
  • c) do...while loop
  • d) None of the above

Answer: c) do...while loop

Explanation: The do...while loop will execute the code block once before checking the condition. This ensures that the loop body is executed at least once.


2. Which of the following is the correct syntax for a for loop in JavaScript?

  • a) for (i = 0; i < 5; i++) {}
  • b) for (i = 0; i <= 5; i++) {}
  • c) for (i = 1; i < 5; i++) {}
  • d) All of the above

Answer: d) All of the above

Explanation: All of the listed options are valid for loop syntaxes with different initialization and termination conditions.


3. What will the following for loop output?

javascript

Copy code

for (let i = 0; i < 5; i++) {

    console.log(i);

}

  • a) 1 2 3 4 5
  • b) 0 1 2 3 4
  • c) 0 1 2 3 4 5
  • d) 5 4 3 2 1

Answer: b) 0 1 2 3 4

Explanation: The loop starts with i = 0 and executes until i < 5, incrementing by 1 each time.


4. How does a while loop differ from a do...while loop?

  • a) while checks the condition before executing the code block.
  • b) do...while checks the condition after executing the code block.
  • c) while executes the code block at least once.
  • d) Both loops are identical.

Answer: b) do...while checks the condition after executing the code block.

Explanation: In a do...while loop, the code block is executed once before the condition is checked, whereas in a while loop, the condition is checked before execution.


5. What is the correct syntax for a while loop in JavaScript?

  • a) while (condition) {}
  • b) while {condition}
  • c) while(condition) {}
  • d) while: condition {}

Answer: a) while (condition) {}

Explanation: The correct syntax is while (condition) {}. The condition is evaluated before executing the loop's body.


6. What will the following code output?

javascript

Copy code

let i = 0;

while (i < 3) {

    console.log(i);

    i++;

}

  • a) 0 1 2
  • b) 0 1 2 3
  • c) 1 2 3
  • d) Infinite loop

Answer: a) 0 1 2

Explanation: The while loop runs as long as i < 3, printing values 0, 1, and 2 before the loop condition becomes false.


7. What is the output of the following do...while loop?

javascript

Copy code

let i = 5;

do {

    console.log(i);

    i++;

} while (i < 5);

  • a) 5
  • b) 5 6
  • c) 6
  • d) Infinite loop

Answer: a) 5

Explanation: The do...while loop runs at least once, so it prints 5 even though the condition i < 5 is false after the first iteration.


8. Which loop is used when you want to execute a block of code a known number of times?

  • a) for loop
  • b) while loop
  • c) do...while loop
  • d) None of the above

Answer: a) for loop

Explanation: The for loop is ideal when the number of iterations is known beforehand, such as counting from 0 to 10.


9. Which of the following loops is the most appropriate for an indefinite loop?

  • a) for loop
  • b) while loop
  • c) do...while loop
  • d) All of the above

Answer: b) while loop

Explanation: A while loop is appropriate for indefinite loops, where the number of iterations isn't predetermined.


10. What will the following code output?

javascript

Copy code

for (let i = 0; i < 3; i++) {

    for (let j = 0; j < 2; j++) {

        console.log(i, j);

    }

}

  • a) 0 0, 0 1, 1 0, 1 1, 2 0, 2 1
  • b) 0 1, 1 1, 2 1
  • c) 0 0, 1 0, 2 0
  • d) Infinite loop

Answer: a) 0 0, 0 1, 1 0, 1 1, 2 0, 2 1

Explanation: The outer loop runs 3 times, and for each iteration, the inner loop runs twice, printing all combinations of i and j.


11. Which statement will cause an infinite loop?

  • a) while (false) {}
  • b) do {} while (false)
  • c) for (let i = 0; i < 5; i++) {}
  • d) while (true) {}

Answer: d) while (true) {}

Explanation: A while (true) loop is infinite since the condition will always evaluate to true.


12. Which statement can be used to terminate a loop early?

  • a) continue
  • b) exit
  • c) break
  • d) end

Answer: c) break

Explanation: The break statement is used to exit a loop early, skipping any remaining iterations.


13. Which loop would you use if you need to execute a block of code after checking the condition at least once?

  • a) for loop
  • b) while loop
  • c) do...while loop
  • d) All of the above

Answer: c) do...while loop

Explanation: The do...while loop ensures that the code block is executed at least once, even if the condition is initially false.


14. What will this code output?

javascript

Copy code

let i = 0;

while (i < 3) {

    console.log(i);

    i += 1;

}

  • a) 0 1 2
  • b) 0 1 2 3
  • c) Infinite loop
  • d) 1 2 3

Answer: a) 0 1 2

Explanation: The while loop starts at i = 0 and increments i by 1 each time, stopping after i = 2.


15. Which loop is useful for iterating through arrays in JavaScript?

  • a) for loop
  • b) while loop
  • c) do...while loop
  • d) for...of loop

Answer: d) for...of loop

Explanation: The for...of loop is specifically designed for iterating through array elements in JavaScript.


16. How do you skip the current iteration of a loop and continue with the next one?

  • a) exit
  • b) continue
  • c) skip
  • d) break

Answer: b) continue

Explanation: The continue statement is used to skip the current iteration and proceed to the next iteration of the loop.


17. Which of the following loops can be used when the number of iterations is known?

  • a) for loop
  • b) while loop
  • c) do...while loop
  • d) All of the above

Answer: a) for loop

Explanation: The for loop is most commonly used when the number of iterations is known, such as iterating over a fixed range or array.


18. What will the following do...while loop print?

javascript

Copy code

let i = 0;

do {

    i++;

    console.log(i);

} while (i < 3);

  • a) 1 2 3
  • b) 0 1 2 3
  • c) 3 2 1
  • d) Infinite loop

Answer: a) 1 2 3

Explanation: The do...while loop increments i before checking the condition, so the loop prints 1, 2, and 3.


19. What is the output of the following for loop?

javascript

Copy code

for (let i = 2; i <= 8; i += 2) {

    console.log(i);

}

  • a) 2 4 6 8
  • b) 1 3 5 7
  • c) 2 3 4 5 6 7 8
  • d) 1 2 3 4 5 6

Answer: a) 2 4 6 8

Explanation: The for loop starts at i = 2, increments by 2, and prints values 2, 4, 6, and 8 before stopping at i = 8.

4o mini

You said:

more

ChatGPT said:

ChatGPT

Here are more MCQs with explanations for the concept of loops in JavaScript:


20. What is the value of i after the following code executes?

javascript

Copy code

for (let i = 1; i <= 5; i++) {

    if (i === 3) {

        break;

    }

}

console.log(i);

  • a) 1
  • b) 2
  • c) 3
  • d) 5

Answer: c) 3

Explanation: The loop will break when i equals 3, and the value of i will be 3 when the loop exits. The value of i is logged after the loop exits.


21. Which of the following will result in an infinite while loop?

  • a) while (true) {}
  • b) while (1) {}
  • c) while (false) {}
  • d) Both a and b

Answer: d) Both a and b

Explanation: Both conditions true and 1 are always truthy, causing the while loop to run infinitely.


22. Which of the following is the correct way to loop through all elements of an array in JavaScript?

  • a) for (let i = 0; i < array.length; i++) { console.log(array[i]); }
  • b) forEach(array, function(element) { console.log(element); })
  • c) array.forEach(function(element) { console.log(element); })
  • d) All of the above

Answer: d) All of the above

Explanation: All of these methods are correct for looping through the elements of an array in JavaScript. The first is a traditional for loop, the second uses the forEach method explicitly, and the third uses forEach with a callback function.


23. What will the following code output?

javascript

Copy code

let i = 0;

for (i = 0; i < 3; i++) {

    console.log(i);

}

console.log(i);

  • a) 0 1 2 3
  • b) 0 1 2
  • c) 1 2 3
  • d) 0 1 2 0

Answer: a) 0 1 2 3

Explanation: The for loop prints 0, 1, and 2, and after the loop ends, i becomes 3. The final console.log(i) prints 3.


24. How many times will the following loop execute?

javascript

Copy code

for (let i = 0; i < 10; i += 2) {

    console.log(i);

}

  • a) 5
  • b) 10
  • c) 4
  • d) 2

Answer: a) 5

Explanation: The loop starts at i = 0 and increments by 2 each time. The loop will print 0, 2, 4, 6, 8, which is 5 iterations in total.


25. What is the output of the following code?

javascript

Copy code

let i = 0;

while (i < 3) {

    console.log(i);

    i++;

}

  • a) 0 1 2
  • b) 1 2 3
  • c) 0 1 2 3
  • d) Infinite loop

Answer: a) 0 1 2

Explanation: The while loop will run while i is less than 3. It will print 0, 1, and 2 before stopping when i = 3.


26. What does the following do...while loop do?

javascript

Copy code

let count = 0;

do {

    count++;

} while (count < 5);

  • a) Prints count five times.
  • b) Runs the loop while count is less than 5.
  • c) Always prints 5.
  • d) Loops infinitely.

Answer: b) Runs the loop while count is less than 5.

Explanation: The loop will execute at least once because the condition is checked after the first iteration. It increments count until it reaches 5, then stops.


27. Which of the following will break the loop execution?

  • a) exit
  • b) continue
  • c) stop
  • d) break

Answer: d) break

Explanation: The break statement is used to exit a loop prematurely.


28. What is the output of the following code?

javascript

Copy code

let x = 0;

for (let i = 0; i < 5; i++) {

    if (i === 3) {

        continue;

    }

    x += i;

}

console.log(x);

  • a) 6
  • b) 10
  • c) 12
  • d) 15

Answer: a) 6

Explanation: The continue statement skips the current iteration when i === 3, so the sum is 0 + 1 + 2 + 4 = 6.


29. What will the following for loop output?

javascript

Copy code

let result = 0;

for (let i = 0; i < 3; i++) {

    result += i;

}

console.log(result);

  • a) 3
  • b) 5
  • c) 6
  • d) 9

Answer: b) 3

Explanation: The loop runs 3 times, adding 0 + 1 + 2, resulting in 3.


30. Which loop will you use to iterate over an object's properties in JavaScript?

  • a) for
  • b) for...in
  • c) while
  • d) do...while

Answer: b) for...in

Explanation: The for...in loop is used to iterate over the properties of an object in JavaScript.


31. What will the following code print?

javascript

Copy code

let i = 10;

do {

    console.log(i);

    i--;

} while (i > 5);

  • a) 10 9 8 7 6
  • b) 10 9 8 7 6 5
  • c) 9 8 7 6 5
  • d) 10 9 8

Answer: a) 10 9 8 7 6

Explanation: The loop starts with i = 10 and decrements i after each iteration. The loop runs as long as i > 5, so it stops after printing 6.


32. Which of the following loops can execute the code block at least once even if the condition is false?

  • a) while
  • b) do...while
  • c) for
  • d) All of the above

Answer: b) do...while

Explanation: The do...while loop always executes the code block at least once before checking the condition.


33. What will the following code print?

javascript

Copy code

let result = 1;

for (let i = 1; i <= 4; i++) {

    result *= i;

}

console.log(result);

  • a) 10
  • b) 12
  • c) 24
  • d) 4

Answer: c) 24

Explanation: The loop multiplies result by i each time, so the result is 1 * 1 * 2 * 3 * 4 = 24.


34. What is the output of the following code?

javascript

Copy code

let arr = [1, 2, 3, 4];

let result = 0;

for (let i = 0; i < arr.length; i++) {

    result += arr[i];

}

console.log(result);

  • a) 10
  • b) 11
  • c) 12
  • d) 14

Answer: a) 10

Explanation: The for loop adds all the elements of the array arr, so the sum is 1 + 2 + 3 + 4 = 10.


35. What will the following for loop output?

javascript

Copy code

let i = 1;

for (i = 1; i < 3; i++) {

    console.log(i);

}

  • a) 1
  • b) 1 2
  • c) 2 3
  • d) 3

Answer: b) 1 2

Explanation: The for loop runs with i starting at 1 and increments until i = 2. Thus, the output will be 1 2.


These MCQs will help assess the understanding of loops and their control structures in JavaScript for beginners. Let me know if you'd like more examples!

4o mini

You said:

yes

ChatGPT said:

ChatGPT

Here are more MCQs with explanations on loops in JavaScript:


36. What will the following for loop output?

javascript

Copy code

let i = 0;

for (i = 0; i < 5; i++) {

    if (i === 3) {

        break;

    }

    console.log(i);

}

  • a) 0 1 2 3
  • b) 0 1 2
  • c) 1 2 3
  • d) 0 1 2 3 4

Answer: b) 0 1 2

Explanation: The loop will break when i equals 3. It will print 0, 1, and 2 before exiting the loop.


37. Which loop will run indefinitely?

  • a) while (false) { break; }
  • b) for (let i = 0; i < 5; i++) { console.log(i); }
  • c) do { console.log("looping"); } while (true);
  • d) for (let i = 0; i < 5; i++) { if (i === 5) break; }

Answer: c) do { console.log("looping"); } while (true);

Explanation: The do...while loop in option c will run indefinitely because true is always true.


38. What is the value of x after the following loop executes?

javascript

Copy code

let x = 1;

for (let i = 1; i <= 4; i++) {

    x *= i;

}

console.log(x);

  • a) 24
  • b) 120
  • c) 6
  • d) 4

Answer: a) 24

Explanation: The loop multiplies x by i each time, resulting in 1 * 1 * 2 * 3 * 4 = 24.


39. What does the following code print?

javascript

Copy code

let i = 0;

while (i < 5) {

    if (i === 3) {

        continue;

    }

    console.log(i);

    i++;

}

  • a) 0 1 2 3 4
  • b) 0 1 2 4
  • c) 1 2 3 4
  • d) 0 1 2 3

Answer: b) 0 1 2 4

Explanation: The continue statement skips the iteration when i === 3, so 3 is not printed.


40. What is the result of the following code?

javascript

Copy code

let i = 5;

do {

    console.log(i);

    i--;

} while (i > 0);

  • a) 5 4 3 2 1
  • b) 5 4 3 2
  • c) 6 5 4 3 2 1
  • d) Infinite loop

Answer: a) 5 4 3 2 1

Explanation: The do...while loop starts with i = 5 and decrements i after each iteration until i becomes 0. It prints 5, 4, 3, 2, and 1.


41. How many times will the following loop run?

javascript

Copy code

for (let i = 0; i < 10; i += 2) {

    console.log(i);

}

  • a) 5
  • b) 10
  • c) 4
  • d) 2

Answer: a) 5

Explanation: The loop starts at i = 0 and increments by 2 each time. It will print 0, 2, 4, 6, 8, running for 5 iterations.


42. Which of the following loops will execute only once?

  • a) for
  • b) while
  • c) do...while
  • d) Both b and c

Answer: d) Both b and c

Explanation: The do...while loop always runs once, and the while loop can be set up to run at least once, depending on the condition.


43. What is the output of the following code?

javascript

Copy code

let count = 0;

while (count < 3) {

    count++;

}

console.log(count);

  • a) 2
  • b) 3
  • c) 0
  • d) Infinite loop

Answer: b) 3

Explanation: The while loop increments count until it reaches 3, and the final value of count is 3.


44. How many times will the do...while loop execute if the condition is initially false?

javascript

Copy code

let i = 0;

do {

    console.log(i);

    i++;

} while (false);

  • a) 0 times
  • b) 1 time
  • c) Infinite times
  • d) Not at all

Answer: b) 1 time

Explanation: The do...while loop will execute at least once, even if the condition is false.


45. What is the correct way to iterate over an array using a for loop?

  • a) for (let i = 0; i < arr.length; i++) { console.log(arr[i]); }
  • b) for (let i of arr) { console.log(i); }
  • c) arr.forEach(element => { console.log(element); })
  • d) All of the above

Answer: d) All of the above

Explanation: All options are valid ways to iterate over an array in JavaScript. Option a is a traditional for loop, option b uses the for...of loop, and option c uses the forEach method.


46. What will this code output?

javascript

Copy code

let result = "";

for (let i = 1; i <= 3; i++) {

    result += i;

}

console.log(result);

  • a) 123
  • b) 6
  • c) "1 2 3"
  • d) 1 2 3

Answer: a) 123

Explanation: The result variable will accumulate the values 1, 2, and 3 as strings, resulting in "123".


47. What will the following for loop output?

javascript

Copy code

let result = 1;

for (let i = 1; i < 3; i++) {

    result *= i;

}

console.log(result);

  • a) 6
  • b) 2
  • c) 3
  • d) 1

Answer: b) 2

Explanation: The loop multiplies result by i each time. The result is 1 * 1 * 2 = 2.


48. What does the following code do?

javascript

Copy code

let i = 0;

while (i < 5) {

    if (i === 3) {

        break;

    }

    console.log(i);

    i++;

}

  • a) Loops indefinitely until i === 3
  • b) Prints 0, 1, 2, and then breaks
  • c) Prints 0, 1, 2, 3, 4
  • d) Prints nothing

Answer: b) Prints 0, 1, 2, and then breaks

Explanation: The loop prints 0, 1, and 2. When i === 3, the break statement is executed, terminating the loop.


49. Which of the following will cause an infinite loop?

  • a) for (let i = 0; i < 10; i++) { console.log(i); }
  • b) while (true) { console.log("Hello!"); }
  • c) do { console.log("Hi!"); } while (false);
  • d) Both a and b

Answer: b) while (true) { console.log("Hello!"); }

Explanation: The while (true) loop will always run indefinitely because the condition is always true.


50. What will this code print?

javascript

Copy code

let count = 5;

do {

    console.log(count);

    count--;

} while (count > 0);

  • a) 5 4 3 2 1
  • b) 4 3 2 1
  • c) 5
  • d) Infinite loop

Answer: a) 5 4 3 2 1

Explanation: The do...while loop executes at least once and then decrements count until it reaches 0, printing the values 5, 4, 3, 2, and 1.


These questions are designed to test the understanding of loops in JavaScript. Let me know if you'd like more questions!

4o mini

You said:

yes

ChatGPT said:

ChatGPT

Here are more MCQs on loops in JavaScript with explanations:


51. What will the following for loop print?

javascript

Copy code

let i = 0;

for (i = 0; i < 5; i++) {

    if (i === 3) {

        continue;

    }

    console.log(i);

}

  • a) 0 1 2 3 4
  • b) 0 1 2 4
  • c) 1 2 3 4
  • d) 0 1 2 3

Answer: b) 0 1 2 4

Explanation: The continue statement skips the iteration when i equals 3, so 3 is not printed.


52. Which of the following is the correct way to loop through an array?

  • a) for (let i = 0; i <= arr.length; i++)
  • b) for (let i = 0; i < arr.length; i++)
  • c) for (let i = 1; i < arr.length; i++)
  • d) for (let i = 0; i < arr.length + 1; i++)

Answer: b) for (let i = 0; i < arr.length; i++)

Explanation: The correct loop iterates over the array from the 0th index to the last index (i.e., arr.length - 1).


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

javascript

Copy code

let i = 1;

do {

    i++;

} while (i < 3);

console.log(i);

  • a) 1
  • b) 2
  • c) 3
  • d) 4

Answer: c) 3

Explanation: The do...while loop executes at least once, and increments i until it becomes 3. The final value of i is 3.


54. What will be the output of this for loop?

javascript

Copy code

let sum = 0;

for (let i = 1; i <= 5; i++) {

    sum += i;

}

console.log(sum);

  • a) 10
  • b) 15
  • c) 5
  • d) 0

Answer: b) 15

Explanation: The for loop adds the values from 1 to 5. The sum is 1 + 2 + 3 + 4 + 5 = 15.


55. Which of the following loops will run at least once, even if the condition is false?

  • a) for loop
  • b) while loop
  • c) do...while loop
  • d) None of the above

Answer: c) do...while loop

Explanation: A do...while loop always runs at least once, regardless of the condition because the condition is checked after the loop executes.


56. What will this for loop print?

javascript

Copy code

for (let i = 0; i < 3; i++) {

    console.log(i);

    if (i === 1) {

        break;

    }

}

  • a) 0 1 2
  • b) 0 1
  • c) 0
  • d) Infinite loop

Answer: b) 0 1

Explanation: The loop breaks when i equals 1, so it prints 0 and 1.


57. What will this while loop output?

javascript

Copy code

let i = 0;

while (i < 3) {

    console.log(i);

    i++;

}

  • a) 0 1 2
  • b) 1 2 3
  • c) 3 2 1
  • d) Infinite loop

Answer: a) 0 1 2

Explanation: The while loop starts with i = 0 and increments i until it reaches 3, printing 0, 1, and 2.


58. How many times will the following loop execute?

javascript

Copy code

let i = 1;

while (i < 5) {

    console.log(i);

    i++;

}

  • a) 4 times
  • b) 5 times
  • c) Infinite times
  • d) 3 times

Answer: a) 4 times

Explanation: The loop will print the values 1, 2, 3, and 4. It will stop when i becomes 5.


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

javascript

Copy code

let i = 0;

do {

    i++;

    console.log(i);

} while (i < 3);

  • a) 1 2 3
  • b) 0 1 2 3
  • c) 0 1 2
  • d) 1 2

Answer: a) 1 2 3

Explanation: The do...while loop will execute at least once and print 1, 2, and 3 as it increments i.


60. What is the output of this loop?

javascript

Copy code

let sum = 0;

for (let i = 0; i < 5; i++) {

    if (i === 3) {

        continue;

    }

    sum += i;

}

console.log(sum);

  • a) 10
  • b) 11
  • c) 9
  • d) 12

Answer: c) 9

Explanation: The continue statement skips the iteration when i === 3. The sum is 0 + 1 + 2 + 4 = 9.


61. What is the correct way to break out of a loop?

  • a) exit;
  • b) return;
  • c) break;
  • d) stop;

Answer: c) break;

Explanation: The break statement is used to exit a loop prematurely.


62. How can you skip to the next iteration of a loop?

  • a) continue;
  • b) skip;
  • c) next;
  • d) pass;

Answer: a) continue;

Explanation: The continue statement skips the rest of the current loop iteration and moves to the next iteration.


63. What is the output of this for loop?

javascript

Copy code

for (let i = 0; i < 3; i++) {

    console.log(i * 2);

}

  • a) 0 2 4
  • b) 0 1 2
  • c) 1 2 3
  • d) 0 1 2 3 4

Answer: a) 0 2 4

Explanation: The loop multiplies each value of i by 2, printing 0, 2, and 4.


64. What does the following code print?

javascript

Copy code

let count = 0;

do {

    console.log(count);

    count++;

} while (count < 2);

  • a) 0 1
  • b) 0
  • c) 1
  • d) Infinite loop

Answer: a) 0 1

Explanation: The do...while loop prints 0 and 1 as it increments count until it reaches 2.


65. How many times will the following for loop execute?

javascript

Copy code

for (let i = 0; i < 10; i += 2) {

    console.log(i);

}

  • a) 5 times
  • b) 4 times
  • c) 3 times
  • d) 10 times

Answer: a) 5 times

Explanation: The loop will print 0, 2, 4, 6, and 8, executing 5 times.


66. Which loop will be useful to execute a block of code until a condition is met?

  • a) for loop
  • b) while loop
  • c) do...while loop
  • d) All of the above

Answer: d) All of the above

Explanation: All three loops can be used to execute a block of code repeatedly, but the do...while loop guarantees at least one execution.


Let me know if you'd like more questions!

4o mini

You said:

yes

ChatGPT said:

ChatGPT

Here are more MCQs related to loops in JavaScript with explanations:


67. What will be the output of this for loop?

javascript

Copy code

for (let i = 5; i >= 0; i--) {

    console.log(i);

}

  • a) 5 4 3 2 1 0
  • b) 4 3 2 1 0
  • c) 5 4 3 2 1
  • d) Infinite loop

Answer: a) 5 4 3 2 1 0

Explanation: The loop starts at 5 and decrements i until it reaches 0, printing all the values from 5 to 0.


68. What is the result of the following code?

javascript

Copy code

let sum = 0;

for (let i = 1; i <= 4; i++) {

    sum += i * i;

}

console.log(sum);

  • a) 10
  • b) 30
  • c) 50
  • d) 20

Answer: b) 30

Explanation: The loop adds the square of each i to sum. The squares are 1^2 + 2^2 + 3^2 + 4^2 = 1 + 4 + 9 + 16 = 30.


69. Which loop is best for iterating over an array?

  • a) for loop
  • b) while loop
  • c) do...while loop
  • d) for...of loop

Answer: d) for...of loop

Explanation: The for...of loop is specifically designed for iterating over array elements, making the code cleaner and easier to understand.


70. What is the output of the following do...while loop?

javascript

Copy code

let i = 0;

do {

    i++;

} while (i < 5);

console.log(i);

  • a) 4
  • b) 5
  • c) 0
  • d) Infinite loop

Answer: b) 5

Explanation: The loop increments i until it reaches 5, so the final value of i is 5.


71. What does this for loop print?

javascript

Copy code

let count = 1;

for (let i = 1; i <= 5; i++) {

    count *= i;

}

console.log(count);

  • a) 120
  • b) 60
  • c) 24
  • d) 5

Answer: a) 120

Explanation: The loop multiplies count by each value of i, which results in 1 * 1 * 2 * 3 * 4 * 5 = 120.


72. What will be the output of this code?

javascript

Copy code

let i = 0;

while (i < 4) {

    i++;

    if (i === 3) {

        break;

    }

}

console.log(i);

  • a) 3
  • b) 4
  • c) 2
  • d) Infinite loop

Answer: a) 3

Explanation: The loop increments i until it reaches 3, where it breaks and exits the loop, printing 3.


73. How many times will this loop execute?

javascript

Copy code

let i = 0;

for (i = 0; i < 10; i++) {

    if (i % 2 === 0) continue;

    console.log(i);

}

  • a) 5 times
  • b) 10 times
  • c) 4 times
  • d) Infinite times

Answer: a) 5 times

Explanation: The loop will only print odd numbers between 0 and 9, so it will print 1, 3, 5, 7, 9.


74. What is the result of the following loop?

javascript

Copy code

let i = 5;

do {

    i--;

    if (i === 2) {

        break;

    }

} while (i > 0);

console.log(i);

  • a) 2
  • b) 3
  • c) 0
  • d) Infinite loop

Answer: a) 2

Explanation: The loop decrements i until it reaches 2, at which point the break statement exits the loop.


75. What will this for loop print?

javascript

Copy code

for (let i = 0; i < 3; i++) {

    if (i === 1) {

        continue;

    }

    console.log(i);

}

  • a) 0 1 2
  • b) 0 2
  • c) 1 2
  • d) 2 3

Answer: b) 0 2

Explanation: The continue statement skips the iteration when i equals 1, so the loop prints 0 and 2.


76. What will be the output of this code?

javascript

Copy code

let sum = 0;

for (let i = 1; i <= 5; i++) {

    sum += i;

}

console.log(sum);

  • a) 5
  • b) 10
  • c) 15
  • d) 20

Answer: c) 15

Explanation: The loop adds the values 1 + 2 + 3 + 4 + 5 = 15.


77. Which loop is best for a scenario where you don’t know how many iterations you need but you have a condition to check before each iteration?

  • a) for loop
  • b) while loop
  • c) do...while loop
  • d) All of the above

Answer: b) while loop

Explanation: A while loop is ideal when the number of iterations is unknown and you want to check a condition before each iteration.


78. What is the output of the following code?

javascript

Copy code

let i = 5;

do {

    console.log(i);

    i--;

} while (i > 0);

  • a) 5 4 3 2 1
  • b) 0 1 2 3 4
  • c) 5 4 3 2
  • d) Infinite loop

Answer: a) 5 4 3 2 1

Explanation: The do...while loop will run and print the values from 5 down to 1, decrementing i each time.


79. Which statement is used to exit a loop before it completes all iterations?

  • a) return
  • b) continue
  • c) break
  • d) exit

Answer: c) break

Explanation: The break statement immediately exits the loop, even if the loop condition hasn’t been fully met.


80. What will this code print?

javascript

Copy code

let i = 0;

for (; i < 3;) {

    console.log(i);

    i++;

}

  • a) 0 1 2
  • b) 1 2 3
  • c) 0 1
  • d) 1

Answer: a) 0 1 2

Explanation: This is a for loop where the initialization is done outside the parentheses. It will print 0, 1, and 2.


Let me know if you'd like more questions or further explanations!

4o mini

You said:

yes

ChatGPT said:

ChatGPT

Here are additional MCQs related to loops in JavaScript with explanations:


81. What is the output of the following code?

javascript

Copy code

let sum = 0;

for (let i = 1; i <= 5; i++) {

    if (i % 2 === 0) {

        continue;

    }

    sum += i;

}

console.log(sum);

  • a) 9
  • b) 15
  • c) 6
  • d) 5

Answer: a) 9

Explanation: The continue statement skips even numbers (2 and 4), so the sum is 1 + 3 + 5 = 9.


82. What is the result of this do...while loop?

javascript

Copy code

let i = 10;

do {

    i--;

    console.log(i);

} while (i > 5);

  • a) 10 9 8 7 6
  • b) 9 8 7 6
  • c) 10 9 8 7
  • d) 10 9 8 7 6

Answer: b) 9 8 7 6

Explanation: The do...while loop starts at 10, decrements i in each iteration, and stops once i is 5.


83. What will be the output of this for loop?

javascript

Copy code

for (let i = 1; i <= 3; i++) {

    console.log(i);

}

console.log(i);

  • a) 1 2 3 3
  • b) 1 2 3 undefined
  • c) 1 2 3 0
  • d) 1 2 3 4

Answer: b) 1 2 3 undefined

Explanation: The console.log(i) after the loop is outside the for loop, and the variable i is not accessible outside the loop since it is block-scoped with let.


84. Which loop would be the best choice for executing a code block only once before a condition is tested?

  • a) for loop
  • b) while loop
  • c) do...while loop
  • d) for...of loop

Answer: c) do...while loop

Explanation: The do...while loop guarantees at least one iteration, because the condition is checked after the loop's body.


85. What will the following for loop print?

javascript

Copy code

for (let i = 0; i < 4; i++) {

    if (i === 2) {

        break;

    }

    console.log(i);

}

  • a) 0 1
  • b) 0 1 2
  • c) 1 2
  • d) 0 1 2 3

Answer: a) 0 1

Explanation: The break statement exits the loop when i equals 2, so it only prints 0 and 1.


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

javascript

Copy code

let i = 0;

while (i < 5) {

    i += 2;

    console.log(i);

}

  • a) 0 2 4 6
  • b) 2 4
  • c) 1 3 5
  • d) 2 4 6 8

Answer: d) 2 4 6 8

Explanation: The loop starts with i = 0, increments i by 2 in each iteration, and prints the values 2, 4, 6, and 8.


87. What is the result of the following code?

javascript

Copy code

let i = 0;

do {

    i++;

} while (i < 3);

console.log(i);

  • a) 2
  • b) 3
  • c) 1
  • d) 0

Answer: b) 3

Explanation: The do...while loop executes at least once, and then checks the condition. The loop continues until i reaches 3.


88. Which loop would be best if you know exactly how many times the loop should run?

  • a) for loop
  • b) while loop
  • c) do...while loop
  • d) None of the above

Answer: a) for loop

Explanation: The for loop is best when you know the number of iterations ahead of time because you can initialize, condition, and update all in one line.


89. What is the output of the following code?

javascript

Copy code

let count = 0;

for (let i = 0; i < 3; i++) {

    count += i;

}

console.log(count);

  • a) 3
  • b) 6
  • c) 1
  • d) 2

Answer: a) 3

Explanation: The loop adds i to count in each iteration. The values are 0 + 1 + 2 = 3.


90. What will this code print?

javascript

Copy code

let i = 0;

while (i < 3) {

    console.log(i);

    i++;

}

  • a) 0 1 2
  • b) 1 2 3
  • c) 0 1 2 3
  • d) Infinite loop

Answer: a) 0 1 2

Explanation: The loop runs three times, printing the values of i from 0 to 2.


91. How many times will the while loop execute?

javascript

Copy code

let count = 0;

while (count < 5) {

    count += 2;

}

console.log(count);

  • a) 2 times
  • b) 3 times
  • c) 5 times
  • d) Infinite times

Answer: a) 2 times

Explanation: The loop increments count by 2 each time, so it runs twice, setting count to 2 and then to 4, and then stops.


92. What will the following loop print?

javascript

Copy code

for (let i = 0; i < 5; i++) {

    if (i === 2) {

        continue;

    }

    console.log(i);

}

  • a) 0 1 2 3 4
  • b) 0 1 3 4
  • c) 0 2 4
  • d) 1 2 3 4

Answer: b) 0 1 3 4

Explanation: The continue statement skips the iteration when i equals 2, so it prints 0, 1, 3, and 4.


93. Which of the following is a correct way to write a while loop that runs indefinitely?

  • a) while (true) {}
  • b) while (1) {}
  • c) while (false) {}
  • d) while (i > 0) {}

Answer: a) while (true) {}

Explanation: A while loop with true as the condition will run indefinitely unless explicitly broken.


94. What is the result of this loop?

javascript

Copy code

let i = 0;

do {

    console.log(i);

    i++;

} while (i < 3);

  • a) 1 2 3
  • b) 0 1 2
  • c) 0 1 2 3
  • d) Infinite loop

Answer: b) 0 1 2

Explanation: The do...while loop runs at least once and prints 0, 1, and 2.


95. What does the following code do?

javascript

Copy code

let i = 5;

while (i > 0) {

    i--;

}

console.log(i);

  • a) Prints 5
  • b) Prints 0
  • c) Infinite loop
  • d) Prints -1

Answer: b) Prints 0

Explanation: The while loop decrements i until it reaches 0, and then exits, printing 0.


Would you like additional questions or more clarification on any topic?

4o mini

You said:

yes

ChatGPT said:

ChatGPT

Here are more MCQs related to loops in JavaScript with explanations:


96. What will the following for loop print?

javascript

Copy code

for (let i = 5; i > 0; i--) {

    console.log(i);

}

  • a) 5 4 3 2 1
  • b) 5 4 3 2
  • c) 4 3 2 1
  • d) 5 4 3

Answer: a) 5 4 3 2 1

Explanation: The loop starts with i = 5 and decrements i until it reaches 1, printing each value.


97. Which of the following loops will execute at least once, even if the condition is false?

  • a) for loop
  • b) while loop
  • c) do...while loop
  • d) for...in loop

Answer: c) do...while loop

Explanation: The do...while loop always executes its block at least once because the condition is checked after the loop body.


98. How many times will the following loop execute?

javascript

Copy code

let i = 0;

while (i < 0) {

    console.log(i);

    i++;

}

  • a) 0 times
  • b) 1 time
  • c) Infinite times
  • d) 2 times

Answer: a) 0 times

Explanation: The condition i < 0 is false from the start, so the loop does not execute.


99. What is the result of the following do...while loop?

javascript

Copy code

let i = 3;

do {

    console.log(i);

    i++;

} while (i < 2);

  • a) 3
  • b) 3 4
  • c) 4
  • d) Infinite loop

Answer: b) 3 4

Explanation: The do...while loop runs once before checking the condition. It prints 3, then increments i to 4 and exits since i is no longer less than 2.


100. Which of the following will stop an infinite for loop?

  • a) break
  • b) continue
  • c) return
  • d) exit

Answer: a) break

Explanation: The break statement immediately exits a loop, stopping it from running further.


Tags

Post a Comment

0Comments

Post a Comment (0)