Java MCQs on ArrayList class of Java Programming Language.
1. Which of these standard collection classes implements a dynamic array? Answer: c 2. Which of these class can generate an array which can increase and decrease in size automatically? Answer: a 3. Which of these method can be used to increase the capacity of ArrayList object manually? Answer: d 4. Which of these method of ArrayList class is used to obtain present size of an object? Answer: a 5. Which of these methods can be used to obtain a static array from an ArrayList object? Answer: c 6. Which of these method is used to reduce the capacity of an ArrayList object? Answer: d 7. What will be the output of the following Java program? a) [A, B, C, D] 8. What will be the output of the following Java program? a) 0 9. What will be the output of the following Java program? a) 1 10. What will be the output of the following Java program? a) 1
a) AbstractList
b) LinkedList
c) ArrayList
d) AbstractSet
Clarification: ArrayList class implements a dynamic array by extending AbstractList class.
a) ArrayList()
b) DynamicList()
c) LinkedList()
d) MallocList()
Clarification: None.
a) Capacity()
b) increaseCapacity()
c) increasecapacity()
d) ensureCapacity()
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);
a) size()
b) length()
c) index()
d) capacity()
Clarification: None.
a) Array()
b) covertArray()
c) toArray()
d) covertoArray()
Clarification: None.
a) trim()
b) trimSize()
c) trimTosize()
d) trimToSize()
Clarification: trimTosize() is used to reduce the size of the array that underlines an ArrayList object.
import java.util.*;
class Arraylist
{
public static void main(String args[])
{
ArrayList obj = new ArrayList();
obj.add("A");
obj.add("B");
obj.add("C");
obj.add(1, "D");
System.out.println(obj);
}
}
b) [A, D, B, C]
c) [A, D, C]
d) [A, B, C]
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].
import java.util.*;
class Output
{
public static void main(String args[])
{
ArrayList obj = new ArrayList();
obj.add("A");
obj.add(0, "B");
System.out.println(obj.size());
}
}
b) 1
c) 2
d) Any Garbage Value
Clarification: None.
Output:
$ javac Output.java
$ java Output
2
import java.util.*;
class Output
{
public static void main(String args[])
{
ArrayList obj = new ArrayList();
obj.add("A");
obj.ensureCapacity(3);
System.out.println(obj.size());
}
}
b) 2
c) 3
d) 4
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
class Output
{
public static void main(String args[])
{
ArrayList obj = new ArrayList();
obj.add("A");
obj.add("D");
obj.ensureCapacity(3);
obj.trimToSize();
System.out.println(obj.size());
}
}
b) 2
c) 3
d) 4
Clarification: trimTosize() is used to reduce the size of the array that underlines an ArrayList object.
Output:
$ javac Output.java
$ java Output
2