Collections
- What is the output of this program?
import java.util.*;
public class Result
{
public static void main(String args[])
{
ArrayList object = new ArrayList();
object.add("Y");
object.add("O");
object.add("U");
object.ensureCapacity(5);
System.out.println(object.size());
}
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: A
Although object.ensureCapacity(5); has manually increased the capacity of object to 5 but the value is stored only at index 3, therefore object.size() returns the total number of elements stored in the object i:e 3, it has nothing to do with ensureCapacity().
- What is the output of this program?
import java.util.*;
public class Result
{
public static void main(String args[])
{
ArrayList object = new ArrayList();
object.add("Y");
object.add("O");
object.add("U");
System.out.println(object.size());
}
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: D
3
- What is the output of this program?
import java.util.*;
public class Arraylist_Example
{
public static void main(String args[])
{
ArrayList object = new ArrayList();
object.add("L");
object.add("V");
object.add("E");
object.add(1, "O");
System.out.println(object);
}
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
object is an object of class ArrayList hence it is an dynamic array which can increase and decrease its size. object.add(“n”) adds to the array element n and object.add(1,”n”) adds element n at index position 1 in the list, Hence object.add(1,”O”) stores O at index position 1 of obj and shifts the previous value stored at that position by 1.
- Which of these method can be used to increase the capacity of ArrayList object manually?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: B
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(p);
- Which of these standard collection classes implements a dynamic array?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: D
ArrayList class implements a dynamic array by extending AbstractList class.