Top 35 Java List Interface Interview Questions Answers for 0 to 5 years Experience

In the previous article, I wrote about  Top 28 Array Concepts Interview Questions Answers in Java. In this article, I am going to share Top 35 Java List Interface Interview Questions Answers for 0 to 5 years Experience frequently asked on Interview. You can expect many questions from this topic in the interview. This topic is one of the favorites of the interviewer.

List interface Interview Question and Answers

Q1. What do you understand by List in java?
Ans: The Java.util.List is a child interface of Collection.

Q2. What based-type elements are inserted and deleted in the List?
Ans:
It has index-based methods for insertion and deletion of elements. Insertion order in the list is preserved( store value in an ordered way).

Q3. Does List  stores duplicate elements?
Ans: Yes,
It can store duplicate values.

Q4. List out the classes implemented by List interface?
Ans:
List interface is implemented by ArrayList, LinkedList, Vector and Stack classes.

Q5. Tell some of the methods of the list interface and explain their uses?
Ans:
List interface contains the following methods:

Methods

Description

Void add(int index, Object o)

It is used to insert the specified element at the specified position in a list.

Boolean add (Object o)

This method is used to append the specified element at the end of a list.

Boolean addAll(Collection<? Extends E> c)

It is used to append all of the elements in the specified collection to the end of a list.

Void clear() This method is used to remove all the element from the list
Boolean equals(Object o) This is used to compare the specified object with the elements of a list.
Boolean isEmpty() Return true if the list is empty, otherwise false
Object[] to Array()

It is used to return an array containing all of the elements in this list in the correct order.

Boolean contains(Object o) Returns true if the list contains the specified element

boolean containsAll(Collection<?> c)

Returns true if the list contains all the specified element
Int indexOf(Object o)

It is used to return the index in this list of the first occurrence of the specified element, or -1 if the List does not contain this element.

remove(int index) This method is used to remove the element present at the specified position of the list

void retainAll(Collection<?> c)

It is used to retain all the elements in the list that are present in the specified collection.

Int size() Returns the number of an element present in the list
Void sort(Collection<?> Super E> c) It is used to sort the elements of the list on the basis of a specified comparator.

Q6. What is ArrayList in Java and found in which package?
Ans: The ArrayList is a class and it is found under java.util package.

Q7. Name the class and interface which ArrayList inherits and implements?
Ans: ArrayList inherits AbstractList class and implements List interface.

Q8. Tell that Arraylist is static or dynamic in nature?
Ans: ArrayList class is a resizable array in nature(dynamic). It means the array size of the ArrayList can be growable.

Q9. Is it possible to store duplicate elements in the ArrayList?
Ans: Yes, we can store the duplicate element in the Arraylist

Q10 ArrayList stored elements are heterogenous and homogeneous in nature?
Ans: ArrayList can store a heterogeneous object. i.e it can store any type of object.

Q11. Can null be store in ArrayList
Ans: Yes, Null can be stored in ArrayList.

Q12. Name all the constructors of the ArrayList?
Ans: Constructor of ArrayList:

  • ArrayList al = new ArrayList();
  • ArrayList al = new ArrayList(int initialCapacity);
  • ArrayList al = new ArrayList (Collection c); This is used to convert one list to another.

Q13. Do you know the default initial capacity of ArrayList and what will be a new capacity, if the initial capacity is filled?
Ans: The default initial capacity of ArrayList is 10.
New capacity= [(current capacity *3/2)+1]
= (10×3/2) +1
=(15 + 1)
= 16

Q14. What are the default interfaces does every collection class implements?
Ans: Every collection class by default implement serializable(I) and cloneable Interface.

Q15. What are the interfaces Arraylist implements?
Ans: ArrayList collection class by default implement serialiable(I) , cloneable(I) and RandomAccess(I) (Marker interface).

Q16. What is the use of RandomAccess(I) Marker Interface?
Ans: RandomAcess interface help to access any random object faster.

Q17. When to use ArrayList?
Ans: If our frequent operation is retrieval operation, then it is the best choice to use ArrayList because it implements RandomAccess Interface so accessing of the element is fast.

Q18. When not to use ArrayList?
Ans: If our frequent operation is the insertion of an object in the middle of the array. Here, to insert the element in the middle every object have to shift it position. So it will make the process slow.

Q19. Difference between ArrayList and vector?
Ans:

ArrayList

Vector

ArrayList class methods are the non-synchronized method. Vector class methods are synchronized method.
Multiple threads are allowed to operate on ArrayList object so it is not threaded safe. At a time, one thread is allowed to use. So, it is thread-safety.
Performance is high. The performance will be low as the thread has to wait to execute.
ArrayList is introduced in the 1.2 version. Vector is introduced in 1.0 version. It is a legacy class.

Q20. How to get a synchronized version of the ArrayList object for thread safety?

Ans:- By default ArrayList is a non-synchronized.
ArrayList al= new ArrayList();
List l = Collection.synchronizedList(al);here, l becomes synchronized

  • public static List synchronizedList(List obj)
  • public static Set synchronizedSet(Set s1); This is for making set method synchronized
  • public static map SysnchronizedMap(Map m) ; This is for making map method synchronized.

Q21. What is LinkedList?
Ans:
LinkedList is a collection class and it is found under java.util package.

Q22. When to use LinkedList?
Ans: If a frequent operation is an insertion in the middle of the array, then go for Linked List.

Q23. When not to use LinkedList?
Ans: If our frequent operation is retrieval operation then it is not the good choice to use LinkedList because its retrieving time is high as No random-Access interface is implemented by it.

Q24. Tell me about the LinkedList?
Ans: The important point for the LinkedList are listed below:

  • Element stored in an ordered manner.
  • Duplication of an object is allowed.
  • Storing a heterogenous object is allowed
  • Null insertion is also possible
  • LinkedList implements a serializable and cloneable interface.
  • No random Access implement.

Q25. What are the methods present in the LinkedList class?
Ans:
Some of the methods of the LinkedList are listed below:

  • void addFirst(Object o)
  • void addLast();
  • Object getFirst();
  • Object getLast();
  • Object removeFirst();
  • Object removeLast();

Q26. Difference between ArrayList and LinkedList?
Ans:

ArrayList LinkedList
The best choice of ArrayList is if the frequent operation is a retriever Best choice for LinkedList if insertion or deletion
Worse choice, if insertion and deletion is in the middle The worse choice for LinkedList retrieves operation.
Resizable or growable array Double linkList
It implements RandomAccess Interface It does implements RandomAccess interface

Q27. What do you know about the vector class in java?
Ans: It is a legacy class. It comes in 1.0 version of java. The main points for the vector class are listed below:

  • Vector is Resizable array or growable array mean it size can be changed dynamically.
  • Duplicate elements are allowed to store.
  • Insertion is preserved mean element stored in an ordered way.
  • Null is possible
  • Heterogeneous element is allowed.
  • Vector class implement serializable and colorable interface.
  • It implements RandomAccess interface
  • Methods are synchronized.

Q28. What are the methods of the Vector class?
Ans:
Methods of the vector class are listed below:

  • addElement(Object o)
  • add(int index, Object o)
  • removeAllElement()
  • remove(int index)
  • removeElementAt(index)

Q29. What are the constructors of vector class?
Ans: Constructor of the vector class are:

  • Vector v = new Vector();
  • Vector v = new Vector (int intial capacity); 
  • Vector v = new Vector ( int initialCapacity, int incrementalCapacity);
  • Vector v = new Vector (Collection<?> c);

Q30. What is the default intial capacity of the vector class and what will be new capacity, if intial capacity is filled?
Ans: Default capacity of the vector class is 10. The new capacity will become 2*capacity;

Q30. What is Enumeration?
Ans: Enumeration is only applicable to the legacy class. It is used to read operation.
Enumeration e = v.element();   here, v is vector class object. This is used for getting the enumeration object.

Q31. What are the methods of Enumeration?
Ans: The Methods of Enumeration are as follows:

  • public boolean hasMoreElements(): This method returns true if any element is present in the next index and false if no element is present in the next index
  • public Object nextElement(): This method returns the next position element value as an object.

Q32. What is Iterator?
Ans: Iterator is applicable to every collection interface. This is used to read and remove the element.
Public Iterator iterator()
Iterator itr= c.iterator(); here, c is any collection object.

Q33. What are the methods of the Iterator and explain about all the method?
Ans: Methods for Iterator are listed below:

  • public boolean hasNext(): This method returns true if any element is present in the next index
  • public Object next(); This method returns the next element of the list.
  • Public void remove(): This method removes the element from the list.

Q34. What are the differences between Enumeration, iterator, and ListIterator?
Ans:

Property Enumeration iterator ListIterator
Applicable for Only legacy class Any collection class Only for list object
Movement Forward Forward Bi( Forward and Reverse)
Accessibility Only read Read and Remove Read, Remove, Replace and Add
How to get it() Element() of vector class Iterator() of collection interface List iterator() of list interface
Is it legacy Yes (1.0v) No (1.2v) No (1.2v)

Q35. What is CopyOnWriteArrayList in Java?
Ans: Its a concurrent collection class which is introduced as an alternative of synchronized List in Java. This class take advantage of advanced thread-safety technique instead of locking. It’s 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.

That’s all about List Interface questions with answers from Core Java Interviews. I have covered almost everything as per my knowledge for both freshers and experienced with 0 to 5 years, so prepare well for the Java interview Questions, I suggest you take a look at more Java Programming Interview Questions with answers on different topics of the core java posted in this xadmin website. Keep Learning!  All the Best!