Test 2 Flashcards
When a generic class is used in a program, when does type checking take place?
Type checking for generics occurs at compile time
public <T extends Comparable<T>> T least(T arg1, T arg2) — What constraint is placed on the type parameter T?</T>
T must implement the interface Comparable<T></T>
What is erasure?
The process used by the Java compiler to remove generic notation and substitute an actual type for type parameters
Do generic types exist at the bytecode level?
No. After compilation, the bytecode contains only raw classes and methods
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>
It replaces them with Object.
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>
It replaces them with the first bound
What is the index of an element in a list?
The index is the integer position of that element within the list
Explain how contiguous allocation works to store elements of a list.
Contiguous allocation uses an array: all elements occupy sequential memory slots.
Explain how linked allocation works to store elements of a list.
Linked allocation uses nodes, each holding an element and a reference (pointer) to the next node (and optionally the previous).
What is the name for a last‑in‑first‑out collection?
stack
What is the name for a first‑in‑first‑out collection?
queue
What is the name for a first‑in‑last‑out collection?
stack
What does the pop method do when it is called on an empty stack?
It throws an exception
Why does the array entry containing an item that is being popped have to be set to null?
To avoid a memory leak.
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?
you’d have to traverse from the head to find its predecessor each time