Java OCA OCP Practice Question 1085

Question

What does the following code output?

List<String> v = Arrays.asList("can", "cup"); 
for (int c = v.size() - 1; c >= 0; c--) 
       System.out.print(v.get(c) + ","); 
  • A. can,cup,
  • B. cup,can,
  • C. The code does not compile.
  • D. None of the above


B.

Note

This is a correct loop to go through an ArrayList or List starting from the end.

It starts with the last index in the list and goes to the first index in the list.

Option B is correct.




PreviousNext

Related