Java OCA OCP Practice Question 1117

Question

What does the following code output?

public class Main{
    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


C.

Note

Since there are no brackets around the for statement, the loop body is only one line.

The break statement is not in the loop.

Since break cannot be used at the top level of a method, the code does not compile, and Option C is correct.




PreviousNext

Related