What is the output of the following code?
package mypkg; /* w w w. ja va 2 s.c o m*/ public class Main { private static int count; private static String[] stops = new String[] { "Washington", "Monroe", "Jackson", "LaSalle" }; public static void main(String[] args) { while (count < stops.length) { if (stops[count++].length() < 8) { break; } } System.out.println(count); } }
B.
Since count is a class variable that isn't specifically initialized, it defaults to 0.
On the first iteration of the loop, "Washington", is 11 characters and count is set to 1.
The if statement's body is not run.
The loop then proceeds to the next iteration.
This time, the post-increment operator uses index 1 before setting count to 2.
"Monroe" is checked, which is only 6 characters.
The break statement sends the execution to after the loop and 2 is output.
Option B is correct.