Test 2 Flashcards

1
Q

When a generic class is used in a program, when does type checking take place?

A

Type checking for generics occurs at compile time

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

public <T extends Comparable<T>> T least(T arg1, T arg2) — What constraint is placed on the type parameter T?</T>

A

T must implement the interface Comparable<T></T>

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

What is erasure?

A

The process used by the Java compiler to remove generic notation and substitute an actual type for type parameters

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

Do generic types exist at the bytecode level?

A

No. After compilation, the bytecode contains only raw classes and methods

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

When the compiler encounters an unbound type parameter (e.g. <T> or <E>), it replaces all occurrences of the type parameter with what type?</E></T>

A

It replaces them with Object.

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

When the compiler encounters a bound type parameter (e.g. <T> or <E>), it replaces all occurrences of the type parameter with what type?</E></T>

A

It replaces them with the first bound

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

What is the index of an element in a list?

A

The index is the integer position of that element within the list

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

Explain how contiguous allocation works to store elements of a list.

A

Contiguous allocation uses an array: all elements occupy sequential memory slots.

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

Explain how linked allocation works to store elements of a list.

A

Linked allocation uses nodes, each holding an element and a reference (pointer) to the next node (and optionally the previous).

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

What is the name for a last‑in‑first‑out collection?

A

stack

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

What is the name for a first‑in‑first‑out collection?

A

queue

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

What is the name for a first‑in‑last‑out collection?

A

stack

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

What does the pop method do when it is called on an empty stack?

A

It throws an exception

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

Why does the array entry containing an item that is being popped have to be set to null?

A

To avoid a memory leak.

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

What problem would you encounter if you tried to use the head of a singly linked list as the rear of a queue and the tail of the list as the front?

A

you’d have to traverse from the head to find its predecessor each time

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