CS165 Exam 4 StudySet: Stacks/Queues/Priority Queue
Assume the following code: LinkedList l=new LinkedList();l.add(2);l.add(1,2);l.add(0,4);l.add(2,10);l.add(3,20);l.add(2,30);l.add(3,40); What does l now contain?
[4, 2, 30, 40, 10, 20, 2]
Assume the following code: Comparator<String> comparator = new Comparator<String>(){ public int compare(String a, String b){ return a.length()-b.length(); }};PriorityQueue<String> queue = new PriorityQueue<String>(10, comparator);queue.offer("abcdef");queue.offer("abcd");queue.offer("abcdefghi");queue.offer("a");queue.offer("abcdefghijklmn"); What is the order of elements in queue?
[a, abcd, abcdef, abcdefghi, abcdefghijklmn]
Assume the following sequence of operations on a stack "s": s.push(9); s.push(3); int a=(Integer)s.pop(); s.push(4); int b=(Integer)s.pop(); s.push(2); s.push(7); int c=(Integer)s.pop(); What do a, b and c store?
a=3, b=4, c=7
Assume the following sequence of operations on queue "q": q.offer(9); q.offer(3); int a=(Integer)q.poll(); q.offer(4); int b=(Integer)q.poll(); q.offer(2); q.offer(7); int c=(Integer)q.poll(); What do a, b and c store?
a=9, b=3, c=4
LIFO
last in first out
Which of the following options are methods from the queue class that can be used to delete elements from a queue? -poll() -peek() -offer() -remove()
poll() remove()
Suppose a CPU needs to decide which instruction sets to process first depending on how "important" the instruction sets are, and not based on which instruction set requested CPU time first. Which data structure should the CPU use? -linked list -priority queue -queue -stack
priority queue
If there is a line of people waiting to purchase movie tickets, and the person at the front of the line is the first to be served and to leave the queue, and any new person would have to join the line at the back, would this be a stack or a queue?
queue
Picture a pile of coins on a table. If three coins were taken off from the top and two new coins were added on top of the pile, would this pile represent a stack or would it represent a queue?
stack
Select all the statements below that are true about data structures: -A data structure is a collection of programs and is also known as a library. -A data structure is a collection of data organized in some fashion. -A data structure allows for data to be accessed and manipulated in specific ways. -A data structure can only be implemented in Java.
-A data structure is a collection of programs and is also known as a library. -A data structure is a collection of data organized in some fashion. -A data structure allows for data to be accessed and manipulated in specific ways.
Select the options below that are interfaces as per Java Collections framework: -List -LinkedList -Queue -Stack
-List -Queue
List l1=new List(); does not compile. Given that the programmer does not care how the elements are accessed or manipulated, which options below are acceptable fixes to the above statement? -List l1 = new Queue(); -List l1 = new ArrayList(); -List l1 = new Stack(); -List l1 = new LinkedList();
-List l1 = new ArrayList(); -List l1 = new Stack(); -List l1 = new LinkedList();
What are the types of collections allowed by the Java Collections framework? -maps -lists -sets -tuples
-maps -lists -sets
Which of the following options are methods from the queue class that can be used to delete elements from a queue? -poll() -remove() -peek() -offer()
-poll() -remove()
Select the options below that are data structures as per Java Collections framework: -stacks -queues -prisms -priority queues
-stacks -queues -priority queues
Assume that a queue named "q" has been declared to store numbers, and the following operations are performed on the queue in that order: q.offer(4); q.offer(5); q.offer(6); q.peek(); What would q.peek() return?
4
Assume that a stack named "s" has been declared to store numbers, and the following operations are performed on the stack in that order: s.push(4); s.push(5); s.push(6); s.pop(); What would s.pop() return?
6
Queues operate on LIFO principle (T/F)
FALSE
Stacks work on LIFO principle (T/F)
True