FInal Exam From JG

¡Supera tus tareas y exámenes ahora con Quizwiz!

Read the following code and determine what goes into blank #3. template<class VertexType> void ShortestPath(GraphType<VertexType> graph, VertexType startVertex) { using namespace std; // ItemType is the type for items that are added to the priority queue. // It has 3 fields: VertexType toVertex, VertexType fromVertex, int distance // It overloads the <, == and <= operators, which compare the distance fields. ItemType item; int minDistance; PQType<ItemType> pq(10); // Assume at most 10 vertices. QueType<VertexType> vertexQ; VertexType vertex; graph.ClearMarks(); // Initialize the first item item.toVertex = _____; // #1 item.fromVertex = _____; // #2 item.distance = 0; pq.Enqueue(item); cout << "Last Vertex Destination Distance" << endl; cout << "-----------------------------------" << endl; do { pq.Dequeue(item); if (______) // #3 { graph.MarkVertex(item.toVertex); cout << item.fromVertex;

!graph.IsMarked(item.toVertex)

Read the following code and determine what goes into blank #4. template<class VertexType> bool DepthFirstSearch(GraphType<VertexType> graph, VertexType startVertex, VertexType endVertex) // Assumes VertexType is a type for which the "==" and "<<" // operators are defined. // Returns true if the endVertex is found and false otherwise. { using namespace std; StackType<VertexType> stack; QueType<VertexType> vertexQ; bool found = false; VertexType vertex; VertexType item; __________; // #1 __________; // #2 do { stack.Pop(vertex); if (__________) // #3 { cout << vertex; found = true; } else { if (__________) // #4 { __________; // #5 cout << vertex; graph.GetToVertices(vertex, vertexQ); while (!vertexQ.IsEmpty()) { vertexQ.Dequeue(item); if (__________) // #6 stack.Push(item); } } } } while (__________); // #7 return found; }

!graph.IsMarked(vertex)

Read the following code and determine what goes into blank #7. template<class VertexType> bool DepthFirstSearch(GraphType<VertexType> graph, VertexType startVertex, VertexType endVertex) // Assumes VertexType is a type for which the "==" and "<<" // operators are defined. // Returns true if the endVertex is found and false otherwise. { using namespace std; StackType<VertexType> stack; QueType<VertexType> vertexQ; bool found = false; VertexType vertex; VertexType item; __________; // #1 __________; // #2 do { stack.Pop(vertex); if (__________) // #3 { cout << vertex; found = true; } else { if (__________) // #4 { __________; // #5 cout << vertex; graph.GetToVertices(vertex, vertexQ); while (!vertexQ.IsEmpty()) { vertexQ.Dequeue(item); if (__________) // #6 stack.Push(item); } } } } while (__________); // #7 return found; }

!stack.IsEmpty() && !found

In an array-of-records linked list implementation, what integer is used to represent the end of a list?

-1

Read the following code and choose the correct response for blank #1. Boolean ListType::IsThere(int value) // Pre: list is initialized // Post: function value = value is an element of list { return ValueInList(info, value, length-1); } bool ValueInList(int info[], ValueType value, int last) // Pre: info contains the elements in an array-based list. // Post:ValueInList returns true if value is in the array info; // false otherwise. { if (last == ________) // 1 return ________; // 2 else if (info[last] ==________) // 3 return _________; // 4 else return ValueInList(info, value, _______) // 5 } Group of answer choices

-1

If the following function is called with a value of 2 for n, what is the resulting output? void Quiz( /* in */ int n ) { if (n > 0) { cout << 0; Quiz(n - 1); cout << 1; Quiz(n - 1); } }

001101

What is the state of the array when Data[0]..Data[2] is sorted using simple insertion sort?

1 9 12 23 17 11

Given the following tree, show the contents of the run-time stack when Insert(Tree, 14) is executed. In the Return column, use R0, R1, or R2. R0 is the initial, non-recursive call; R1 is the call to Insert(tree>left); R2 is the call to Insert(tree->right). Use the notation 10 to represent the node whose value is 10. What is put on the stack with call 2?

15 and R2

Based on the function defined below, what is the value of z(6,8)? int z(int k, int n) { if (n == k) return k else { if (n > k) return z(k, n-k); else return z(k-n, n); } }

2

Draw the binary search tree containing the following 11 values in the order shown, then answer the question. 15 7 9 21 44 30 33 29 10 1 17 (in that order). What level is the value 17 (root is level 0)?

2

Draw the binary search tree whose elements are inserted in the following order: 8, 20, 6, 4, 23, 30, 29, 33, 7. Then answer the following question.What is the inorder successor of the node containing 20?

23

What is the state of the array after each element has been inserted into the heap (highest priority is largest value) to be used in heap sort?

23 17 11 1 12 9

If the following function is called with a value of 73 for n, what is the resulting output? void Func( /* in */ int n ) { if (n > 0) { Func(n / 5); cout << n % 5; } }

243

Draw the binary search tree containing the following 11 values in the order shown and answer the question. 15 7 9 21 44 30 33 29 10 1 17 (in that order). Which is the inorder predecessor of 44?

33

In which of the following situations is a copy constructor NOT called?

A copy constructer is used instead of an assignment operator if the object contain poiner

Which of the following prevents us from achieving the maximum possible performance increase from parallel merge sort?

Both the overhead from creating threads and using a serial merge algorithm

What is the C++ construct that is used to implement generic types?

Class template

A complete graph is defined by the fact that every vertex is part of at least one edge.

F

A copy constructor is automatically built into any class that needs it.

F

A heap must be a full binary tree.

F

A shallow copy copies one class object to another, including any pointed-to data.

F

A trailer node is syntactically different from the other nodes in the list.

F

An O(N logN) sort is always faster than an O(N*N) sort.

F

An array-of-records implementation of a linked list usually requires less memory than a dynamic data implementation.

F

An undirected graph has direct links between all vertices.

F

In serial processing, multiple operations are performed simultaneously.

F

Inserting into and deleting from a binary search tree cannot be programmed iteratively.

F

Not all recursive algorithms need a base case.

F

Radix sort is an unstable sort.

F

ReheapUp is used in heap sort to maintain the heap order of the unsorted elements.

F

The base case in a recursive algorithm is the one where the recursive expression(s) occur.

F

The general case in a recursive algorithm is the one where you know the answer.

F

Value-returning functions can be recursive, but void functions cannot.

F

Which of the following statements could describe the general (recursive) case of a recursive algorithm?

F(x) = x - F(x - 1)

Which of the following lists the graph nodes in breadth-first order beginning at F?

F, C, D, A, B, E, G

Which of the following lists the graph nodes in priority queue order (lowest value is highest priority)?

F, C, D, B, G, A, E

In the array of records implementation, what is the equivalent of delete?

FreeNode

If 40 is removed, the resulting tree is:

Full

In the array of records implementation, what is the equivalent of new?

GetNode

When writing a recursive algorithm, what is the first step?

Getting an exact problem definition

Which NlogN sort should you NOT use if stability is required?

Heap Sort

In the following recursive function, which line(s) represent the general (recursive) case? void PrintIt( /* in */ int n ) // Line 1 { // Line 2 if (n == 0) // Line 3 cout << n << endl; // Line 4 else // Line 5 { // Line 6 cout << "Again" << endl; // Line 7 PrintIt(n - 1); // Line 8 } }

Lines 7-8

Examine the following binary search tree and answer the question. The numbers on the nodes are labels so that we can talk about the nodes; they are not values in the key members of the items in the tree. If an item is to be inserted into the tree whose key data member is less than the key data member in node 1 but greater than the key data member in node 5, where would it be inserted?

Node 5's right child

Examine the following binary search tree and answer the question. The numbers on the nodes are labels so that we can talk about the nodes; they are not values in the key members of the items in the tree. If node 1 is to be deleted, the value in which nodes could be used to replace it?

None of these answers is correct

If a priority queue is being implemented using a linked list of values sorted by priority (in descending order), what is the Big-O complexity of the Dequeue operation?

O(1)

The order of a postorder traversal of a binary search tree is:

O(N)

What is the complexity order of heap sort?

O(N*logN)

If a priority queue is being implemented using a heap, what is the Big-O complexity of the Dequeue operation?

O(logN)

The destructor for a binary search tree should use which traversal for the best efficiency?

Postorder

What is the node in a binary tree that has no parent?

Root

When a client declares an object of a templated class SomeClass, which is the correct syntax if ItemType is int?

SomeClass<int> myClass

For which sorting algorithm is the following true? After I iterations, Data[0]..Data[I-1] is sorted AND the elements in Data[I]..Data[N-1] are greater than the elements in Data[0]...Data[I-1].

Straight selection

A full binary tree has all the leaf nodes on the same level, and every nonleaf node has two children.

T

A header node is a placeholder node at the beginning of a list used to simplify list processing.

T

A recursive function must always contain both a base case and a general case, although the base case may be empty.

T

A stable sort preserves the original ordering of elements with identical keys.

T

Given the following recursive formula, the value of F(2) is 7. F(N) = 2 + F(N - 1), if N > 1 andF(N) = 5, if N = 1

T

Half the nodes in a complete binary tree are leaf nodes.

T

Heap sort does not use additional memory to sort the input array.

T

In a directed graph, every edge has a direction.

T

In an array-of-records linked list implementation, there are always at least two lists.

T

In serial processing, one operation is performed at a time.

T

Inserting into and deleting from a binary search tree can programmed recursively.

T

Inserting into and deleting from a binary search tree cannot be programmed iteratively.

T

Operations on a binary search tree are a good use of recursion.

T

ReheapDown is used in heap sort to maintain the heap order of the unsorted elements.

T

Sorting an array of pointers or a linked list may be slow due to not taking advantage of caching.

T

Tail recursion often indicates that the problem could be solved more efficiently using iteration.

T

The fastest sorting algorithms are those in which a list is represented directly as an array rather than a linked list.

T

The maximum number of nodes that can be on level N is 2^N (root is level 0).

T

When a node with only one child is deleted, what replaces the pointer to the deleted node?

The Child

In a function, value parameters do not necessarily protect the contents of the caller's data structures from being affected by execution of the function under which of the following conditions?

The value parameters are pointer variables

In an undirected graph, no edges have a direction.

True

ReheapUp restores the heap property after:

an insertion.

A ____________________ structure is the main control structure in a recursive routine, and a ____________________ structure is the main control structure in an iterative routine.

branching, looping

An adjacency matrix is a way of representing:

edges

When implementing the print operation of a large integer ADT, it is easier to access the nodes:

either way; it doesn't matter

When implementing the arithmetic operations for a large integer ADT, it is easier to access the nodes:

from right to left

Read the following code and determine what goes into blank #7. // In TreeType.h struct TreeNode; class TreeType { public: // The prototypes of the functions go here void PutItem(int item); private: TreeNode* root; }; #include "TreeType.h" // In the implementation file: struct TreeNode { int info; TreeNode* left; TreeNode* right; }; void TreeType:: PutItem(int item) { Insert(root, item); } void Insert(________ tree, int item) // 1 { if (______________) // 2 { tree = ____________; // 3 tree->info = ________; // 4 __________ = NULL; // 5 tree->right = ________; // 6 } else if (item < tree->info) _________________; // 7 else _________________; // 8 }

insert(tree->left,item)

Heap sort does not use a separate heap (e.g., an object of type HeapType) because:

it is more memory efficient to turn the input array into an array-based heap.

Read the following code and determine what goes into blank #2. TreeNode<ItemType>* Find(TreeNode<ItemType>* tree, ItemType, item) // non-recursive version // Pre: tree has been created. // Post: If item is in tree (a binary search tree), return a pointer // to the node; else return NULL { TreeNode<ItemType>* tempPtr = tree; bool found = false; while (____________) // 1 { switch (___________) // 2 { case EQUAL : _______________; // 3 break; case GREATER : ______________; // 4 break; case LESS : ______________; // 5 break; } } return __________; // 6 }

item.ComparedTo(tempPtr->info)

Suppose that a table is an array of structs used to implement a linked list, as shown below. Each component of the table contains an information field and a pointer to the next element in a linked list. (The pointer values are indices of table, and -1 signifies the end of a list.) What is the list of characters starting with table[5]?

jfclbd

Read the following code and determine what goes in blank #1. int Length(NodeType* list) // Pre: list has been initialized. // Post: Length returns the number of elements in list. No counter is kept as // part of the list. This function is recursive. { if (______________) // 1 _______________; // 2 else_________________ // 3 }

list == NULL

Given the following list, show the contents of the run-time stack when Insert(listNode, 14) is executed. R0 is the initial, non-recursive call; R1 is the recursive call. The question number corresponds to the call. What does the sixth call put on the stack?

listNode = NULL and return = R1

Given the following list, show the contents of the run-time stack when Insert(listNode, 14) is executed. R0 is the initial, non-recursive call; R1 is the recursive call. The question number corresponds to the call. What does the second call put on the stack?

listNode->info = 6 and return = R1

Read the following code and determine what goes in blank #5. // Recursive binary search algorithm bool BinarySearch (ItemType info[], ItemType item,int fromLocation, int toLocation) { if (fromLocation ________ toLocation) // 1 return _____________; // 2 else { int midPoint; midPoint = (_________ + toLocation) / 2; // 3 if (item < info[midPoint]) return BinarySearch(info, item, fromLocation, __________); // 4 else if (item == info[midPoint]) return true; else return BinarySearch(info, item, ____________, toLocation); // 5 } }

midpoint + 1

Read the following bubble sort algorithm and determine the correct answer for blank #4. template<class ItemType> void BubbleUp(ItemType values[], int startIndex, int endIndex) // Post: Adjacent pairs that are out of order have been switched // between values[startIndex]..values[endIndex] beginning at // values[endIndex]. { for (int index = _________; index > startIndex; index--) // 1 if (values[index] ___________ values[index-1]) // 2 Swap(values[index], values[index-1]); } template<class ItemType> void BubbleSort(ItemType values[], int numValues) // Post: The elements in the array values are sorted by key. { int current = ____________; // 3 while (current < numValues - 1) { BubbleUp(values, current, _____________); // 4 current++; }

numValues-1

The first nonleaf node of a complete binary tree (such as one used for a heap) that contains numValues total elements is found at position:

numValues/2 - 1

The order of inserting into a degenerate tree is:

o(N)

A stable sort is one that:

preserves the orignal ordering of elements with identical keys

The NlogN sort that you should NOT use when the data is almost sorted is:

quick sort

Read the following code and determine what goes in blank #3. int Length(NodeType* list) // Pre: list has been initialized. // Post: Length returns the number of elements in list. No counter is kept as // part of the list. This function is recursive. { if (______________) // 1 _______________; //2 else_________________ // 3 }

return Length(list->next) + 1

The data structure that keeps track of activation records during the execution of a program is the:

run-time stack

Read the following code and determine what goes into blank #2. template<class VertexType> void ShortestPath(GraphType<VertexType> graph, VertexType startVertex) { using namespace std; // ItemType is the type for items that are added to the priority queue. // It has 3 fields: VertexType toVertex, VertexType fromVertex, int distance // It overloads the <, == and <= operators, which compare the distance fields. ItemType item; int minDistance; PQType<ItemType> pq(10); // Assume at most 10 vertices. QueType<VertexType> vertexQ; VertexType vertex; graph.ClearMarks(); // Initialize the first item item.toVertex = _____; // #1 item.fromVertex = _____; // #2 item.distance = 0; pq.Enqueue(item); cout << "Last Vertex Destination Distance" << endl; cout << "-----------------------------------" << endl; do { pq.Dequeue(item); if (______) // #3 { graph.MarkVertex(item.toVertex); cout << item.fromVertex; cout <<

startVertex

Read the following code and determine what goes into blank #1. TreeNode<ItemType>* Find(TreeNode<ItemType>* tree, ItemType, item) // non-recursive version // Pre: tree has been created. // Post: If item is in tree (a binary search tree), return a pointer // to the node; else return NULL { TreeNode<ItemType>* tempPtr = tree;bool found = false; while (____________) // 1 { switch (___________) // 2 { case EQUAL : _______________; // 3 break; case GREATER : ______________; // 4 break; case LESS : ______________; // 5 break; } } return __________; // 6 }

tempPtr != NULL

To use the template construct, you must put which of the following phrases before the class definition?

template<class ItemType>

A recursive version of an insert operation that takes the external pointer of a list as a parameter works only if:

the list is passed by reference

The syntax for overloading an operator in C++ is:

the word operator followed by the symbol to be overloaded


Conjuntos de estudio relacionados

B.3.4 Network+ Domain4: Network Security Part 2

View Set

Los avances tecnológicos Sustantivos

View Set

Anatomy & Physiology Review pt 1.

View Set

CH 8 Specialized Audit Tools: Attributes Sampling, Monetary Unit Sampling, and Data Analytics Tools

View Set

Bio 108 Master Biology 9 - Chapter 15

View Set