Generics & ArrayList Flashcards

1
Q

An ArrayList is less efficient than an array.

A

True.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

ArrayList’s use the same bracket notation as arrays.

A

False.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

The base type of an ArrayList cannot be a primitive type.

A

True.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

The base type of an ArrayList can be an integer.

A

True.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

The ArrayList class is built into what java package?

A

java.util

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

You can specify the initial capacity of an ArrayList.

A

True.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Specifying an initial capacity limits the size to which an ArrayList can grow.

A

False.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

The following is a valid syntax for the 2-parameter .add() method:
list.add(2, Object1);

A

True.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

When you specify the index in .add(), the element originally at that index is replaced.

A

False (it and the those that follow are shifted up the list).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the name of the method that returns the number of elements in an ArrayList?

A

list.size();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What ArrayList method is used to replace elements at a specific index?

A

.set(index, object1);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

list.add(5, object1) is valid if list has 4 elements.

A

False, but list.add(4, object1) is valid.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

list.set(4, object1) is valid if list has 4 elements.

A

False, set() can only reset an element at an index that already contains an element.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

A call to the ArrayList’s default constructor creates an empty ArrayList with what initial capacity?

A

10.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

remove(int index) returns the Object removed.

A

True.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

list.removeRange(3, 7); removes the elements from indices 3 up to and including the element at index 7.

A

False, the element at index 7 is not removed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

list.trimToSize(6); trims an list to only 6 elements.

A

False, trimToSize() does not take any arguments.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

If list is an ArrayList of base type String, then the following is valid:
String[] list2 = list.toArray();

A

False, .toArray() returns an array of type Object. Correction:
String[] list2 = (String[]) list.toArray();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

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);

A

False, if list2 isn’t big enough to hold all elements in list, then the operation fails.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

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);

A

True, unless list2 is null (run-time error) or not initialized (compiler error) - then the code fails.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

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);

A

They remain as they were previously defined.

22
Q

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);}

A

False (replace foreach with for).

23
Q

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);}

A

False (swap ‘list’ and ‘String x’).

24
Q

The following syntax works for iterating over LinkedLists as well as ArrayLists:
for (Type x : list) {…}

25
In memory, ArrayLists grow and shrink automatically.
False, ArrayLists do not shrink automatically - shrinking requires an invocation of trimToSize().
26
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.
27
The following is a valid line: ArrayList list = new ArrayList();
True - there exists an ArrayList class with no type parameter (of base type Object).
28
Any non-keyword identifier can be used for the type parameter in a class definition.
True - T is only used by convention.
29
The following is a valid instantiation of the generic class "Thing": Thing = new Thing();
False, correction: Thing = new Thing();
30
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.
31
Arrays are valid reference types that can be plugged in for a type parameter.
True.
32
If list is an ArrayList of type integer, then the following is valid. list.add(1);
True (automatic boxing).
33
Class definitions can only have a limited number of type parameters.
False.
34
A generic class can be an exception class.
False, a generic class cannot extend any descendent class of Throwable.
35
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 > {...}
36
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 > {...}
37
The following is a valid class header: public class C {...}
True.
38
Interfaces can be made generic (made to accept type parameters).
True.
39
Generic methods can be defined, whose type parameter does not belong to any class.
True.
40
Generic methods can only be written in generic classes.
False.
41
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.
42
The following is a valid signature for a generic method: public static T genMethod(T[] a) {...}
False - the type parameter must come after all the modifiers, and before the return type: public static T genMethod(T[] a) {...}
43
The following is a valid invocation of a generic method: String s = NonG.< String >genMethod(c);
True.
44
Generic classes can be defined as a derived class of another generic class or of an ordinary class.
True.
45
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.
46
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".
47
The following is a valid use of the type parameter: Pair< String >[] a = new Pair< String >[10];
False, you cannot create arrays of generics.
48
The following is a valid use of the type parameter: T[] a = new T[10];
False.
49
Why can't you create arrays of generics?
By design, Java requires arrays at run-time to include information about their contents.
50
The following is a valid use of the type parameter: Pair a = new Pair();
True.
51
The following is a valid use of the type parameter: Pair> a = new Pair>();
True.