Computer Science 109 : Introduction to Programming

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Based on the following Sets and the Iterator method, what will be the first value to display on the screen after the code runs?

0.

How many edges does this graph have?

10

A shop has a number of printers each with a different serial number. Some of the printers are on sale. What gets printed out after the following code is executed: costPrinter.put ("HP101", "$195"); costPrinter.put("HP203", "$204"); costPrinter.put("HP101", "$159"); System.out.println(costPrinter.size()):

2. Explanation: 2 will be printed out. The Map data type has 2 different values put into the same key and only one value will be saved. So the Map costPrinter() contains only 2 key-value pairs.

In the following diagram, node 78 is deleted. Which of the following options represents the most balanced structures after the deletion?

25-40-93-50-64. Explanation: Node 78 is deleted and node 93 is swapped to take its place.

Which of the following will get you from point 3 to point 5?

3 to 2 to 5.

Suppose we just performed Dijkstra's algorithm on the graph shown to find the shortest path from vertex A to vertex C, so all of the vertices have been visited as indicated with the X ' s, including the starting vertex of A. Based on the graph and all of its final marks, what is the length of the shortest path from vertex A to vertex C?

3.

How many vertices are in this graph?

5.

What will the list look like if the following code is run: myList.addNodeToHead(75); myList.addNodeToTail(100); myList.addNodeToHead(50);

50, 75, 100 Explanation: No matter the order, the tail item will be 100. But the most recent addition to the head is 50 and so it becomes the new head, while 75 is moved to the middle.

How many vertices are in this graph?

6.

Which of the following is a practical example of a doubly linked list?

A quest in a game that lets users retry stages. Explanation: The back/forward functionality is best served by a doubly linked list. You can't go past the last step, nor can you proceed before the first. However, you can go back and forth within the quest itself.

Which of the following does NOT describe a step in Dijkstra's algorithm?

Always replace a vertex's mark with the new calculated distance (even if it is larger than a previous mark).

What is a Binary Search Tree (BST)?

BST's are searchable collections of elements characterized by a nodal tree structure. Explanation: Binary Search Trees (BST) are searchable collections of elements characterized by a nodal tree structure. The tree is made of nodes and child nodes which are assigned search key values.

What could you change to make the following code not result in an error? interface A { public int one(); public int two(); public int three(); } public class B implements C { public int one() { return 1; public int two() { return 2; public int three() { return 3; } }

Change ''implements C'' to ''implements A'' Explanation: In this example there is no C to implement.

In the diagram below key 64 needs to be added. The best position will be position _____.

D. Explanation: Position D. Key 64 is greater than the root node 40 so its position will be on the right of node 40. 64 is less than 78 so its position will be left of 78. It is greater than 50 so its correct position is the right of node 50.

Which type of queue can be used as a queue or a stack?

Double-ended queue. Explanation: Double-ended queues can be used as either a queue or a stack.

What are the main characteristics of a good Hash Function?

Easy to compute and uniform distribution. Explanation: A good Hash Function should be easy to compute to avoid high processing time and distribute uniformly to guarantee randomization.

When an item is removed from a linked list, what happens to the other element references?

Element indexes are re-ordered. Explanation: If you had a linked list with 5 items and removed 2, then all indexes get re-numbered.

What are the key things to consider when it comes to using Hash Tables?

Hashing and Collision Resolution Explanation: Hashing will determine how the keys are created for the values and Collision Resolution will help deal with a hash function assigning the same key to multiple values.

What keyword is used when creating a class that uses an interface?

Implements. Explanation: A class implements an interface, so the keyword ''implements'' makes sense.

Which Java language construct is commonly used to create abstract data types?

Interfaces. Explanation: Interfaces define abstract data types, which are then implemented by classes in Java.

When you add a new element to the tail of a circularly linked list, what is true of the current tail?

It connects to the head. Explanation: tail.next = head; This is the code that tells Java that the new tail's next element is now the head, so it keeps the circular list in sync.

Based on the following Set, which of the following correctly iterates over all items in the Set?

Iterator <Integer> itr = counts.iterator (); while (itr.hasNext ()) { itr.next(); }

What is the structure used for storing data in a Hash Table?

Key-value. Explanation: The structure is known as Key-Value: HashTable (k,V)

Stack operation is based on what approach?

LIFO Explanation: The operations in Stack data structure is based on last-in-first-out, and it is predicated on the fact that there is only one entry point which also serves as exit point and called top.

What is the purpose of the following Java code in context of a doubly-linked list? Node myNode = new Node(element); Node end = head; while(end.next != null) { // Perform some operation or update on the current node System.out.println(end.data); // Example: Print the data of the current node // Move to the next node end = end.next; }

Looping until the last node is found. Explanation: This code loops until it finds the last node; if the last node has a next value of null, it is the tail.

If you need to remove an element from the tail of a circular linked list, what special step is needed?

Make sure the tail is really the last term. Explanation: You need to make sure the tail is really the last item. If you are at the tail, the next item will be the head. Therefore, you can remove this element.

Which of the following correctly checks to see if the current node is the head of a doubly linked list?

Node myNode = new Node(element); if(myNode.prev == null) { } Explanation: If the previous pointer is null, it means that the current node is the head of the list.

Why would an enhanced for loop not work with a circular linked list?

Nodes are all connected, it would result in infinite loop. Explanation: The list is like a railroad track in a circle, how do you know when to end? We use while and do...while loops to check to see if we are at the tail/head of the list.

When creating a stack data type in Java, which methods are most appropriate for the interface to the Stack?

Push, pop, peek, size, isEmpty Explanation: We must be able to add (push) and remove (pop) elements from the stack. We should also be able to view the top option (peek) without moving it, check the size, and determine if it is an empty stack.

Which statement is true?

Regular queues use the first-in, first-out structure while priority queues remove elements based on priority. Explanation: Unlike regular queues, the first-in, first-out structure does not apply to priority queues. Priority queues remove elements based on the highest priority. remove items based on their priority. The highest priority is removed first.

The following code won't compile. What should be done to correct the problem?

Remove or change the 25.35 to a String.

What does the remove method do in regard to priority queues?

Remove the highest priority element in the queue. Explanation: We use the remove method to delete the element with the highest priority.

What is function of the following code? tail = head, head = head.next;

Rotating the list. Explanation: The list is being rotated in a round-robin fashion.

Based on the following snippet of a Set, which of the following declarations would sort it in ascending order?

Set <Double> m = new TreeSet();

Which implementation method provides the best efficiency?

Sorted list. Explanation: Implementing a priority queue with a sorted list gives us greater efficiency. Since the elements are stored in priority order, removing the highest priority item is a quick process.

When would it be most appropriate to create a linked list in Java?

Storing elements in a dynamic array. Explanation: A linked list acts as a dynamic array because you can add, change, and remove elements.

The Map studentGrades contains the student IDs and the Student GPAs for the final exam of the year. If you were to store this in a Map data type, what is the key and what is the value?

Student ID is the key and the GPA is the value. Explanation: To store this in a Map data type, as a key-value pair, the key should be unique. Each student ID is unique. So Student ID is the key. Each Student ID is associated with a GPA, so GPA is the value.

The Map data type arrHomes contains home numbers and the number of pets on Broad Street. House number 205 has 3 pets. What code should be executed to print out the number of pets?

System.out.println (arrHomes.get('205')); Explanation: The get() method prints out the value associated with the key. Here the key is the house number (because the house numbers will be unique) and the value is the number of pets. So System.out.println (arrHomes.get('205')); will print out the number of pets: 3.

The Map busInfo contains bus numbers and the first stop the bus halts at after it leaves the bus depot. The following code was implemented: busInfo.put ('206', 'Maple Street'); busInfo.put ('206', 'Green Town'); What happens after the two lines of code are executed?

The Map data type busInfo() has one-key pair: 206-Green Town. Explanation: The method put() inserts a new key-value pair into the Map data type if the key does not already exist. If the key already exists, the new value will overwrite the old one. So after the execution of the 2 lines of code, the Map data type will have one key-value pair: 206-Green Town.

Which is true about an interface definition?

The methods have no bodies. Explanation: An interface is like a blueprint. The method's parameters and return type are defined, but there is no method body.

What is the shortest path problem?

The shortest path problem arises when we are trying to find the shortest distance from one vertex to another vertex in a weighted graph.

When using a linked list to represent a stack, which element(s) do you have access to within the stack?

The top (tail) Explanation: A stack is last-in-first-out, meaning you only have access to the top of the stack and therefore have to remove items from the top if you wanted to get to any other nodes.

What does it mean for a data type to be abstract?

The ways its behaviors are implemented are not defined. Explanation: Abstract data types define what the behaviors are, but not how they are implemented in code.

Which of the following describes a loop?

This edge connects point C to point C.

The Map data type carPlates contains license plate numbers and car make and model in the state of New York. One of the cars in the Map data type is a Toyota Camry with license plate 12015, and another is a Honda Accord with license plate 50064. When the following code is executed, what gets printed out? System.out.println (carPlates.containsValue('Toyota Camry')):

True. Explanation: The method ContainsValue() prints true if the value exists in the Map data type array. Here the key would be the license plate numbers, since they would be unique for the state. The value is the make and model of the car. containsValue checks to see if the value 'Toyota Camry' exists in the map adt.

Based on the following code, in what order will the values display after iterating through the set?

Undetermined until compiled and run

Which of the following statements is true about an unsorted list?

Unsorted lists have no organized order. Explanation: Unsorted lists are those in which the elements on the list are in no discernable order. Simply, they are not organized in any fashion.

What do we use in Dijkstra's algorithm for?

We use Dijkstra's algorithm to find the shortest path between two vertices on a weighted graph.

What is a collision in Hash Tables?

When the hash function assigns a key that has already been assigned. Explanation: Collisions are when the hash function randomly assigns the same key to multiple values.

When selecting a collision resolution method for Hash Tables, when are linked lists better than open addressing?

When the storage utilization is above 80%. Explanation: Open Addressing tends to run faster than linked lists up to 80% storage utilization, then the time grows exponentially

The following linked list, called j, exists in your program: [J, U, P, I, T, E, R] If you execute the following code, what will be the final value? j.add(2, "CERES");

[J, U, CERES, P, I, T, E, R] Explanation: Java starts counting at 0, so it is put in the third node, pushing all the other nodes down.

A Sort Map is _____.

a searchable collection of elements. Explanation: A binary search tree is like a sort map. A Sort Map is a searchable collection of elements.

Which two methods add an element to the back of the queue?

add, offer. Explanation: Both the add and offer methods add an element to the back of the queue. The add method will throw an exception if the queue is full, while the offer method will not.

What method is used to populate a priority queue?

add. Explanation: The add method is used to add elements to a priority queue.

Dictionary in Java is _____.

an abstract class. Explanation: The correct answer is 'an abstract class.' It has a number of abstract methods to support mappings of key/value.

Map in Java is _____.

an interface. Explanation: The correct answer is 'an interface.' It is a newer structure that is meant to replace Dictionary with the same basic functionality, but it has many implementing classes and additional functionality such as computing a mapping based on a mapping function.

All of the following are associated with queue in Java EXCEPT:

directly defined. Explanation: Queues are abstract data types because they are indirectly defined. Queues are also first-in, first-out and have six methods in the interface.

Dictionary's elements() method returns _________

enumeration of values. Explanation: The correct answer is 'Enumeration of values.' There is also a method to return enumeration of keys keys from which a corresponding value can be obtained using the get method.

Which construct is used by regular queues?

first-in, first-out Explanation: Regular queues have a head and a tail and use the first-in, first-out construct.

Which of the following Java code snippets correctly checks whether a given Node 'm' is at the end of a linked list?

if(m.next == null) Explanation: If you have reached the last node, there is no next node.

In Java Dictionary, the _____ method returns true if there are no mappings.

isEmpty() Explanation: The isEmpty() method returns true if there are no mappings.

One of the most fundamental rules in a balanced BST nodal structure representation is _____.

keys on the right of a node should be larger than the node and keys on the left side of a node should be less than that node. Explanation: The child nodes on the left must be less than the root node. The child nodes on the right must be greater than the root node

Which of the following correctly appends a new value specifically to the tail of the following linked list: LinkedList l = new LinkedList(); l.add("M"); l.add("A"); l.add("R");

l.addLast("S"); Explanation: The addLast method will add to the end (tail) of the linked list.

Which construct is used by stacks?

last-in, first-out. Explanation: Stacks, an abstract data type, use the last-in, first-out construct.

In the following linked list (named m in your code), you need to change the Z to an S. Which code example accomplishes this? [M, A, R, Z];

m.set(3, "S"); Explanation: Java starts counting at zero, so the fourth letter is index 3; use the set method to set the fourth node to an S.

If you had a Java class called myDll, how would you create a new doubly linked list instance?

myDll dll = new myDll(); Explanation: In order to add any nodes, you need to first create the doubly linked list; to do so requires the instantiation of a new instance of your class (myDll).

Which of the following Java statements is correct with a queue name of 'myQueue' and the data of type integer?

myQueue.add(319); Explanation: The dot notation is used to call the add() method on the 'myQueue'' queue. The number 319 is passed as the integer to add to the queue.

Which Queue Interface method retrieves the front element of a queue without removing it?

peek.

Which method retrieves but does not remove the element at the head of the deque?

peek.

Which Queue Interface method retrieves and removes the front element of a queue?

poll. Explanation: The poll method retrieves the element from the front of the queue and also removes it.

Which method retrieves and removes the first element from a deque?

pollFirst Explanation: The pollFirst method will both retrieve and remove the head item.

Which of the following Java statements best depicts the instantiation of the first element within a stack?

private Node top; Explanation: We use a class (usually called Node) to represent an element in our linked list. Since the head of the list is the one we can use in a stack, we can name it top.

What method is used to add data items to a stack in Java?

push() Explanation: The push() method is that which adds elements to the collection of data items in the Stack.

How do you obtain a list of keys with Java Dictionary?

use the keys() method Explanation: To obtain the list of keys, however, we have to use the keys() method, which returns Enumeration instead of the keySet() we used in Map.

Suppose we are using Dijkstra's algorithm to find the shortest path from A to D in the graph shown. The first step is to mark the ending vertex, D, with a 0, label it as current and put a circle around it as shown. In the next step, we identify E and C as vertices that are connected by an edge to the current vertex. What would we mark vertex E and vertex C with, and what vertex would be the next current vertex in the algorithm?

vertex E = 7 vertex C = 5 next current vertex = c


Ensembles d'études connexes

Chapter 4: Operating Manuals - Requirements & Development

View Set

csd 425 exam 3 pre/post class quizzes

View Set

Spanish Countries Geography Facts

View Set