Java OCA OCP Practice Question 2861

Question

Given:

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class Main {
   public static void main(String[] args) {
      List<String> desk = new ArrayList<String>();
      desk.add("pen");
      desk.add("scissors");
      desk.add("redStapler");
      System.out.print(desk.indexOf("redStapler"));
      Collection.reverse(desk);/*from w w  w  .j  a  va  2s  .  com*/
      System.out.print(" " + desk.indexOf("redStapler"));
      Collection.sort(desk);
      System.out.println(" " + desk.indexOf("redStapler"));
   }
}

What is the result?

  • A. 1 1 1
  • B. 2 0 1
  • C. 2 0 2
  • D. 2 2 2
  • E. Compilation fails.
  • F. An exception is thrown at runtime.


E is correct.

Note

We had to throw at least one of these in here, sorry.

The reverse() and sort() methods are in the Collections class, not the Collection class-ouch.

Again, look for this kind of misdirection in the real exam! If the invocations had been Collections.

reverse(desk) and Collections.

sort(desk), the result would have been 2 0 1-remember, indexes are zero-based.




PreviousNext

Related