Javascript | Loops

Last Updated :
Discuss
Comments

Question 1

Can the initialization statement in a for loop declare multiple variables?

  • Yes, like for (let i = 0, j = 10; i < 5; i++) {}

  • No, only one variable can be declared.

  • Only if they are related to each other.

  • It will cause a syntax error.

Question 2

What will be the output of the following code?

JavaScript
let arr = [1, 2, 3]; for (let i in arr) {  console.log(i); } 


  • 0, 1, 2

  • 1, 2, 3

  • "0", "1", "2"

  • "1", "2", "3"

Question 3

Which of the following code snippets will result in an infinite loop?

  • JavaScript
    let i = 10; while (i > 0) {  console.log(i);  i--; } 


  • JavaScript
    let i = 5; while (i > 0) {  console.log(i); } 


  • JavaScript
    let i = 5; do {  console.log(i);  i--; } while (i >= 0); 


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


Question 4

What will happen if you use for (let i = 0; i <= arr.length; i++) to loop through an array?

  • It will correctly loop through all elements.

  • It will try to access arr[arr.length], which is undefined.

  • It will skip the last element.

  • It will cause a syntax error.

Question 5

What will be the output of the following code?

JavaScript
let num = 10; while (num > 0); {  num -= 2;  console.log(num); } 


  • 10 8 6 4 2 0

  • 8 6 4 2 0

  • Error

  • Infinite loop

Question 6

What will be the output of the following code?

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


  • 0 1 2

  • 1 2 3

  • 0 1 2 3

  • 1 2

Question 7

What will be the value of i after the following loop?

JavaScript
for (let i = 0; i < 3; i++) {} console.log(i); 
  • 0

  • 2

  • 3

  • ReferenceError: i is not defined

Question 8

What is the difference between for...in and for...of when used with arrays?

  • for...in gives indices, for...of gives values

  • for...in gives values, for...of gives indices

  • Both give values

  • Both give indices

Question 9

How many times will be the while condition executed?

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


  • 5

  • 4

  • 6

  • Infinity

Question 10

In a for loop, if the initialization is omitted, what happens?

JavaScript
let i = 10; for ( ; i < 15; i++) {  console.log(i); } 


  • It uses the current value of i

  • It starts from 0

  • Syntax error

  • It doesn't run

There are 10 questions to complete.

Take a part in the ongoing discussion