Java Questions & Answers – Java.util – ArrayList Class

This section of our 1000+ Java MCQs focuses 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
View Answer

Answer: c
Explanation: 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()
View Answer

Answer: a
Explanation: 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()
View Answer

Answer: d
Explanation: 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);
advertisement
advertisement

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()
View Answer

Answer: a
Explanation: 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()
View Answer

Answer: c
Explanation: None.

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

Answer: d
Explanation: 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?

advertisement
  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]
View Answer

Answer: b
Explanation: 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:

advertisement
$ 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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: a
Explanation: 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
View Answer

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

$ javac Output.java
$ java Output
2

Sanfoundry Global Education & Learning Series – Java Programming Language.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.