Lecture 5 Flashcards

(36 cards)

1
Q

What assumptions are made about collection elements?

A

they have a proper equals()

they have a proper hashCode() method for hashed collections

they have a proper compareTo() method for sorted collections

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

how does the method removeIf work with collections?

A

takes a predicate and removes any element if true

myList.removeIf(x->x < 0);

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

What exception is thrown if an illegal element is added to a collection?

A

ClassCastException

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

In a List, elements are ordered

T or F

A

T

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

In a List, elements may be duplicate

T or F

A

T

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

add() inserts a new element at the front of a List

T or F

A

F - at the back

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

How do you get List elements between certain indices?

A

sublist(fromIndex, toIndex)

inclusive, exclusive

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

what does unmodifiableList() method do?

A

return a read only view of a collection

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

what does synchronizedList() do?

A

return a syncrnoized, thread safe view of a collection

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

what does checkedList() method do?

A

return a dynamically type safe view of a collection

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

How can you give a method a variable number of arguments?

A

varargs:

public static int max(int … elements){

int result = Integer.MIN_VALUE;

for (int i : elements)

if (i > result) result = i;

return result

}

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

How are vararg parameters passed?

A

In an array

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

public static void someMethod(int … elements, int c)

^ this signature is a legal method signature in Java

T or F

A

False - varargs must be in last position

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

What does @SafeVarargs annotation do?

A

suppresses compiler warnings about possibly unsafe use of varargs related to subtyping

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

Object[] Collection.toArray() returns an array of objects that can be modified without changing the original collection

T or F

A

T

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

what does Arrays.asList(1, 2, 3); do?

A

Returns a List<integer> with elements 1, 2 and 3</integer>

17
Q

Queues are typically LIFO (last in, first out)

T or F

A

F: Usually FIFO except for priority queues

18
Q

In a priority queue,

peek(), element() and size() take what time?

add(), remove(), offer(), poll() take what time?

remove() and contains() take what time?

A

constant time

O(log(n)) time

linear time in size of queue

19
Q

How do priorityqueues order elements?

A

Based on some priority for each element where the head has the lowest value

20
Q

What is an ArrayBlockingQueue<e>?</e>

A

bounded FIFO queue backed by an array

bounded = can’t change size after creation

new elements inserted at end

21
Q

What two blocking operations exist for an ArrayBlockingQueue?

A

attempts to add an element into a full queue block until the queue is no longer full

attempts to take an element from an empty queue block until the queue contains an element

22
Q

A circular buffer is often implemented using an ____________

A

ArrayBlockingQueue

23
Q

Java contains a blocking queue backed by a linked list

T or F

24
Q

BlockingQueue implementations are never thread safe

T or F

A

F - guaranteed to always be thread safe

25
What is a Set?
collection with no duplicates two sets equal if contain same elements
26
what is a sortedset?
A set, where elements are ordered based on their comparator
27
BitSet in Java implements Collection interface T or F
F
28
What is the difference between LinkedHashMap and HashMap?
Linked one stores elements in the order they are added. Not so with HashMap
29
HashMap, LinkedHashMap and TreeMap are not cloneable T or F HashMap, LinkedHashMap and TreeMap are not synchronised T or F
F T
30
How do you constrain type value in generic method?
public static > E min(E[] a) { ... }
31
When is an ArrayStoreException thrown?
When an attempt is made to store an element of the wrong type in an array
32
What is covariant subtyping?
A T[] can be used in place of an S[] as long as T is a subtype of S
33
Arrays do/do not have covariant subtyping in Java Collections do/do not have covariant subtyping in Java
do do not
34
What is the Optional class?
Optional which may or may not contain a value of type T method isPresent returns true if non-null value is present method get() gets the value if it is not null
35
Optional type is serialisable T or F
F
36
What is PECS?
Producer extends, consumer super So if a method on a collection is a producer, it should be List extends T\> If the method is a consumer, it should be List super T\>