This set of Java Multiple Choice Questions & Answers (MCQs) on “Data Structures-List”.
1. How can we remove an object from ArrayList? Answer: c 2. How to remove duplicates from List? Answer: a 3. How to sort elements of ArrayList? Answer: b 4. When two threads access the same ArrayList object what is the outcome of the program? Answer: b 5. How is Arrays.asList() different than the standard way of initialising List? Answer: c 6. What is the difference between length() and size() of ArrayList? Answer: d 7. Which class provides thread safe implementation of List? Answer: b 8. Which of the below is not an implementation of List interface? Answer: d 9. What is the worst case complexity of accessing an element in ArrayList? Answer: b 10. When an array is passed to a method, will the content of the array undergo changes with the actions carried within the function?
a) remove() method
b) using Iterator
c) remove() method and using Iterator
d) delete() method
Clarification: 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.
a) HashSet
b) HashSet
c) HashSet
d) HashSet
Clarification: Duplicate elements are allowed in List. Set contains unique objects.
a) Collection.sort(listObj);
b) Collections.sort(listObj);
c) listObj.sort();
d) Sorter.sortAsc(listObj);
Clarification: Collections provides a method to sort the list. The order of sorting can be defined using Comparator.
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
Clarification: ArrayList is not synchronized. Vector is the synchronized data structure.
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()
Clarification: 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.
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
Clarification: 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.
a) ArrayList
b) CopyOnWriteArrayList
c) HashList
d) List
Clarification: 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.
a) RoleUnresolvedList
b) Stack
c) AttibuteList
d) SessionList
Clarification: SessionList is not an implementation of List interface. The others are concrete classes of List.
a) O(n)
b) O(1)
c) O(nlogn)
d) O(2)
Clarification: ArrayList has O(1) complexity for accessing an element in ArrayList. O(n) is the complexity for accessing an element from LinkedList.
a) True
b) False
Clarification: 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[0];
}
else
{
this.myArray = Arrays.copyOf(newArray, newArray.length);
}
}