Java OCA OCP Practice Question 1304

Question

What is the output of the following?

public class Main { 
       public static void main(String[] args) { 
         String v = null; 
         while (v == null) 
            v = "v"; 
            System.out.print(v); 
       } 
} 
  • A. null
  • B. v
  • C. vv
  • D. None of the above


B.

Note

The first time the loop condition is checked, the variable v is null.

The loop body executes, setting v.

Despite the indention, there are no brackets surrounding the loop body so the print does not run yet.

Then the loop condition is checked and v is not null.

The print runs after the loop, printing out v once, making Option B correct.




PreviousNext

Related