Java OCA OCP Practice Question 1089

Question

What does the following code output?

String v = ""; 
while (v.length() != 2) 
        v+="a"; 
System.out.println(v); 
  • A. aa
  • B. aaa
  • C. The loops complete with no output.
  • D. This is an infinite loop.


A.

Note

Immediately after v is initialized, the loop condition is checked.

The variable v is of length 0, which is not equal to 2 so the loop is entered.

In the loop body, v becomes length 1 with contents "a".

The loop index is checked again and now 1 is not equal to 2.

The loop is entered and v becomes length 2 and contains "aa".

Then the loop index is checked again.

Since the length is now 2, the loop is completed and aa is output.

Option A is correct.




PreviousNext

Related