Top 50+ ArrayList Interview Questions in Java

38. Is there any way by which we can reverse the elements in ArrayList?

Answer: Collections class provides reverse() method to reverse elements in ArrayList. The sample code is as follows:

List<Integer> list = new ArrayList<>();
  list.add(20);
  list.add(10);
  list.add(5);
  list.add(30);
System.out.println("Original list: " +list); // 20, 10, 5, 30
Collections.reverse(list);
System.out.println("Reversed list: " +list); // 30, 5, 10, 20

39. In which scenario ArrayList is useful in Java?

Answer: There are the following scenarios where ArrayList can be used. They are as:

  • When we want to store duplicate elements.
  • When we want to store null elements.
  • It is more preferred when getting (retrieval) of the element is more as compared to adding and removing elements.
  • When we are not working in the multi-threading environment because ArrayList is non-synchronized.

40. What are the ways by which we can iterate over elements of an ArrayList?

Answer: There are five ways by which we can retrieve (or iterate) elements of an array list. They are as:

  • Using for loop
  • Using Enhanced for loop or Advanced for loop
  • Using while Loop
  • By using Iterator
  • By ListIterator

For more detail, go to this tutorial: How to iterate ArrayList in Java.

41. Will the below code snippet compile successfully? If yes, what will be the result of the following program?

a)

import java.util.ArrayList;
import java.util.List;
public class ArrayListTest {
public static void main(String[] args)
{
   List<Integer> list = new ArrayList<>();
     list.add(20);
     list.add(10);
     list.add(5);
     list.add(30);
   for(Integer i : list) {
      System.out.println(i);
      list.remove(3);
    }
  }
}

Answer: Yes, the above code will compile successfully but at runtime, JVM will throw ConcurrentModificationException because we can not add or remove an element in the ArrayList during Iteration.

Iteration over elements in the ArrayList using Enhanced for loop is fail-fast.

b)

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ArrayListTest {
public static void main(String[] args)
{
   List<String> list = new ArrayList<String>();
    list.add("John");
    list.add("Herry");
    list.add("Ivaan");
    list.add("Deep");
  Iterator<String> itr = list.iterator();
  while(itr.hasNext())
  {
    String str = itr.next();
     if(str.equals("Herry")) {
        itr.remove();
        System.out.println(list);
    }
}}}

Answer: Output: [John, Ivaan, Deep]


42. Can we iterate over elements of ArrayList in both forward and backward directions?

Answer: Using ListIterator.

43. What is the output of following program after execution?

public class ArrayListTest {
public static void main(String[] args)
{
   List<String> list = new ArrayList<String>();
    list.add("John");
    list.add("Herry");
    list.add("Ivaan");
    list.add("Deep");
  ListIterator<String> litr = list.listIterator();
  while(litr.hasNext()) {
     System.out.println(litr.next());
   }
  }
}

Answer: Output: John, Herry, Ivaan, Deep.

44. What are the ways by which we can synchronize ArrayList objects in Java?

Answer: There are two ways by which we can get the synchronized version of ArrayList. They are as follows:

  • By using Collections.synchronizedList() method
  • By using CopyOnWriteArrayList

For more detail, go to this tutorial: How to synchronize ArrayList in Java