250+ TOP MCQs on Java.util – ArrayList Class and Answers

Java MCQs on ArrayList class of Java Programming Language.

1. Which of these standard collection classes implements a dynamic array?
a) AbstractList
b) LinkedList
c) ArrayList
d) AbstractSet

Answer: c
Clarification: ArrayList class implements a dynamic array by extending AbstractList class.

2. Which of these class can generate an array which can increase and decrease in size automatically?
a) ArrayList()
b) DynamicList()
c) LinkedList()
d) MallocList()

Answer: a
Clarification: None.

3. Which of these method can be used to increase the capacity of ArrayList object manually?
a) Capacity()
b) increaseCapacity()
c) increasecapacity()
d) ensureCapacity()

Answer: d
Clarification: When we add an element, the capacity of ArrayList object increases automatically, but we can increase it manually to specified length x by using function ensureCapacity(x);

4. Which of these method of ArrayList class is used to obtain present size of an object?
a) size()
b) length()
c) index()
d) capacity()

Answer: a
Clarification: None.

5. Which of these methods can be used to obtain a static array from an ArrayList object?
a) Array()
b) covertArray()
c) toArray()
d) covertoArray()

Answer: c
Clarification: None.

6. Which of these method is used to reduce the capacity of an ArrayList object?
a) trim()
b) trimSize()
c) trimTosize()
d) trimToSize()

Answer: d
Clarification: trimTosize() is used to reduce the size of the array that underlines an ArrayList object.

7. What will be the output of the following Java program?

  1.     import java.util.*;
  2.     class Arraylist 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             ArrayList obj = new ArrayList();
  7.             obj.add("A");
  8.             obj.add("B");
  9.             obj.add("C");
  10.             obj.add(1, "D");
  11.             System.out.println(obj);
  12.         }
  13.     }

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

Answer: b
Clarification: obj is an object of class ArrayList hence it is an dynamic array which can increase and decrease its size. obj.add(“X”) adds to the array element X and obj.add(1,”X”) adds element x at index position 1 in the list, Hence obj.add(1,”D”) stores D at index position 1 of obj and shifts the previous value stored at that position by 1.
Output:

$ javac Arraylist.java
$ java Arraylist
[A, D, B, C].

8. What will be the output of the following Java program?

  1.     import java.util.*;
  2.     class Output 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             ArrayList obj = new ArrayList();
  7.             obj.add("A");
  8.             obj.add(0, "B");
  9.             System.out.println(obj.size());
  10.         }
  11.     }

a) 0
b) 1
c) 2
d) Any Garbage Value

Answer: c
Clarification: None.
Output:

$ javac Output.java
$ java Output
2

9. What will be the output of the following Java program?

  1.     import java.util.*;
  2.     class Output 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             ArrayList obj = new ArrayList();
  7.             obj.add("A");
  8.             obj.ensureCapacity(3);
  9.             System.out.println(obj.size());
  10.         }
  11.     }

a) 1
b) 2
c) 3
d) 4

Answer: a
Clarification: Although obj.ensureCapacity(3); has manually increased the capacity of obj to 3 but the value is stored only at index 0, therefore obj.size() returns the total number of elements stored in the obj i:e 1, it has nothing to do with ensureCapacity().
Output:

$ javac Output.java
$ java Output
1

10. What will be the output of the following Java program?

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             ArrayList obj = new ArrayList();
  6.             obj.add("A");
  7.             obj.add("D");
  8.             obj.ensureCapacity(3);
  9.             obj.trimToSize();
  10.             System.out.println(obj.size());
  11.          }      
  12.     }

a) 1
b) 2
c) 3
d) 4

Answer: b
Clarification: trimTosize() is used to reduce the size of the array that underlines an ArrayList object.
Output:

$ javac Output.java
$ java Output
2

Leave a Reply

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