CS210 Midterm Exam II

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

a. {3, 67, 22, 18, 43, empty, empty, empty} *An array-based list would double its allocation size when the current allocated list space needs to increased for accommodating more elements. With 5 integers added to the array-based list, the allocated list size would become 8, the list will become: {3, 67, 22, 18, 43, empty, empty, empty} Refer to zyBook 14.22

An integer array-based list (dynamic array) is created with an initial allocated size 2: {empty, empty}, meaning it has two empty slots in the allocated memory to start with, what would the list become after appending the following sequence of integers: 3, 67, 22, 18, 43 a. {3, 67, 22, 18, 43, empty, empty, empty} b. {3, 67, 22, 18, 43} c. {empty, empty, empty, 3, 67, 22, 18, 43} d. {3, 67, 22, 18, 43, empty, empty}

d. key % 50 *key % 50 would produce fewest collisions, the larger the table size, the less collisions from key % 50

Consider a hash table with keys 10, 20, 30, 40, 50, and 60. Which hash function produces the fewest collisions? a. key % 5 b. key % 10 c. key % 6 d. key % 50

a. 2 *Using the double hashing formula, ( h1(key) + i * h2(key) ) mod (tablesize) the first probing --> (44 % 5 + 0 * (10 - 44 % 10)) % 8 = 4, bucket 4 has an empty-after-removal flag, so the search continues to the next probing step, second probing --> (44 % 5 + 1 * (10 - 44 % 10)) % 8 = 2, it finds bucket 2 which has an empty-from-start flag, search stops

Consider the following hash table that uses double hashing for probing. Suppose we use key % 5 as the first hash function and 10 - key % 10 as the second hash function. Then, HashSearch(valsTable, 44) probes _____ buckets. The double hashing formula is ( h1(key) + i * h2(key) ) mod (tablesize), i starts from 0 a. 2 b. 3 c. 1 d. 4

a. 2; 0; 1 *HashSearch(newTable, 72) --> 72 % 10 = 2, bucket 2 at this point would have a linked list with 2 list items {12, 72, 22}. Both list items would be compared for finding 72. Number of list items compared would be 2. HashSearch(newTable, 77) --> 77 % 10 = 7, bucket 7 at this point would be empty because the previous HashRemove(newTable, 27) removed the 27 key there. Number of list items compared for searching 77 would be 0. HashSearch(newTable, 63) --> 63 % 10 = 3, bucket 3 at this point would have a linked list with 63, 33, 83. For searching 63, one list item will be compared as key 63 is at the beginning of the list. Number of list items compared for searching 63 would be 1.

Consider the following hash table with chaining, and a hash function of key % 10. The Hash Table starts with the buckets having the values shown in the picture below. After the series of HashInsert or HashRemove operation, how many list items will be compared for the Hash search operations at the end (yellow high-lighted)? a. 2; 0; 1 b. 3; 0; 1 c. 2; 1; 1 d. 2; 0; 2 e. 2; 0; 3

a. 6 *When inserting node or key 2, BSTInsertRecursive would be called when it visits nodes 6, 4, 3; When inserting node or key 10, BSTInsertRecursive would be called when it visits nodes 6, 8, 9; Total number of calls to BSTInsertRecursive would be 6

How many calls are made to the function BSTInsertRecursive when the following operations are executed on the given tree? a. 6 b. 7 c. 4 d. 5

d. 16 *Hexadecimal integers are with base 16, which has 16 possible digit values (0-F), so 16 buckets are needed, refer to zyBook 13.12

The Radix Sort method creates _____ buckets when sorting a list of 256 hexadecimal numbers, with the minimum value of 0x1000 and the maximum value of 0x3000. a. 8 b. 256 c. 0x2000 d. 16

Why the crash? (6 points) When reg.checkStudent function is called, a shallow copy of Student object stu would be passed in as an argument. When this copied object of stu goes out of the scope upon the completion of reg.checkStudent, its destructor will be exercised and its stuID member integer pointer will be deleted. When main function completes, it will try to call the destructor of stu object, which would try to delete the already deleted stuID pointer again, which would result in crash. Fix (4 points) To fix this, a copy constructor needs to be created for class Student, refer to zyBook 5.9 C++ copy constructors section.

The following code would result in a crash, why? How do you fix it? (describe the fix in plain English, or change the existing c++ code)

Answer 1: 3 *11 is the root, 23 is inserted as the right child of 11, 6 is inserted as the left child of 11, 47 is inserted as the right child of 23, 46 is inserted as the left child of 47. Height would be 3 after all insertions. Answer 2: O(N) *O(N) for the worst case scenario, where the tree has a height of N - 1

What is the height of a BST built by inserting nodes in the order 11, 23, 6, 47, 46? 3 The upper bound of the time complexity of a BST insertion algorithm is O(logN) . N is the number of nodes in the tree.

a. 6 a. 10 *The correct answer is 10, as n is passed in as a pointer to Func2 and gets modified

What value would variable i become after the execution of the above snippet: a. this code cannot compile b. 6 c. 5 What value would n become after Func2 call: a. 10 b. 0 c. This code would crash because of passing of an uninitialized pointer to the Func2

a. Because the predecessor could have a left subtree and algorithm needs to use the same recursive way to remove the predecessor *Note there is no right subtree of a predecessor because the predecessor is the rightmost node of the left subtree.

When removing a BST node with two children, if the predecessor is used for helping the removal, why might the removal need to recursively remove the predecessor of the node? The predecessor is the rightmost node of the left subtree. a. Because the predecessor could have a left subtree and algorithm needs to use the same recursive way to remove the predecessor b. Because the predecessor would have one more left child and algorithm needs to use the same recursive way to remove the predecessor c. Because the predecessor could have both left and right subtrees and algorithm needs to use the same recursive way to remove the predecessor d. Because the predecessor could have a right subtree and algorithm needs to use the same recursive way to remove the predecessor

a. LinkedList list; List.append(1); *list object is created on stack, and it will be deallocated automatically (destructor is called) upon going out of the scope of function. Explicit instantiation using the new operator would create the object on the heap, which would need explicit deallocation by calling delete on the object, which would call the object destructor.

Which XXX will call a destructor after invoking the following function? a. LinkedList list; List.append(1); b. LinkedList* list = new LinkedList(); List->append(1); c. LinkedList list = new List(); List.append(1); d. LinkedList& list = *(new LinkedList()); List.append(1);

The following two code snippets will result in memory leaking. In both snippets, the pointers need to be explicitly deallocated by using delete. snippet 1:void StudentClass() { int * ptr = new int(3); }int main() { StudentClass(); return 0; } snippet 2 (studentID pointer needs to be deleted explicitly):class StudentClass {private:int *studentID = nullptr;public:StudentClass(int id) {studentID = new int;*studentID = id;}~StudentClass() {studentID = nullptr;}};int main() { StudentClass* stu1 = new StudentClass(2); delete stu1; return 0; } snippet 3 (loses track of stu1 for deletion): class StudentClass {private:int studentID;public:StudentClass(int id) {studentID = id;}};int main() { StudentClass* stu1 = new StudentClass(2); StudentClass* stu2 = new StudentClass(5); stu1 = stu2;delete stu1; delete stu2; return 0; }

Which of the following code snippets result in a memory leak? (check all apply) Note for each incorrect answer that you select, you would lose the same points allocated for an correct answer. The score cannot go negative.

a. Hash table resizing could involve reallocation of augmented size of the hash table, rehashing of the existing inserted keys and re-inserting them into the new hash table buckets.

Which of the following statements is true about the hash table resizing? a. Hash table resizing could involve reallocation of augmented size of the hash table, rehashing of the existing inserted keys and re-inserting them into the new hash table buckets. b. Hash table resizing would involve augmenting the size of the hash table at existing location, rehashing of the existing inserted keys and re-inserting them into the new hash table buckets. c. Hash table resizing would involve augmenting the size of the hash table at existing location, preserving existing hashed keys at the same buckets. d. Hash table resizing would only involve reallocation of augmented size of the hash table and copying existing items over to the new allocated space.

d. 22, 91, 4, 15, 11 *The ListTraverse recursively traverse the linked list in the reversed traversal order, so it would print out the sequence of numbers in the list in the reversed traversal order: 22, 91, 4, 15, 11 Refer to zyBook 14.15.5

a. 11, 15, 4, 91, 22 b. 22, 91, 4, 11, 15 c. 11, 4, 22, 15, 91 d. 22, 91, 4, 15, 11


Conjuntos de estudio relacionados

Exam 5 - NIP 126 Biliary Disorders Chapter 50

View Set

Public Health Exam 3 Textbook info

View Set

final exam for nutrition ch. 9 and 10, chapter 2

View Set

Identify the accounting assumption, principle, or constraint that describes each situation below.

View Set

chapter 35 assessment of musculoskeletal function

View Set

1.2 How are our Ecological Footprint Affecting Earth?

View Set

REF - Chapter 10 - Defaults & Foreclosures

View Set

test3 elec principles study guide

View Set