Loop in Java MCQs Exercise I

Ques 1 Loop


Which loop in Java allows code to be executed repeatedly based on a condition?

A for loop
B while loop
C do-while loop
D All of the above

Ques 2 Loop


Which loop is guaranteed to execute the loop body at least once?

A for loop
B while loop
C do-while loop
D None of the above

Ques 3 Loop


What does the continue statement do in a loop?

A Skips the remaining code in the loop and proceeds to the next iteration
B Exits the loop and continues with the next statement after the loop
C Restarts the loop from the beginning
D None of the above

Ques 4 Loop


What is an infinite loop?

A A loop that runs forever
B A loop that does not run at all
C A loop that executes only once
D A loop that does not have a loop condition

Ques 5 Loop


What is the output of the following code snippet?

for (int i = 0; i < 3; i++) {
   for (int j = 0; j < 3; j++) {
      if (j == 1)
         continue;
      System.out.print(i + " " + j + " ");
   }
}

A 0 0 0 2 1 0 1 2 2 0 2 2
B 0 0 0 2 1 0 2 2
C 0 0 0 2 1 0
D 0 0 0 2

Ques 6 Loop


What is the output of the following Java code snippet?

int i = 0;
while (i < 3) {
   int j = 0;
   while (j < 2) {
      if (i == 1 && j == 1)
         break;
      System.out.print(i + " " + j + " ");
      j++;
   }
   i++;
}

A 0 0 0 1 1 0 1 1 2 0
B 0 0 0 1 1 0 1 1
C 0 0 0 1 1 0 2 0
D 0 0 0 1 2 0