Java API Flashcards

1
Q

API offered by Queue Interface

A

add(E element) - insert element into queue if possible (else exception)

E element() - retrive but not remove the head (exception wheen empty)

boolean offer(E) - add if possible (else return false)

E peek() - same as element(), but returns null when empty

E poll() - similar to peek, but removes the element

E remove() - similar to poll, but throws exception when empty

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

API offered by the Deque interface

A

addFirst(E)/addLast(E) - add element if possible, otherwise exception

offerFirst(E)/offerLast(E) - add element if possible, otherwise false

E removeFirst()/removeLast() - remove element, otherwise exception if empty

E pollFirst()/pollLast() - retrive and remove element or null

E peekFirst()/peekLast() - retrive element or null

E getFirst()/getLast() - retrieve element or exception

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

What are the Stack equivalent methods of a Deque?

A

push(E) -> addFirst(E)

E pop() -> E removeFirst()

E peek() -> E peekFirst()

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

Which class should be used instead of a Stack and why?

A

A Deque (a LinnkedList for example) as it is more “complete” and “consistent”, and a Stack is an extension of the Vector class, which is not recommended when used in a non-concurrent environment.

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

Can you sub-class a final class in java?

A

No, it is not allowed.

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

How can you prevent a class from being subclassed without declaring it final?

A

Make its constructor(s) private, thus subclasses won’t be able to inherit from it.

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

When is the finalize method called in java?

A

Before the class gets garbage collected.

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

PriorityQueue class API

A

add(E) - same as offer

clear()

offer(E)

E peek() - retrive but does not revome or null

E poll() - retrive and remove or null

T[] toArray(T[]) - convert to an array

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

Can you iterate over a PriorityQueue using an Iterator to retrive elements in ascending or descending order?

A

No. It supports iterator, but its ordering is not guaranteed or sorted. If you need to access the elements in a specific order, then you have to convert it to an array usiing toArray(T[]) and then sort the array using Arrays.sort()

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

Describe some of the Scanner API methods.

A

boolean hasNext() - are there more tokens?

boolean hasNextInt()/hasNextBoolean()/… - next token can be interpreted as int/boolean/…

next()/nextLine()/nextInt()/nextBoolean()/… - read next token as String/int/boolean/etc

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

Lock interface API

A

void lock() - acquires lock

Condition newCondition() - return a new condition bound to this lock

tryLock() -

tryLock(long time, TimeUnit unit) - trys to lock

unlock() - releases this lock

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

ReadWriteLock interface API

A

Lock readLock() - obtains a read lock

Lock writeLock() - obtains a write lock

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

Which classes implement the Lock interface?

A

ReentrantLock

ReentrantReadWriteLock

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

Semaphore class API

A

Semaphore(int permits) / Semaphore() - constructor

acquire() - acquire a permit or wait until one is available

acquire(int permits) - acquire more than one permit

release()/release(int permits) - releases a permit

boolean tryAcquire()/tryAcquire(int permits) - tries to acquire pemits (all or nothing)

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

CountdownLatch API

A

CountdownLatch(int count) - constructor

await()/await(int time, TimeUnit units) - waits for counter to reach zero

countDown() - decrements the count of the latch

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

What is the ordering of elements of the PriorityQueue class? Which one it returns first?

A

It returns the least element of its contents.