Java OCA OCP Practice Question 1077

Question

What is the output of the following?

public class Main {
   public static void main(String[] args) {
      boolean v = false;
      do {//from  w ww .ja v a  2s.  co m
         if (!v) {
            v = true;
            System.out.print("inflate-");
         }
      } while (!v);
      System.out.println("done");

   }
}
  • A. done
  • B. inflate-done
  • C. The code does not compile.
  • D. This is an infinite loop.


B.

Note

This is a correct do-while loop.

On the first iteration of the loop, the if statement executes and prints inflate-.

Then the loop condition is checked.

The variable v is true, so the loop condition is false and the loop completes.




PreviousNext

Related