Top 50+ ArrayList Interview Questions in Java

26. What is the main difference between ArrayList and Arrays?

Answer: The main differences between ArrayList and Arrays are as follows:

a) An array is always of fixed length and it cannot be changed at runtime, while ArrayList’s size can be changed dynamically.

b) The array is of static type whereas, ArrayList is of dynamic size.

c) In an array, we can store both primitive data types and objects. In the case of ArrayList, we can store only objects, not primitive data types.

27. Can we delete an element from an ArrayList?

Answer: Yes, we can remove an element from an ArrayList using remove() method.

28. What is the output of the below program?

import java.util.ArrayList;
public class ArrayListTest {
public static void main(String[] args)
{
ArrayList<String> list = new ArrayList<String>();
  list.add(null);
  list.add(0, "Zero");
  list.add(null);
  
  list.add(2, "Two");
  list.add("Four");
  list.add(1, "One");
  list.remove(2);
  System.out.println(list);
 }
}

Answer: Output: [Zero, One, Two, null, Four]

29. Is it possible to convert an array to ArrayList in java?

Answer: Yes, it is possible to convert an array to ArrayList using asList() method of Arrays class. The asList() method is a static method provided by Arrays class that accepts the List objects.

The sample code is given below:

public class ArrayListTest {
public static void main(String[] args)
{
     String[ ] cityArray = {"Dhanbad", "California", "Mumbai", "Paris"};
// Converting an array to ArrayList.
     List<String> cityList = Arrays.asList(cityArray);
     System.out.println(cityList);
   }
}
Output:
          [Dhanbad, California, Mumbai, Paris]

30. Can we convert an ArrayList to Array in Java?

Answer: Yes, we can convert an ArrayList to Array in java. ArrayList provides toArray() method that returns an array of elements of the same type as List.

The sample code is given below:

public class Test {
public static void main(String[] args)
{
  ArrayList<String> fruitList = new ArrayList<>();
   fruitList.add("Orange");
   fruitList.add("Apple");
   fruitList.add("Banana");
   
   int size = fruitList.size();
   String[ ] fruitArray = new String[size];
   fruitArray = fruitList.toArray(fruitArray);

// Displaying array of objects.
     for (String str : fruitArray)
          System.out.print(str + " ");
    }
}
Output:
           Orange Apple Banana

31. Can we store duplicate elements in an ArrayList? If yes, what are the ways to remove duplicate elements from ArrayList?

Answer: Yes, we can store duplicate elements in ArrayList. There are mainly two ways to remove duplicates from ArrayList. They are as follows:

a) Using HashSet: To remove duplicates from ArrayList using HashSet, we first need to convert ArrayList to HashSet. However, in this case, HashSet will not preserve the insertion order.

b) Using LinkedHashSet: The use of LinkedHashSet preserves the insertion order while removing duplicates. It can be performed by the following steps:

  • First, copy all elements of ArrayList to LinkedHashSet.
  • Now, empty the ArrayList by using clear() method. The clear() method will delete all elements from the list and make it empty.
  • At last, copy all the elements of LinkedHashSet to ArrayList.

32. What is the return type of get() method provided by ArrayList?

Answer: The return type of get() method is Object.

33. What is the output of the following program?

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(0, 30);
     list.add(2, 40);
     list.add(40);
     Object element = list.get(1);
     System.out.println(element);
   }
}

Answer: Output: 20


34. Which method of ArrayList is used to replace an element at a particular position with the specified element?

Answer: ArrayList class provides set() method that can be used to replace an element at a specific position with the specified element.

35. Which element will you get when the below code will be executed?

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(0, 30);
    list.add(2, 40);
    list.add(50);
    list.add(3, 60);
    list.set(1, 80);
   Object element = list.get(4);
   System.out.println(element);
  }
}

Answer: While executing the above code, the result will get 50.

36. By default, ArrayList is an ordered collection. Is there any way by which we can sort elements in ArrayList?

Answer: In Java, Collections class provides a static method sort() that can be used to sort elements of ArrayList. The sample code snippet is given below:

public class SortingArrayList {
public static void main(String[] args)
{
   List<Integer> list = new ArrayList<>();
    list.add(20);
    list.add(10);
    list.add(5);
    list.add(30);
    list.add(60);
   System.out.println("Original list: " +list);
   Collections.sort(list);
   System.out.println("Sorted list: " +list);
  }
}
Output:
            Original list: [20, 10, 5, 30, 60]
            Sorted list: [5, 10, 20, 30, 60]

37. How to sort elements of ArrayList in descending order?

Answer: Collections.sort() method will sort elements of ArrayList in ascending order. To get elements in descending order, either use comparator or reverseOrder() method of Collections class.

The sample code is given below:

public class DescOrderArrayList {
public static void main(String[] args)
{
   List<Integer> list = new ArrayList<>();
     list.add(20);
     list.add(10);
     list.add(5);
     list.add(30);
     list.add(60);
    System.out.println("Original list: " +list);
    Collections.sort(list, Collections.reverseOrder());
    System.out.println("Sorted list in descending order: " +list);
   }
}
Output:
         Original list: [20, 10, 5, 30, 60]
         Sorted list in descending order: [60, 30, 20, 10, 5]