CSCI 2380 Exam 3

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

In a bubble sort, the smaller elements move toward the bottom, and the larger elements move toward the top of the list. a. True b. False

B. False

The sequential search algorithm uses a(n) ____ variable to track whether the item is found. a. int b. bool c. char d. double

B. bool

What this output? cout << Fun(2,2); int Fun(int m, int n) { if (m == 0) return n; if (n == 0 && m > 0) return Fun(m - 1, 1); else return Fun(m-1, Fun(m,n-1)); }

1

What is the worst case big-Oh run time of: ___ 1. Binary Search ___ 2. Insertion Sort ___ 3. Merge Sort ___ 4. Quick Sort (pivot choice: first item)

1. 2. 3. O(nlog2n) 4. O(n^2)

___ 1. The best case running time of merge sort is ___ 2. The best case running time of quick sort is ___ 3. The worst case running time of bubble sort is ___ 4. The best case running time of insertion sort is a. O(nlog(n) b. O(n2) c. O(1) d. O(n)

1. A 2. A 3. B 4. D

Select the correct answer that would fill out the blank: ___ 1. log n2 = __(n) ___ 2. n = __(log n2) ___ 3. n log n + 5 = __(log n) ___ 4. 15 = __(log log 10000) a. Ω b. ϴ c. O

1. C 2. A 3. B

Assuming the following list declaration, which element is at the position 0 after the first iteration of selection sort? int list[] = {16, 30, 24, 7, 62, 45, 5, 55} a. 5 b. 7 c. 16 d. 62

A. 5

What this output? cout << Fun(3,2) int Fun(int m, int n) { if (m == 0 || n == 0) return m+2; return Fun(n, m - 1) + Fun(m-1, n- 1) + 2; }

35

If n = 1000, then to sort the list, selection sort makes about 50,000 key comparisons. a. True b. False

B. False

int puzzle(int start, int end) { if (start > end) return start - end; else if (start == end) return start + end; else return end * puzzle(start + 1, end - 1); } What is the output of the following statement? cout << puzzle(3, 7) << endl; a. 10 b. 21 c. 42 d. 421

420

In a binary search, first, the search item is compared with the last element of the list. a. True b. False

B. False

Consider the accompanying definition of a recursive function. int puzzle(int start, int end) { if (start > end) return start - end; else if (start == end) return start + end; else return end * puzzle(start + 1, end - 1); } What is the output of the following statement? cout << puzzle(5, 10) << endl; a. 720 b. 5040 c. 5760 d. 10800

A. 720

Which of the following solution methods would be the best choice for a mission control system? a. Iterative b. Direct recursive c. Indirect recursive d. Infinite recursive

A. Iterative

____ control structures use a looping structure, such as while, for, or do...while, to repeat a set of statements. a. Iterative b. Recursive c. Procedural d. Object

A. Iterative

Which of the following solutions is easier to construct for the Tower of Hanoi problem? a. Recursive b. Iterative c. Procedural d. Step-by-step

A. Recursive

During the sorting phase of insertion sort, the array containing the list is divided into two sublists, sorted and unsorted. a. True b. False

A. True

The binary search algorithm can be written iteratively or recursively. a. True b. False

A. True

The sequential search algorithm does not require that the list be sorted. a. True b. False

A. True

Consider the following definition of the recursive function mystery. int mystery(int first, int last) { if (first > last) return 0; else if (first == last) return first; else return first + mystery(first + 1, last - 1); } What is the output of the following statement? cout << mystery(6, 10) << endl; a. 13 b. 21 c. 40 d. 42

B. 21

For a list of length n, selection sort makes ____ item assignments. a. n(n - 1)/2 b. 3(n - 1) c. 3(n) d. 4(n + 1)

B. 3(n-1)

Assume that list consists of the following elements. What is the result after bubble sort completes? int list[] = {2, 56, 34, 25, 73, 46, 89, 10, 5, 16}; a. 89 73 56 46 34 25 16 10 5 2 b. 2 56 34 25 5 16 89 46 73 c. 2 5 10 16 25 34 46 56 73 89 d. 2 10 16 25 34 46 56 73 89

C. 2 5 10 16 25 34 46 56 73 89

int mystery(int list[], int first, int last) { if (first == last) return list[first]; else return list[first] + mystery(list, first + 1, last); } Given the declaration: int alpha[5] = {1, 4, 5, 8, 9}; what is the output of the following statement? cout << mystery(alpha, 0, 4) << endl; a. 1 b. 18 c. 27 d. 35

C. 27

Consider the following list: int list[] = {4, 8, 19, 25, 34, 39, 45, 48, 66, 75, 89, 95} When performing a binary search, the target is first compared with ____. a. 4 b. 25 c. 39 d. 95

C. 39

void print(int num) { if (num > 0) { cout << num << " "; print(num - 1); } } What is the output of the following statement? print(4); a. 0 1 2 3 4 b. 1 2 3 4 c. 4 3 2 1 d. 4 3 2 1 0

C. 4 3 2 1

If every recursive call results in another recursive call, then the recursive function (algorithm) is said to have ____ recursion. a. unlimited b. indefinite c. infinite d. tail

C. Infinite

How many needles are used in the Tower of Hanoi problem? a. one b. two c. three d. four

C. Three

In a bubble sort for list of length n, the first step is to compare elements ____. a. list[0] and list[n] b. list[0] and list[n-1] c. list[0] and list[1] d. list[n-1] and list[n+1]

C. list[0] and list[1]

When working with the unsorted portion of a list, the second step in a selection sort is to ____. a. divide the list into two parts b. move the smallest element to the top of the list (position 0) c. move the smallest element to the beginning of the unsorted list d. find the smallest element

C. move the smallest element to the beginning of the unsorted list

A sequential search of an n-element list takes ____ key comparisons on average to determine whether the search item is in the list. a. 0 b. n/2 c. n d. n2

C. n

Consider the accompanying definition of a recursive function. int recFunc(int num) { if (num >= 10) return 10; else return num * recFunc(num + 1); } What is the output of the following statement? cout << recFunc(8) << endl; a. 4 b. 8 c. 72 d. 720

D. 720

Which of the following rules should you follow to solve the Tower of Hanoi problem? a. Only two disks can be moved at a time. b. You can remove disks only from the first needle. c. The removed disk must be placed on a smaller disk. d. A smaller disk can be placed on top of a larger disk.

D. A smaller disk can be placed on top of a larger disk.

Consider the accompanying definition of a recursive function. int foo(int n) //Line 1 { //Line 2 if (n == 0) //Line 3 return 0; //Line 4 else //Line 5 return n + foo(n - 1); //Line 6 } //Line 7 Which of the statements represent the general case? a. Statements in Lines 1-6 b. Statements in Lines 3 and 4 c. Statements in Lines 4, 5, and 6 d. Statements in Lines 5 and 6

D. Statements in Lines 5 and 6

Consider the accompanying definition of a recursive function. void printNum(int num) //Line 1 { //Line 2 if (n < 0) //Line 3 cout << "Num is negative" << endl; //Line 4 else if (num == 0) //Line 5 cout << "Num is zero" << endl; //Line 6 else //Line 7 { //Line 8 cout << num << " "; //Line 9 printNum(num - 1); //Line 10 } //Line 11 } Which of the statements represent the general case? a. Statements in Lines 3-11 b. Statements in Lines 5-6 c. Statements in Lines 5-11 d. Statements in Lines 7-11

D. Statements in Lines 7-11

When moving array values for insertion sort, to move list[4] into list[2], first ____. a. move list[2] to list[3] b. delete list[2] c. move list[4] to list[3] d. copy list[4] into temp

D. copy list[4] into temp

In the bubble sort algorithm, the following code accomplishes swapping values in elements at positions index and index + 1. a. list[index] = list[index + 1] list[index + 1] = list[index] b. list[index + 1] = list[index] list[index] = list[index + 1] c. list[index] = temp; list[index] = list[index + 1]; temp = list[index + 1]; d. temp = list[index]; list[index] = list[index + 1]; list[index + 1] = temp;

D. temp = list[index]; list[index] = list[index + 1]; list[index + 1] = temp;

In the Tower of Hanoi recursive program, if needle 1 contains three disks, then the number of moves required to move all three disks from needle 1 to needle 3 is 8.

False

Infinite recursions execute forever on a computer.

False

With recursion, the base case must eventually be reduced to a general case.

False

Given the following tree, write the nodes visited in a pre-order, in-order, and post-order traversal. 0 / \ 1 2 / \ / \ 3 4 5 6 \ / \ 7 8 9

Pre-Order: In-Order: Post-Order:

A definition in which something is defined in terms of a smaller version of itself is called a(n) ____ definition. a. step-wise b. recursive c. member-wise d. iterative

Recursive

Suppose we want to sort the following array. Write down how the subarrays are formed and merged based on the following algorithm. If the algorithm needs a pivot, use the first element. [32, 4, 6, 21, 10, 1, 3, 12] Merge Sort:

The first step in merge sort is to find the middle point of the array to divide it into two halves. (l = left, r = right) mergeSort(arr[], l, r){ if(r > 1){ //Step 1. middle m = l + (r-1) / 2; //Step 2. recursion for first half of array mergeSort(arr, l, m); //Step 3. recursion for second half of array mergeSort(arr, m + 1, r); In this example middle is 0 + (7-1) /2 = 3, the middle index is 3, the left array is [32, 4, 6, 21] right array is [10, 1, 3, 12], further recursive calls give you something like [32, 4] [6, 21] [10, 1] [3, 12], pairs are broken up into single subarrays and then compared against each other, the minimum of the first pair is 4, 4 is added to the sorted array. 6 and 21 are compared and 6 is added to the sorted array, now the first four pairs are sorted after [4, 32] [6, 21]. Now the first elements from each pair are compared, 4 and 6, 4 is smaller so it is added to the sorted left array at index 0. 6 is compared to 21 and added to the sorted left array at index 1. 32 and 21 are now compared and 21 is added to index 2 of the sorted array, since there are no other values to compare, 32 is added at index 4 of the sorted array. left array is now [4, 6, 21, 32]. Now the right subarray goes through the same process, [10, 1] becomes [1, 10], [3, 12] remains [3, 12], 1 is compared to 3 and added at index 0 of the right sorted array, 3 is added at index 1, 10 at index 2 and 12 at index 3, right subarray is now sorted [1, 3, 10, 12]. Now that both subarrays are sorted a comparison of the first element in each list is compared, and the smaller of the two is added at index 0. The remaining elements are compared in sequential order so next 4 and 3 are compared, 3 is added at idx 1, 4, and 10, 4 is added at idx 2, 6, and 10, 6 is added at idx 3, 21, and 10, 10 is added at idx 4, 21 and 12, 12 is added at idx 5, there are no values to compare against the right subarray so the remaining values are added to the sorted array at idx 6 and 7 respectively leaving you with [1, 3, 4, 6, 10, 12, 21, 32]

Every call to a recursive function requires the system to allocate memory for the local variables and formal parameters.

True

In a recursive function, the base case stops the recursion.

True

The following is a valid recursive definition to determine the factorial of a non-negative integer. 0! = 1 1! = 1 n! = n * (n - 1)! if n > 0

True

The following is an example of a recursive function. void print(int x) { if (x > 0) { cout << x << " " ; print (x - 1); } }

True

To design a recursive function, you must determine the base case conditions

True

You can use a recursive algorithm to find the largest element in an array.

True

Suppose we want to sort the following array. Write down how the subarrays are formed and merged based on following algorithm. If the algorithm needs a pivot, use the first element. [32, 4, 6, 21, 10, 1, 3, 12] Quick Sort:

Using the first element of the array "32" as the pivot "q", subarrays are created with [p...q-1] to the left of the pivot and [q+1...r] to the right of the pivot. In this case, 32 is the largest value so the subarrays will be formed as such: p[4, 6, 21, 10, 1, 3, 12] and r[32]. The right subarray will meet the base case here because there are no comparisons to make. The first value of subarray p is 4 which will now be the pivot "q". Now values less than q are moved to subarray p and greater than q are moved to subarray r[1, 3,] 4 [6, 21, 10, 12]. Subarray p is now [1, 3] (sorted) r is now[6, 21, 10, 12], 6 is in its correct position, subarray r is now [21, 10, 12] using 21 as the pivot, values less than 21 are moved to the left of the subarray, [10, 12, 21], [1, 2, 4, 6, 10, 12, 21] the array is sorted.

Let T(n) = 5n^3 + 13n^2 + 4n + 17. Check any true statements below: a. T(n) = O(n4) b. T(n)=O(n3) c. T(n)=O(n2) d. T(n)=O(n) e. T(n)=O(1) f. T(n)=Ω(n2) g. T(n)=Ω(n3) h. T(n)=Ω(n4) i. T(n)=ϴ(n2) j. T(n)=ϴ(n3) k. T(n)=ϴ(n4)

a. True b. True c. False d. False e. False f. True g. True h. True i. False j. True k. False

Draw the BST resulting from inserting the following strings in the given order. Also fill in the blanks about the tree drawn: "apple", "dog", "cat", "egg", "farm", "goat", "hat". The number of leaves in the BST is _________and the height of the BST is______.

apple \ dog / \ cat egg \ farm \ goat \ hat Number of Leaves: 2 Height: 5

The ____ case is the case for which the solution to an equation is obtained directly. a. general b. base c. direct d. tail

base

Which of the following function headings can be used for a recursive definition of a function to calculate the nth Fibonacci number? a. void rFibNum(int a, int b) b. bool rFibNum(int a, int b) c. bool rFibNum(int a, int b, int n) d. int rFibNum(int a, int b, int n)

d. int rFibNum(int a, int b, int n)

Suppose you have a bag that can handle maximum b lb weights. You want to fill the bag with diamonds. Each diamond is d lb and the value of each diamond is 1000000 dollar. Recursively compute how much is the value of your bag after filling it with diamonds. int FillBag(int b, int d) { //start code below }

int Fillbag(int b, int d){ if(b < d){ return 0; } else return 1000000 + Fillbag(b-d, d); }


Set pelajaran terkait

The Bits and Bytes of Computer Networking - Week 1

View Set

Major Themes of the Romantic Period

View Set

NREMT Practice Test Bank - Multiple Choice

View Set

ch 4 Maintaining a safe environment

View Set

Curso de Estudios H - Sistema Politico de UE

View Set

Advanced accounting Chapters 3 & 4

View Set

Stranger-originated Life Insurance (STOLI) and Investor-originated Life Insurance (IOLI)

View Set