Generics & ArrayList Flashcards
An ArrayList is less efficient than an array.
True.
ArrayList’s use the same bracket notation as arrays.
False.
The base type of an ArrayList cannot be a primitive type.
True.
The base type of an ArrayList can be an integer.
True.
The ArrayList class is built into what java package?
java.util
You can specify the initial capacity of an ArrayList.
True.
Specifying an initial capacity limits the size to which an ArrayList can grow.
False.
The following is a valid syntax for the 2-parameter .add() method:
list.add(2, Object1);
True.
When you specify the index in .add(), the element originally at that index is replaced.
False (it and the those that follow are shifted up the list).
What is the name of the method that returns the number of elements in an ArrayList?
list.size();
What ArrayList method is used to replace elements at a specific index?
.set(index, object1);
list.add(5, object1) is valid if list has 4 elements.
False, but list.add(4, object1) is valid.
list.set(4, object1) is valid if list has 4 elements.
False, set() can only reset an element at an index that already contains an element.
A call to the ArrayList’s default constructor creates an empty ArrayList with what initial capacity?
10.
remove(int index) returns the Object removed.
True.
list.removeRange(3, 7); removes the elements from indices 3 up to and including the element at index 7.
False, the element at index 7 is not removed.
list.trimToSize(6); trims an list to only 6 elements.
False, trimToSize() does not take any arguments.
If list is an ArrayList of base type String, then the following is valid:
String[] list2 = list.toArray();
False, .toArray() returns an array of type Object. Correction:
String[] list2 = (String[]) list.toArray();
If list is an ArrayList of type String, and list2 if of type String[], then the following always copies list into list2:
list.toArray(list2);
False, if list2 isn’t big enough to hold all elements in list, then the operation fails.
If list is an ArrayList of base type String and list2 if of type String[], then the following always copies list into list2:
list2 = list.toArray(list2);
True, unless list2 is null (run-time error) or not initialized (compiler error) - then the code fails.