Java OCA OCP Practice Question 1145

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


D.

Note

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

The loop body is empty due to the semicolon right after the condition.

The loop condition keeps running with no opportunity for v to be set.

This is an infinite loop, and Option D is correct.




PreviousNext

Related