250+ TOP MCQs on Data Structures-List and Answers

This set of Java Multiple Choice Questions & Answers (MCQs) 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

Answer: c
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.

2. How to remove duplicates from List?
a) HashSet listToSet = new HashSet(duplicateList);
b) HashSet listToSet = duplicateList.toSet();
c) HashSet listToSet = Collections.convertToSet(duplicateList);
d) HashSet listToSet = duplicateList.getSet();

Answer: a
Clarification: Duplicate elements are allowed in List. Set contains unique objects.

3. How to sort elements of ArrayList?
a) Collection.sort(listObj);
b) Collections.sort(listObj);
c) listObj.sort();
d) Sorter.sortAsc(listObj);

Answer: b
Clarification: 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

Answer: b
Clarification: 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()

Answer: c
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.

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

Answer: d
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.

7. Which class provides thread safe implementation of List?
a) ArrayList
b) CopyOnWriteArrayList
c) HashList
d) List

Answer: b
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.

8. Which of the below is not an implementation of List interface?
a) RoleUnresolvedList
b) Stack
c) AttibuteList
d) SessionList

Answer: d
Clarification: 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?
a) O(n)
b) O(1)
c) O(nlogn)
d) O(2)

Answer: b
Clarification: 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?
a) True
b) False

Answer: a
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.

  1. public void setMyArray(String[] myArray)
  2. {
  3. 	if(myArray == null)
  4.         {
  5. 		this.myArray = new String[0];	
  6. 	} 
  7.         else 
  8.         {
  9. 		this.myArray = Arrays.copyOf(newArray, newArray.length);
  10. 	} 
  11. }

Leave a Reply

Your email address will not be published. Required fields are marked *