Loop in Java Programming 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


d is the correct option




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


c is the correct option




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


a is the correct option




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


a is the correct option




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


a is the correct option




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


b is the correct option