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) {…}

A

True.

25
Q

In memory, ArrayLists grow and shrink automatically.

A

False, ArrayLists do not shrink automatically - shrinking requires an invocation of trimToSize().

26
Q

It is always possible to make a deep copy of an ArrayList.

A

False - not if it is impossible to make a deep copy of objects of the base type.

27
Q

The following is a valid line:
ArrayList list = new ArrayList();

A

True - there exists an ArrayList class with no type parameter (of base type Object).

28
Q

Any non-keyword identifier can be used for the type parameter in a class definition.

A

True - T is only used by convention.

29
Q

The following is a valid instantiation of the generic class “Thing”:
Thing<String> = new Thing();</String>

A

False, correction:
Thing<String> = new Thing<String>();</String></String>

30
Q

The following is a valid definition the generic Thing class default constructor:
public Thing() { attribute = null; }

A

True - you must not put the type parameter in the constructor headings, unless it is to define the type of a parameter.

31
Q

Arrays are valid reference types that can be plugged in for a type parameter.

A

True.

32
Q

If list is an ArrayList of type integer, then the following is valid.
list.add(1);

A

True (automatic boxing).

33
Q

Class definitions can only have a limited number of type parameters.

A

False.

34
Q

A generic class can be an exception class.

A

False, a generic class cannot extend any descendent class of Throwable.

35
Q

Write the class header for a generic class named “C1” which only accepts instances or children of class “C2” for its type parameter.

A

public class C1 < T extends C2 > {…}

36
Q

Write the class header for a generic class named “C1” which only accepts classes that implement interface “I1” as their type parameter.

A

public class C1 < T extends I1 > {…}

37
Q

The following is a valid class header:
public class C<T1 extends A, T2 extends A & A2> {…}

A

True.

38
Q

Interfaces can be made generic (made to accept type parameters).

A

True.

39
Q

Generic methods can be defined, whose type parameter does not belong to any class.

A

True.

40
Q

Generic methods can only be written in generic classes.

A

False.

41
Q

The type parameter of a generic method becomes local to the class, if the class itself is generic.

A

False - the type parameter of a generic method is local to that method.

42
Q

The following is a valid signature for a generic method:
public static T genMethod(T[] a)<T> {...}</T>

A

False - the type parameter must come after all the modifiers, and before the return type:
public static <T> T genMethod(T[] a) {...}</T>

43
Q

The following is a valid invocation of a generic method:
String s = NonG.< String >genMethod(c);

A

True.

44
Q

Generic classes can be defined as a derived class of another generic class or of an ordinary class.

A

True.

45
Q

If B is a subclass of A, and G is a generic class, then G< A > is a superclass of G< B >.

A

False, there is no relationship between G< A > and G< B >, no matter the relationship between A and B.

46
Q

The following is a valid use of the type parameter:
T object = new T();

A

False. The type parameter cannot be used in expressions involving “new”.

47
Q

The following is a valid use of the type parameter:
Pair< String >[] a = new Pair< String >[10];

A

False, you cannot create arrays of generics.

48
Q

The following is a valid use of the type parameter:
T[] a = new T[10];

A

False.

49
Q

Why can’t you create arrays of generics?

A

By design, Java requires arrays at run-time to include information about their contents.

50
Q

The following is a valid use of the type parameter:
Pair<String[]> a = new Pair<String[]>();

A

True.

51
Q

The following is a valid use of the type parameter:
Pair<Pair<String[]» a =
new Pair<Pair<String[]»();

A

True.