ArrayList<Integer> arr = new ArrayList<Integer>();
arr.add(2);
arr.add(3);
arr.add(4);
arr.add(4);
arr.add(5);
System.out.print(arr.indexOf(4));
Output of the above code snippet.
(A) 3
(B) 2
(C) 1
(D) None of the mentioned
Answer: (B)
Explanation: Arraylist index starts from 0.
add(): appends the specific element to the end of the list.
indexOf(): returns the index of the first occurrence of specified element in the list i.e. 2 in this case.
Quiz of this Question
Share your thoughts in the comments
Please Login to comment...