Java ArrayList MCQ-1

Java ArrayList MCQ-1 | In Java, collection framework classes are used to store elements in value or key-value format without size limitation. We have listed some Java ArrayList MCQ and programming questions, answer them. See:- Java Collections Quiz-1

Also see- Java Collection Framework OverviewJava Collection InterfaceList Interface in Java

Q1) Which of the following classes implements the RandomAccess interface? (More than one options are correct)?

a) ArrayList
b) LinkedList
c) Vector
d) Stack

View Answer Answer:- (a) ArrayList & (c) Vector

ArrayList & Vector classes implements RandomAccess interface. Hence they can access any random element with the same speed. It means retrieving nth element directly without retrieving (n-1) elements.

Q2) Find the output of the below program?

import java.util.*;
import java.io.*;
public class Test {
   public static void main(String[] args) {
      List l1 = new Vector();
      List l2 = new ArrayList();
      Stack s1 = new Stack();
      System.out.println(l1 instanceof RandomAccess);
      System.out.println(l2 instanceof AutoCloseable);
      System.out.println(s1 instanceof Serializable);
   }
}

a) true, true, true
b) true, false, false
c) true, false, true
d) false, false, false

View Answer Answer:- b) true, false, false

The Vector class implements the RandomAccess interface. ArrayList class doesn’t implement the AutoCloseable interface. Stack class doesn’t implement any interface.

Q3) In Java, the underlying data structure for the ArrayList and Vector class is?

a) Resizable Array or Growable Array
b) Hash table
c) Balanced Tree
d) Linked List

View Answer Answer:- a) Resizable Array or Growable Array

Q4) What will be the output of the below ArrayList program?

import java.util.ArrayList;
import java.util.List;

public class Test {
   public static void main(String[] args) {
      List<String> al = new ArrayList<String>();
      al.add("A");
      al.add("B");
      al.add("A");
      al.add(null);
      System.out.println(al);
   }

}

a) Compile time error
b) [A, B, null]
c) [A, B]
d) [A, B, A, null]

View Answer Answer:- d) [A, B, A, null]

In ArrayList duplicate objects are allowed and we can also insert null values.

Q5) Find the output of the below ArrayList program?

import java.util.ArrayList;
import java.util.List;

public class Test {
   public static void main(String[] args) {

     List<String> al1 = new ArrayList<String>();
     al1.add("A");
     al1.add("B");
     
     List<String> al2 = new ArrayList<String>();
     al2.add("P");
     al2.add("Q");
     
     al1.addAll(al2);
     System.out.println(al1);
   }
}

a) [A, B]
b) [P, Q]
c) [A, B, P, Q]
d) [P, Q, A, B]

View Answer Answer:- c) [A, B, P, Q]

The addAll(Collection c) method is used to add collection “c” to the given list at the end index.

Q6) Find the output of the below ArrayList program?

import java.util.ArrayList;
import java.util.List;

public class Test {
   public static void main(String[] args) {
      List<String> al1 = new ArrayList<String>();
      al1.add("A");
      al1.add("B");

      List<String> al2 = new ArrayList<String>();
      al2.add("P");
      al2.add("Q");

      al1.addAll(0, al2);
      System.out.println(al1);
   }
}

a) [A, B]
b) [P, Q]
c) [A, B, P, Q]
d) [P, Q, A, B]

View Answer Answer:- d) [P, Q, A, B]

The addAll(index, Collection c) method is used to add Collection “c” at the given index. Here passed index value is 0 therefore the collection will be added at the beginning of the given list.

Q7) Find the output of the below ArrayList program?

import java.util.ArrayList;
import java.util.List;

public class Test {
   public static void main(String[] args) {

      List<String> al = new ArrayList<String>();
      al.add("A");
      al.add("B");
      al.add("C");
      al.add(1, "Z");
      al.add(4, "V");
      System.out.println(al);
   }
}

a) [A, Z, B, C, V]
b) [A, B, C, Z, V]
c) [Z, A, B, V, C]
d) None of these

View Answer Answer:- a) [A, Z, B, C, V]

The add(index, E) method is used to insert the given element at the specified position or index. Similar to the array, the list index starts from 0.

Q8) Find the output of the below ArrayList program?

import java.util.ArrayList;
import java.util.List;

public class Test {
   public static void main(String[] args) {
      List<String> al = new ArrayList<String>();
      al.add("A");
      al.add(4, "V");
      System.out.println(al);
   }
}

a) [A, V]
b) [A, null, null, null, V]
c) Compile time error
d) IndexOutOfBoundsException

View Answer Answer:- d) IndexOutOfBoundsException

List contains only 1 element, without inserting 3 elements we are trying to insert an element at 4th position hence we get IndexOutOfBoundsException. We will not get the compile-time error because syntactically it is a valid statement.

Q9) Find the output of the below Java ArrayList program?

import java.util.ArrayList;
import java.util.List;

public class Test {
   public static void main(String[] args) {
      List<String> al = new ArrayList<String>();
      al.add("A");
      al.add("B");
      al.add("A");
      al.add("B");
      al.add("A");
      
      al.remove("A");
      System.out.println(al);
   }
}

a) [B, B]
b) [B, A, B, A]
c) [A, B, A, B]
d) None of these

View Answer Answer:- b) [B, A, B, A]

The remove(Object ob) method removes the first occurrence of the given element.

Q10) Find the output of the below Java ArrayList program?

import java.util.ArrayList;
import java.util.List;

public class Test {
   public static void main(String[] args) {
      List<String> al = new ArrayList<String>();
      al.add("A");
      al.add("B");
      al.add("A");
      al.add("B");
      al.add("A");

      al.remove(3);
      System.out.println(al);
   }
}

a) Compile time error
b) [A, B, A, B]
c) [A, B, A, A]
d) None of these

View Answer Answer:- c) [A, B, A, A]

The remove(index) method removes the element at the given index. The index starts from 0.

Q11) How to get the Synchronized version of the ArrayList object?

a) synchronizedList(List l)
b) synchronizedSet(Set s)
c) synchronizedMap(Map m)
d) None of these

View Answer Answer:- a) synchronizedList(List l)

By default ArrayList is non-synchronized but we can get the synchronized version of the ArrayList object by using synchronizedList() method of the Collections class.

Example:-

ArrayList al = new ArrayList();
List li = Collections.synchronizedList(al);

Here “al” is non-synchronized & “li’ is synchronized.

Q12) ArrayList gives poor performance when we are performing?

1) Insert at the end
2) Insertion in the middle

View Answer Answer:- 2) Insertion in the middle

ArrayList gives poor performance when we are inserting elements in the middle of the list because for every insertion lot of shifting operations need to be performed. ArrayList is the best choice for retrieving operations. To insert in middle we should use LinkedList.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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