This set of Java Multiple Choice Questions & Answers (MCQs) focuses on “Data Structures-List”.
1. How can we remove an object from ArrayList?
a) remove() method
b) using Iterator
c) remove() method and using Iterator
d) delete() method
Explanation: There are 2 ways to remove an object from ArrayList. We can use overloaded method remove(int index) or remove(Object obj). We can also use an Iterator to remove the object.
2. How to remove duplicates from List?
a) HashSet<String> listToSet = new HashSet<String>(duplicateList);
b) HashSet<String> listToSet = duplicateList.toSet();
c) HashSet<String> listToSet = Collections.convertToSet(duplicateList);
d) HashSet<String> listToSet = duplicateList.getSet();
Explanation: Duplicate elements are allowed in List. Set contains unique objects.
3. How to sort elements of ArrayList?
Explanation: Collections provides a method to sort the list. The order of sorting can be defined using Comparator.
4. When two threads access the same ArrayList object what is the outcome of the program?
a) Both are able to access the object
b) ConcurrentModificationException is thrown
c) One thread is able to access the object and second thread gets Null Pointer exception
d) One thread is able to access the object and second thread will wait till control is passed to the second one
Explanation: ArrayList is not synchronized. Vector is the synchronized data structure.
5. How is Arrays.asList() different than the standard way of initialising List?
a) Both are same
b) Arrays.asList() throws compilation error
c) Arrays.asList() returns a fixed length list and doesn’t allow to add or remove elements
d) We cannot access the list returned using Arrays.asList()
Explanation: List returned by Arrays.asList() is a fixed length list which doesn’t allow us to add or remove element from it.add() and remove() method will throw UnSupportedOperationException if used.
6. What is the difference between length() and size() of ArrayList?
a) length() and size() return the same value
b) length() is not defined in ArrayList
c) size() is not defined in ArrayList
d) length() returns the capacity of ArrayList and size() returns the actual number of elements stored in the list
Explanation: length() returns the capacity of ArrayList and size() returns the actual number of elements stored in the list which is always less than or equal to capacity.
7. Which class provides thread safe implementation of List?
Explanation: CopyOnWriteArrayList is a concurrent collection class. Its very efficient if ArrayList is mostly used for reading purpose because it allows multiple threads to read data without locking, which was not possible with synchronized ArrayList.
8. Which of the below is not an implementation of List interface?
Explanation: SessionList is not an implementation of List interface. The others are concrete classes of List.
9. What is the worst case complexity of accessing an element in ArrayList?
Explanation: ArrayList has O(1) complexity for accessing an element in ArrayList. O(n) is the complexity for accessing an element from LinkedList.
10. When an array is passed to a method, will the content of the array undergo changes with the actions carried within the function?
Explanation: If we make a copy of array before any changes to the array the content will not change. Else the content of the array will undergo changes.
public void setMyArray(String myArray)
if(myArray == null)
this.myArray = new String;
this.myArray = Arrays.copyOf(newArray, newArray.length);
Sanfoundry Global Education & Learning Series – Java Programming Language.