Java OCA OCP Practice Question 1149

Question

What is the output of the following?

boolean v = false; 
do { 
        if (!v) { 
           v = true; 
           System.out.print("v-"); 
        } 
} while (v); 
System.out.println("done"); 
  • A. done
  • B. v-done
  • C. The code does not compile.
  • D. This is an infinite loop.


D.

Note

On the first iteration of the loop, the if statement executes printing v-.

Then the loop condition is checked.

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

The if statement no longer runs, but the variable never changes state again, so the loop doesn't end.




PreviousNext

Related