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.
If list is an ArrayList of base type String and has length 3, and list2 if of type String[] and has length 6, what happens to indices 3 - 5 of list2?
list.toArray(list2);
They remain as they were previously defined.
If list is an ArrayList of base type String with 3 elements, then the following will run:
foreach (String x : list) {System.out.println(x);}
False (replace foreach with for).
If list is an ArrayList of base type String, with 3 elements, then the following will run:
for (list : String x) {System.out.println(x);}
False (swap ‘list’ and ‘String x’).
The following syntax works for iterating over LinkedLists as well as ArrayLists:
for (Type x : list) {…}
True.
In memory, ArrayLists grow and shrink automatically.
False, ArrayLists do not shrink automatically - shrinking requires an invocation of trimToSize().
It is always possible to make a deep copy of an ArrayList.
False - not if it is impossible to make a deep copy of objects of the base type.
The following is a valid line:
ArrayList list = new ArrayList();
True - there exists an ArrayList class with no type parameter (of base type Object).
Any non-keyword identifier can be used for the type parameter in a class definition.
True - T is only used by convention.
The following is a valid instantiation of the generic class “Thing”:
Thing<String> = new Thing();</String>
False, correction:
Thing<String> = new Thing<String>();</String></String>
The following is a valid definition the generic Thing class default constructor:
public Thing() { attribute = null; }
True - you must not put the type parameter in the constructor headings, unless it is to define the type of a parameter.
Arrays are valid reference types that can be plugged in for a type parameter.
True.
If list is an ArrayList of type integer, then the following is valid.
list.add(1);
True (automatic boxing).
Class definitions can only have a limited number of type parameters.
False.
A generic class can be an exception class.
False, a generic class cannot extend any descendent class of Throwable.
Write the class header for a generic class named “C1” which only accepts instances or children of class “C2” for its type parameter.
public class C1 < T extends C2 > {…}
Write the class header for a generic class named “C1” which only accepts classes that implement interface “I1” as their type parameter.
public class C1 < T extends I1 > {…}
The following is a valid class header:
public class C<T1 extends A, T2 extends A & A2> {…}
True.
Interfaces can be made generic (made to accept type parameters).
True.
Generic methods can be defined, whose type parameter does not belong to any class.
True.
Generic methods can only be written in generic classes.
False.
The type parameter of a generic method becomes local to the class, if the class itself is generic.
False - the type parameter of a generic method is local to that method.
The following is a valid signature for a generic method:
public static T genMethod(T[] a)<T> {...}</T>
False - the type parameter must come after all the modifiers, and before the return type:
public static <T> T genMethod(T[] a) {...}</T>
The following is a valid invocation of a generic method:
String s = NonG.< String >genMethod(c);
True.
Generic classes can be defined as a derived class of another generic class or of an ordinary class.
True.
If B is a subclass of A, and G is a generic class, then G< A > is a superclass of G< B >.
False, there is no relationship between G< A > and G< B >, no matter the relationship between A and B.
The following is a valid use of the type parameter:
T object = new T();
False. The type parameter cannot be used in expressions involving “new”.
The following is a valid use of the type parameter:
Pair< String >[] a = new Pair< String >[10];
False, you cannot create arrays of generics.
The following is a valid use of the type parameter:
T[] a = new T[10];
False.
Why can’t you create arrays of generics?
By design, Java requires arrays at run-time to include information about their contents.
The following is a valid use of the type parameter:
Pair<String[]> a = new Pair<String[]>();
True.
The following is a valid use of the type parameter:
Pair<Pair<String[]» a =
new Pair<Pair<String[]»();
True.