Java ArrayList MCQ-2

Java ArrayList MCQ-2 | 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 ArrayList MCQ-1

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

Q1) 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");

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

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


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

The removeAll(Collection c) method removes all the elements of collection “c” which exist in the given list (including duplicates).

Here list al contains [A, B, A, B, A], and list al1 contains [P, Q, B]. On calling al.removeAll(al1) it will try to remove all P, Q, and B from the list “al”. The list al doesn’t contain P, and Q but contains 2 “B” which will be removed.


Q2) 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");

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

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


Answer:- a) [B, B]

The retainAll(Collection c) method removes all elements from the list except those that are present in collection “c”.

Here list al contains [A, B, A, B, A], and list al1 contains [P, Q, B]. On calling al.retainAll(al1) it will try to remove all elements except P, Q, and B from the list “al”.


Q3) 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("Java");
      al.add("Python");
      al.add("C");
      al.add("C++");
      al.add("JavaScript");
      
      System.out.println(al.get(3));
   }
}

a) C++
b) JavaScript
c) false
d) true


Answer:- a) C++

The get(int index) method fetches the element at the given index and return it to the calling method. In this list [Java, Python, C, C++, JavaScript], the “C++” String is at 4th position (3rd index).


Q4) 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");
      
      System.out.println(al.get(9));
   }
}

a) A
b) B
c) Compile time error
d) IndexOutOfBoundsException


Answer:- d) IndexOutOfBoundsException

The list contains only 5 elements, therefore we can access the 10th element of the list. It gives IndexOutOfBoundsException.


Q5) 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("Java");
      al.add("Python");
      al.add("C");
      al.add("C++");
      al.add("JavaScript");
      
      al.set(2, "SQL");
      al.set(4, "Kotlin");
      System.out.println(al);
   }
}

a) [Java, Python, C, C++, JavaScript]
b) [Java, Python, SQL, C++, Kotlin]
c) [Java, Python, SQL, C++, JavaScript]
d) [Java, Python, C, C++, JavaScript]


Answer:- b) [Java, Python, SQL, C++, Kotlin]

The set(int index, E element) method replaces the element present at the specified index with the provided object.


Q6) 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("Java");
      al.add("Python");
      al.add("C");
      al.add("C++");
      al.add("JavaScript");
      
      System.out.println(al.set(4, "Kotlin"));
   }
}

a) JavaScript
b) Kotlin
c) true
d) false


Answer:- a) JavaScript

The set(int index, E element) method replaces the element present at the specified index with the provided object. and return the old object.


Q7) 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.add("C");
      
      System.out.println(al.indexOf("B") + ", " 
                       + al.lastIndexOf("A") );
   }
}

a) 1, 0
b) 1, 4
c) 3, 4
d) 3, 0


Answer:- b) 1, 4

The “int indexOf(Object o)” method returns index of the first occurrence of element “o”. And “int lastIndexOf(Object o)” Returns the index of the last occurrence of element “o”.


Q8) 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.add("C");
      
      List<String> newList = al.subList(1, 4);
      System.out.println(newList);
   }
}

a) [B, A, B]
b) [B, A, B, A]
b) [A, B]
b) [A, B, A]


Answer:- a) [B, A, B]

The subList(int fromIndex, int toIndex) method returns a view of the portion of this list between the specified fromIndex (inclusive), and toIndex (exclusive).


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<Integer> al = new ArrayList<Integer>();
      al.add(100);
      al.add(220);
      al.add(50);
      al.add(100);
      al.add(50);
      al.add(100);

      System.out.println(al.contains(50) 
               + " " + al.contains(90));
   }
}

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


Answer:- a) true false

The “boolean contains(Object o)” method checks whether a specific element is available in the list or not? It returns true if the list contains the specific element, else it returns false.


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<Integer> al = new ArrayList<Integer>();
      al.add(100);
      al.add(220);
      al.add(50);
      al.add(100);
      al.add(50);
      al.add(100);

      List<Integer> al1 = new ArrayList<Integer>();
      al1.add(100);
      al1.add(220);
      al1.add(50);
      System.out.println(al.containsAll(al1) + " " + al.equals(al1));
   }
}

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


Answer:- a) true false

The “boolean containsAll(Collection c)” method checks whether all elements of collection “c” is available or not. The “boolean equals(Object o)” method compares the specified object with this list for equality. It returns true if the specified object is equal to this list.

Here list al contains [100, 220, 50, 100, 50, 100], and the list al1 contains [100, 220, 50]. Therefore list al contains all the elements of al1 therefore containsAll() method returns true. But both lists are not exactly the same therefore equals() method returns false.


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 *