Java OCA OCP Practice Question 1087

Question

What does the following code output?

public static void main(String[] args) { 
        List<String> c = Arrays.asList("A", "B"); 
        for (int type = 0; type < c.size();) { 
          System.out.print(c.get(type) + ","); 
          break; 
        } 
        System.out.print("end"); 
} 
  • A. A,end
  • B. A,B,end
  • C. The code does not compile.
  • D. None of the above


A.

Note

The first time through the loop, the index is 0 and A, is output.

The break statement then skips all remaining executions on the loop and the main() method ends.

If there was no break keyword, this would be an infinite loop.




PreviousNext

Related