Java OCA OCP Practice Question 1128

Question

What is the output of the following code?

package mypkg; //  w ww  .  jav  a2  s. com
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) { 
                 continue; 
               } 
           } 
           System.out.println(count); 
        } 
} 
  • A. 1
  • B. 2
  • C. 4
  • D. The code does not compile.


C.

Note

The continue statement is useless here since there is no code later in the loop to skip.

The continue statement merely resumes execution at the next iteration of the loop, which is what would happen if the if-then statement was empty.

Therefore, count increments for each element of the array.

The code outputs 4, and Option C is correct.




PreviousNext

Related