Linear Nodes / Queues
For the following code, what is returned? Assume that ArrayQueue is a correctly implemented class, with lots of capacity. Also note that we are using add/remove instead of enqueue/dequeue, which is just a different vocabulary. public String testQueue() throws EmptyCollectionException { String s; Queue<String> queue = new ArrayQueue<String>(); queue.add("a"); queue.add("b"); s = queue.remove(); queue.add("c"); queue.add("d"); s = queue.remove(); s = queue.remove(); return s; } "b" "a" "c" Nothing, an exception is thrown. "d"
"c"
Which of the following are not true when working with linked list Queues? 1.Can add at any index point 2.Able to see last element without removing it 3.Can view any element if you have the index 4.They function through FIFO
1 and 3
Will of the following lines of code wll compile (assume all needed files have been imported)? 1.Queue strings = new Queue(); 2.Queue strings = new LinkedList(); 3.Queue strings = new Queue< />(); 4.Queue strings = new LinkedList< />();
2 and 4
What assert calls do you need to test that a dequeue() operation works on a queue of String objects? Assume result is what the dequeue operation returns, expResult is what you think you should get, and instance is the collection. You have created the instance, and just added one element to it, then you removed it. 1)AssertEquals(instance.size(), 0); 2)AssertEquals(result, expResult); AssertEquals(instance.size(), 0); 3)AssertEquals(result, expResult); AssertEquals(instance.size(), 1); 4)AssertEquals(result, expResult);
2)
Assume you have a linked list data structure with n nodes. It is a singly-linked list that supports generics, so it can hold any type of object. How many references are in this data structure, including references that are null?
2n + 1 (any node has ref for the next node, a ref for their object, plus the first ref to the head node)
Assume you have a stack implemented with single-linked nodes, that looks like this: 8 -> 17 -> 23 -> 93 -> 66 'top' is a reference variable that points to the node containing 8. 'count' is an internal int that holds the count of nodes (currently 5) If you want to do push operation with the value 19, what is the pseudocode to achieve this? 1) create single linked node called temp set temp's element pointer to 19 set temp's next pointer to be the same as top's next pointer set top to point at temp 2) create single linked node called temp set top to point at temp set temp's element pointer to 19 set temp's next pointer to be the same as top's next pointer increment count 3) create single linked node called temp set temp's element pointer to 19 set top to point at temp set temp's next pointer to be the same as top's next pointer increment count 4) create single linked node called temp set temp's element pointer to 19 set temp's next pointer to be the same as top set top to point at temp increment count 5) create single linked node called temp set temp's element pointer to 19 set top to point at temp increment count
4)
Assume you have a queue called nameQ that has the following items in it: "Jess", "Pearl", "Dewan", "Nadica". Jess is at the front/head of the queue. You then execute the following code: String s = nameQ.dequeue(); nameQ.enqueue("Nina"); nameQ.enqueue("Niki"); What does the queue look like? 1."Jess", "Pearl", "Dewan", "Nina", "Niki" 2."Niki", "Nina", "Jess", "Pearl", "Dewan" 3."Niki", "Nina", "Pearl", "Dewan", "Nadica" 4."Pearl", "Dewan", "Nadica", "Nina", "Niki"
4."Pearl", "Dewan", "Nadica", "Nina", "Niki"
Given the following code using a queue X: X.enqueue(new Integer(4)); X.enqueue(new Integer(3)); Integer y = X.dequeue(); X.enqueue(new Integer(7)); X.enqueue(new Integer(2)); X.enqueue(y); X.enqueue(new Integer(9)); y = X.dequeue(); X.enqueue(new Integer(3)); X.enqueue(new Integer(8)); What is the value returned by X.first()? 4 7 8 3 Nothing, an exception is thrown.
7
If you have a queue with 700 items in it, how many linear nodes would be in use? 699 700 701 702
700
How many interfaces can a Java class theoretically implement? A Java class cannot implement any interfaces. Depending on the class, only a certain amount of interfaces can be implemented. A Java class can only implement one interface. A Java class can implement any number of interfaces.
A Java class can implement any number of interfaces.
Which of the following statements is NOT true about implementations of interfaces? An implementation can have less methods than its interface specifies. An implementation can have more methods than its interface specifies. An implementation can overload any methods specified by the interface, as long as at least one of them includes the specified parameters. An implementation can accomplish a method specified by its interface in any way the programmer pleases, as long as the method is there.
An implementation can have less methods than its interface specifies.
A queue processes elements in a _____ manner. FIFO LILO LIFO FILO
FIFO
The top variable keeps track of the number of elements in a LinkedStack implementation (T/F)
False
For the following code, what is returned? public String testQueue() throws EmptyCollectionException { String s; Queue<String> queue = new ArrayQueue<String>(); queue.add("a"); queue.add("b"); s = queue.remove(); s = queue.remove(); s = queue.remove(); queue.add("c"); queue.add("d"); s = queue.remove(); s = queue.remove(); return s; } c d Nothing, an exception is thrown. Nothing, this code won't compile. b a
Nothing, an exception is thrown
For the following code, what is returned? public String testQueue() throws EmptyCollectionException { String s; Queue<String> queue = new ArrayQueue<Integer>(); queue.add("a"); queue.add("b"); s = queue.remove(); s = queue.remove(); queue.add("c"); queue.add("d"); s = queue.remove(); s = queue.remove(); return s; } "b" Nothing, the code won't compile. Nothing, an exception is thrown. "a" "c" "d"
Nothing, the code won't compile.
Which of the following is NOT a difference between Stacks and Queues? Stacks add elements by 'pushing' them, while Queues add elements by 'enqueueing' them. Stacks must utilize LinkedLists, while Queues must utilize ArrayLists Stacks are Last-In First-Out (LIFO), while Queues are First-In First-Out (FIFO) Stacks are ideal for situations such as implementing an undo button, while Queues are ideal for situations such as a handling help requests in the order they're recieved.
Stacks must utilize LinkedLists, while Queues must utilize ArrayLists
In implementing the push, pop and peek StackADT methods using a linked structure, you never have to iterate through the entire set of linear nodes. (T/F)
True
It is possible for the head and tail pointers in a linked list queue implementation to point at the same node. False True
True
What is the benefit of implementing a stack with a linked linear node structure, as opposed to an array? There is less programming to do. There are no benefits. They are the same. You won't run out of capacity like with an array, and have to create and copy a whole new structure. You no longer have to throw Empty Collection exceptions for pop and peek methods.
You won't run out of capacity like with an array, and have to create and copy a whole new structure.
Which of the following test asserts pass when evaluated against a queue instance with 1 item in it? assertTrue(instance.isEmpty()); assertNull(instance); assertEquals(instance.size(),1); assertFalse(!instance.isEmpty());
assertEquals(instance.size(),1);
A queue is appropriate for which of the following domains: call center call processing maze navigation contact list undo tracking
call center call processing
Because a linear node holds a reference to a generic object, we call it a: self-referential type container collection genotype
container, linear nodes are just containers holding references to other datatypes
Which Java queue operations throw exceptions? add() element() poll() peek() remove()
element() and remove()
When using a Queue implemented with Linked Nodes how would you gain access to the element of the second node in the list? tail.getNext().getElement(); tail.getNext(); head.getNext().getElement(); head.getNext();
head.getNext().getElement();
Linked nodes have this advantage over arrays for implementing collections: easier to program because they rely on pointers no capacity issues faster access to elements in the collection none of the above
no capacity issues, arrays have to keep updating their capacity, linked nodes are just referencing objects so no memory needs to be changed
Which of the following methods allows you to return the head from a Queue, removing it and not throw an exception if the Queue is empty? element() peek() remove() poll()
poll()
There are two main types of linear nodes: recursive and non-recursive horizontal and vertical static and non-static singly-linked and doubly-linked
singly-linked and doubly-linked (forward only, or forward/backwards)