OCA Java SE 8 Mock Exam 2 - OCA Mock Question 7








Question

Which of the following statements can be used to determine if cat can be found in the list?

         ArrayList<String> list = new ArrayList<>(); 
         list.add("dog"); 
         list.add("cat"); 
         list.add("frog"); 
  1. list.contains("cat")
  2. list.hasObject("cat")
  3. list.indexOf("cat")
  4. list.indexOf(1)




Answer



A and C

Note

The contains() returns true if the object is found and indexOf takes an object reference and returns the index of the object if found, otherwise it returns a -1.

The indexOf method does not take an integer argument.

The hasObject method does not exist in ArrayList class.