Java Questions & Answers – Data Structures-List

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
View Answer

Answer: c
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();
View Answer

Answer: a
Explanation: 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);
View Answer

Answer: b
Explanation: Collections provides a method to sort the list. The order of sorting can be defined using Comparator.
advertisement
advertisement

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
View Answer

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

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

Answer: d
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?
a) ArrayList
b) CopyOnWriteArrayList
c) HashList
d) List
View Answer

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

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

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

Answer: b
Explanation: ArrayList has O(1) complexity for accessing an element in ArrayList. O(n) is the complexity for accessing an element from LinkedList.
advertisement

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
View Answer

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

  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. }

Sanfoundry Global Education & Learning Series – Java Programming Language.

To practice all areas of Java language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.