Math 140 Exam 2

Ace your homework & exams now with Quizwiz!

Big oh notation develops the -------- of an algorithm. A. The upper bound B. The lower bound C. The average D. The optimal

A. The upper bound

Any problem that can be solved recursively can also be solved iteratively, with a loop. A. True B. False

A. True

Only the wrapper classes can call the compareTo() method A. True B. False

A. True

Push and Pop operations for stacks are constant algorithms A. True B. False

A. True

Recursion is never absolutely required to solve a problem. A. True B. False

A. True

Selection sort is an in-place sort. A. True B. False

A. True

Selection sort is quadratic A. True B. False

A. True

------ scenario of an algorithm happens when the algorithm is working on the easiest input or the minimum number of operations. A. best case B. best time C. average case D. worst case

A. best case

Find the order of growth for finding the largest value in an array with n elements. Assume n is a positive integer. A. n B. n^2 C. 1 D. √n

A. n

The order of growth of which code is not constant? A. Accessing an element of an array B. Push operation on a stack C. Inserting a new Node to a Linked List D. Finding the total of all elements in an array.

D. Finding the total of all elements in an array.

Which option is not correct about worst case of an algorithm. A. Provides a guarantee for all inputs. B. Determined by "most difficult" input. C. Upper bound on cost D. Lower bound on cost

D. Lower bound on cost

What is the order of growth for the following function of the input of size N. (3N^4)/5 + 2N^3 + N A. 1 B. N C. N^4 + N^3 D. N^4

D. N^4

Find the tilde approximation and the order of growth for the following functions N^3/6 - N^2/2 + N/3 A. Tilde approximation: ∼ N^3/6 Order of growth: N^3/6 B. Tilde approximation: ∼N/3 Order of growth: N^3/6 C. Tilde approximation: ∼N^3 Order of growth: N^3 D. Tilde approximation: ∼N^3/6 Order of growth: N^3

D. Tilde approximation: ∼N^3/6 Order of growth: N^3

Selection-sort sorts an array of N elements by repeating the following steps: Find the next ------ item in the array and placing it ----------. A. Maximum, in the first element of the array. B. minimum, in the middle of the array. C. minimum, in the rightmost element of the array. D. minimum, where it belongs in the array.

D. minimum, where it belongs in the array.

Which of the following algorithms describes Shell Sort? A. 1 - Select the highest value of gap h. 2 - Apply insertion sort on the elements that are h elements apart. 3 − Reduce the value of h and repeat until complete list is sorted B. 1 - Select the lowest value of gap h. 2 - Apply insertion sort on the elements that are h elements apart. 3 − Increment the value of h and repeat until complete list is sorted C. 1 - Select the highest value of gap h. 2 - Divide the list to h subarrays. 3 - Apply selection sort on each subarrays. 4 - Reduce the value of h and repeat until complete list is sorted D. 1 - Select the lowest value of gap h. 2 - Divide the list to h subarrays. 3 - Apply insertion sort on each subarrays. 4 - Increase the value of h and repeat until complete list is sorted

A. 1 - Select the highest value of gap h. 2 - Apply insertion sort on the elements that are h elements apart. 3 − Reduce the value of h and repeat until complete list is sorted

What is the order of growth for the following function of the input size of N. (1 + 1/N)(1 + 2/N) A. 1 B. 1/N C. 1/N^2 D. N

A. 1

How many instructions as a function of N do we have in the following nested loop: for(row=0;row<N;row++) { for (col=o;col<N;com++) { p=a[row][col] } } A. 3N^2 + 4N + 2 B. 3N + 4 C. N^2 D. 3N^3 + 4N^2 + 2N

A. 3N^2 + 4N + 2

Sort the following list into ascending order. R, O, Q, P, T, S. A. O, P, Q, R, S, T B. O, P, Q, R, T, S C. O, R, Q, P, S, T D. O, Q, P, R, S, T

A. O, P, Q, R, S, T

Estimate the order of growth for the following method as a function of n. Do not multiply your answer by a coefficient not equal to 1. Example: If the number of the operations equals n/2 your answer should be the order of growth n. public static int getSum2(int[] arr) { int sum = 0; int n = arr.length; for (int i = 0; i < n; i++) for (int j = n ; j > 0; j = j - 2) sum += arr[j]; return sum; } A. n^2 B. lg(n) C. √n D.n

A. n^2

Which of the following makes class Animals Comparable? A. public class Animals implements Comparable<Animals> B. public class Animals extends Comparable<Animals> C. public class Animals implements CompareTo<Animals> D. public class Animals implementing Comparable<Animals>

A. public class Animals implements Comparable<Animals>

A method that calls itself is a ________ method. A. recursive B. redundant C. binary D. derived

A. recursive

The order of growth of which code is constant? N is input size. A. int COUNT = N; int y = 0; for(int i = 0; i < COUNT; i++) y += i; B. int[] arr = new int[30]; for(int i = 0; i < arr.length; i++) arr[i] = 0; C. int sum = 0; for (int i = 0; i < N/2; i+=2) sum += i D. Scanner sc = new Scanner(System.in); string str1 = sc.nextLine(); string str2 = sc.nextLine(); string str3 = concat(str1, str2)

B. int[] arr = new int[30]; for(int i = 0; i < arr.length; i++) arr[i] = 0;

What is the final gap value in Shell sort? A. 0 B. 1 C. 2 D. 4

B. 1

A recursive method can have only one base case. A. True B. False

B. False

An algorithm finds the minimum number in a 2-D array. This is a time constant algorithm. A. True B. False

B. False

Concatenation is a constant algorithm A. True B. False

B. False

Insertion Sort has less exchanges than Selection Sort A. True B. False

B. False

We consider the CPU type when we analyze an algorithm, A. True B. False

B. False

When in the same try statement you are handling multiple exceptions and some of the exceptions are related to each other through inheritance, you should handle the more general exception classes before the more specialized exception classes. A. True B. False

B. False

Without a base case, a recursive method will call itself only once and stop. A. True B. False

B. False

Which method runs fastest for an array with all keys identical, selection sort or insertion sort A. Selection Sort. B. Insertion Sort

B. Insertion Sort

Which of the following statements is true about the order of growth of linear and binary search algorithms? A. Linear search is linear and Binary search is quadratic. B. Linear search is linear and Binary search is logarithmic. C. Linear search and Binary search are both logarithmic. D. Linear search and Binary search are both linear.

B. Linear search is linear and Binary search is logarithmic.

Traversing a stack is what big O order operation A. N^2 B. N C. logN D.NlogN

B. N

What is The big 'O' notation of Insertion Sort? A. N B. N^2 C. Nlog^N D. √n

B. N^2

What is The big 'O' notation of Selection Sort? A. N B. N^2 C. Nlog^N D. √n

B. N^2

An algorithm is developed to find a name starting with a given letter in a list. What is the worst case scenario to find a name starting with letter 'X' in a list of student names. A. The first name in the list begins with "X." B. No names in the list begin with "X" C. The last name in the list begins with "X." D. No answer text provided.

B. No names in the list begin with "X"

Find the error in the following program: public class FindTheError { public static void main(String[] args) { myMethod(0); } public static void myMethod(int num) { System.out.print(num + " "); myMethod(num + 1); } } A. There is no error in this code. B. There is no base case for myMethod. C. myMethod can't be a static method. D. myMethod should return an integer.

B. There is no base case for myMethod.

What is the purpose of exch(Comparable[] a,int i, int j)? A. To swap the first element of the array with the last element of the array. B. To swap two elements at a time. C. To swap the first element with the middle element. D. all the elements

B. To swap two elements at a time.

The ________ is at least one case in which a problem can be solved without recursion. A. recursive case B. base case C. termination point D. point of absolution

B. base case

This method can be used to retrieve the error message from an exception object. A. invocation list B. call stack C. call list D. list trace

B. call stack

Consider the following list: 40 35 80 74 22 Which of the following shows the steps in the selection sort? A. exchange elements at index 4 and 0: 22, 80, 35, 74, 40 exchange elements at index 2 and 1: 22, 35, 80, 74, 40 exchange elements at index 4 and 2: 22, 35, 40, 74, 80 exchange elements at index 3 and 3: 22, 35, 40, 74, 80 B. exchange elements at index 4 and 0: 22, 35, 80, 74, 40 exchange elements at index 1 and 1: 22, 35, 80, 74, 40 exchange elements at index 4 and 2: 22, 35, 40, 74, 80 exchange elements at index 3 and 3: 22, 35, 40, 74, 80 C. exchange elements at index 1 and 0: 22, 40, 74, 80, 35 exchange elements at index 4 and 1: 22, 35, 74, 80, 40 exchange elements at index 4 and 2: 22, 35, 40, 80, 74 exchange elements at index 4 and 3: 22, 35, 40, 74, 80 D. exchange elements at index 1 and 0: 22, 35, 74, 80, 40 exchange elements at index 1 and 1: 22, 35, 74, 80, 40 exchange elements at index 4 and 2: 22, 35, 40, 80, 74 exchange elements at index 4 and 3: 22, 35, 40, 74, 80

B. exchange elements at index 4 and 0: 22, 35, 80, 74, 40 exchange elements at index 1 and 1: 22, 35, 80, 74, 40 exchange elements at index 4 and 2: 22, 35, 40, 74, 80 exchange elements at index 3 and 3: 22, 35, 40, 74, 80

In the following code that uses recursion to find the factorial of a number, what is the base case? private static in factorial(int n) { if(n == 0) return 1; else return n * factorial(n-1) } A. factorial(int n) B. if(n == 0) return 1; C. else return n * factorial(n-1) D. Cannot tell from this code

B. if(n == 0) return 1;

In the following code that uses recursion to find the greatest common divisor of a number, what is the base case? public static int gcd(int x, int y) { if(x % y == 0) return y else return gcd(y , x % y) } A. gcd(int x, int y) B. if(x % y == 0) return y C. else return gcd(y , x % y) D. Cannot tell from this code

B. if(x % y == 0) return y

What's the best case and the worst case for Insertion Sort?

Best case: If the array is in ascending order, insertion sort makes N - 1 compares and 0 exchanges Worst Case: If the array is in descending order (and no duplicates), insertion sort makes ~ ½ N^2 compares and ~ ½ N^2 exchanges.

What's the best case and the worst case for Selection Sort?

Best case: array is already sorted O(N^2) Worst Case: array is unsorted or sorted in descending order (N(N-1))/2

Which algorithm defines the insertion sort? A. public static void sort(Comparable[] list) { int size = list.length; for (int nextPos = 1; nextPos < size; nextPos++) for (int beforePos = nextPos; beforePos > 0 && less(list[beforePos] list[beforePos + 1]); beforePos++) exch(list, beforePos, beforePos + 1); } B. public static void sort(Comparable[] list) { int size = list.length; for (int nextPos = 0; nextPos <= size; nextPos++) for (int beforePos = nextPos; beforePos > 0 && less(list[beforePos -1], list[beforePos + 1]); beforePos++) exch(list, beforePos - 1, beforePos + 1); } C. public static void sort(Comparable[] list) { int size = list.length; for (int nextPos = 1; nextPos < size; nextPos++) for (int beforePos = nextPos; beforePos > 0 && less(list[beforePos], list[beforePos - 1]); beforePos--) exch(list, beforePos, beforePos - 1); } D. public static void sort(Comparable[] list) { int size = list.length(); for (int nextPos = size; nextPos > 1; nextPos--) for (int beforePos = nextPos + 1; beforeP

C. public static void sort(Comparable[] list) { int size = list.length; for (int nextPos = 1; nextPos < size; nextPos++) for (int beforePos = nextPos; beforePos > 0 && less(list[beforePos], list[beforePos - 1]); beforePos--) exch(list, beforePos, beforePos - 1); }

Which statement describes the steps in a Binary Search algorithm? A. 1. BS checks the first element in the list, if the key is equal to the first element then the search stops. 2. Else BS check the last element. If the last element is equal to the key then the search stops. 3. BS repeats step 1 and 2 till the key is found. B. 1. BS algorithm finds the middle element in the list. 2. If the key is less than the middle element, BS continues on the right sublist. 3. Else if the key is greater than the middle element, BS continues on the left sublist. 4. Else the key is equal to the middle element of the list. C. 1. BS algorithm finds the middle element in the list. 2. If the key is less than the middle element, BS continues on the left sublist. 3. Else if the key is greater than the middle element, BS continues on the right sublist. 4. Else the key is equal to the middle element of the list. D. 1. BS starts from the first element in the list, 2. If the first element is equal to the key, the search stops. 3. Else BS checks the rest of the list till finds the key or the list is over.

C. 1. BS algorithm finds the middle element in the list. 2. If the key is less than the middle element, BS continues on the left sublist. 3. Else if the key is greater than the middle element, BS continues on the right sublist. 4. Else the key is equal to the middle element of the list.

What is the maximum exchanges to sort 7 elements using insertion sort? A. 7 B. 14 C. 21 D. 70

C. 21

How many swaps do you perform to sort the following list using Insertion sort? Character[] arr2 = {'R' ,'R' ,'R' ,'G' ,'R' ,'S' ,'K' }; A. 6 B. 7 C. 8 D. 9

C. 8

Apply shell sort on Integer[] a = {9 ,10 , 8,19, 12, 18, 11, 15, 16, 17, 14, 13 , 6 ,7 }; with h = 5. Show the answer just after h= 5; A. 6, 7, 8, 9, 10, 13, 11, 12, 16, 17, 14, 18, 19, 15 B. 6, 7, 8, 13, 9, 10, 11, 15, 12, 17, 14, 19, 16, 18 C. 9, 10, 6, 7, 12, 14, 11, 8, 16, 17, 18, 13, 15, 19 D. 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19

C. 9, 10, 6, 7, 12, 14, 11, 8, 16, 17, 18, 13, 15, 19

Constant time operations Which of the following statements is true with reference to an algorithm's runtime? A. The runtime of an algorithm is independent of the speed of the processor. B. The runtime of an algorithm is analyzed in terms of nanoseconds. C. A single algorithm can execute more quickly on a faster processor. D. The runtime of an algorithm is independent of the input values.

C. A single algorithm can execute more quickly on a faster processor.

Assume we wrote Insertion Sort using the following method: public static void sort(Comparable[] list) { /*Insertion sort is implemented here*/ } Which line of the following code gives you a compiler error: 1 Character[] chars = {'R', 'R', 'R', 'G', 'R', 'S', 'K'}; 2 int[] nums = {6, 5, 4, 3, 2, 1, 0}; 3 4 File[] files = new File[4]; 5 files[0] = new File("Names.txt"); 6 files[1] = new File("Numbers.txt"); 7 files[2] = new File("Grades.txt"); 8 files[3] = new File("Documents.txt"); 9 10 Insertion.sort(chars); 11 Insertion.sort(nums); 12 Insertion.sort(files); A. Line 4-8. B. Line 10 C. Line 11 D. line 12

C. Line 11

What operation does the following code implement? public static int mystery(int a, int b) { if (b == 0) return 0; if (b%2 == 0) return mystery(a+a,b/2); return mystery(a+a,b/2)+a; } A. Additon B. Subtraction C. Multiplication D. Division

C. Multiplication

Using selection sort on a list of size N, what is the maximum number of exchanges? Hint: The maximum number of exchanges happens when we need to exchange any particular item of the list. A. 1 exchange B. N/2 exchanges C. N exchanges D. N^2 exchanges.

C. N exchanges

What is the order of growth for the following function of input size; 2N^2 + 1 A. 2N^2 + 1 B. 2N^2 C. N^2 D. 1

C. N^2

O notation The number of instructions in the most time consuming part of a code as a function of input size N is: 2N^3 − 6N^2 + N What is the order of growth of the code? A. 2N^3 B. 2N^3 − 6N^2 C. N^3 D. N^2

C. N^3

Find the error in the following code segment: catch (FileNotFoundException e) { System.out.println("File not found."); } try { File file = new File("MyFile.txt"); Scanner inputFile = new Scanner(file); } A. No Error B. Used a wrong exception C. The try block must appear first. D. no need for {}

C. The try block must appear first.

The purpose of analyzing algorithms is to avoid algorithms with _____. A. low memory usage and high runtime B. high memory usage and low runtime C. high memory usage and high runtime D. high computational efficiency

C. high memory usage and high runtime

Give the order of growth (as a function of N) of the running times of the following code fragment. int sum = 0; for (int i = 1; i < N; i *= 2) for (int j = 0; j < N; j++) sum++; A. linear O(N) B. logarithmic or O(lg^N) C. linearithmic or O(Nlg^N) D. O(N^2) quadratic or

C. linearithmic or O(Nlg^N)

You use this statement to throw an exception manually. A. try B. generate C. throw D. System.exit(0)

C. throw

What are the helper methods that we used in our sorting algorithms. A. // is v < w ? private static int less(Comparable v, Comparable w) { return v.compareTo(w) < 0; } // exchange a[i] and a[j] private static void exch(Object[] a, int i, int j) { Object swap = a[i]; a[j] = a[i]; a[i] = swap; } B. // is v < w ? private static int less(int v, int w) { return v<w; } // exchange a[i] and a[j] private static void exch(Object[] a, int i, int j) { Object swap = a[j]; a[i] = a[j]; a[j] = swap; } C. // is v < w ? private boolean less(Object v, Object w) { return v.compareTo(w) < 0; } // exchange a[i] and a[j] private static void exch(Comparable[] a, Comparable i, Comparable j) { Comparable swap = a[i]; a[i] = a[j]; a[j] = swap; } D. // is v < w ? private static boolean less(Comparable v, Comparable w) { return v.compareTo(w) < 0; } // exchange a[i] and a[j] private static void exch(Object[] a, int i, int j) { Object swap = a[i]; a[i] = a[j]; a[j] = swap; }

D. // is v < w ? private static boolean less(Comparable v, Comparable w) { return v.compareTo(w) < 0; } // exchange a[i] and a[j] private static void exch(Object[] a, int i, int j) { Object swap = a[i]; a[i] = a[j]; a[j] = swap; }

Given Character[] arr2 = {'Q' ,'U' ,'I' ,'Z' ,'T' ,'E' ,'N' }; What is the order of the elements during insertion sort? A. E, U, I, Z, T, Q, N E, I, U, Z, T, Q, N E, I, N, Z, T, Q, U E, I, N, Q, T, Z, U E, I, N, Q, T, Z, U E, I, N, Q, T, U, Z B. E I U Z T Q N E I U Z T Q N E I U Z T Q N E I T U Z Q N E I Q T U Z N E I N Q T U Z E I N Q T U Z C. U Q I Z T E N U Q I Z T E N Z U Q I T E N Z U T Q I E N Z U T Q I E N Z U T Q N I E Z U T Q N I E D. Q U I Z T E N I Q U Z T E N I Q U Z T E N I Q T U Z E N E I Q T U Z N E I N Q T U Z E I N Q T U Z

D. Q U I Z T E N I Q U Z T E N I Q U Z T E N I Q T U Z E N E I Q T U Z N E I N Q T U Z E I N Q T U Z

Which code is selection sort algorithm? A. public static void sort(Comparable[] list) { for (int fill = 0; fill < list.length; fill++) { int minPos = fill; for (int next = fill + 1; next < list.length; next--) if (less(list[minPos], list[fill])) minPos = fill; exch(list, next, minPos); } } B. public static void sort(Comparable[] list) { for (int fill = 0; fill < list.length; fill++) { for (int next = fill + 1; next < list.length; next++) { int minPos = fill; if (less(list[next], list[minPos])) minPos = next; } exch(list, next, fill); } } C. public static void sort(Comparable list) { for (int fill = 0; fill < list.length(); fill++) { //work with index int minPos = fill; for (int next = fill + 1; next < list.length; next--) if (less(list[fill], list[minPos])) minPos = next; exch(list, fill, minPos);

D. public static void sort(Comparable[] list) { for (int fill = 0; fill < list.length; fill++) { int minPos = fill; for (int next = fill + 1; next < list.length; next++) if (less(list[next], list[minPos])) minPos = next; exch(list, fill, minPos); } }

Consider the following code; What is the value of mystery(2,14)? public static int mystery(int a, int b) { if (b == 0) return 0; if (b%2 == 0) return mystery(a+a,b/2); return mystery(a+a,b/2)+a; } A. 7 B. 16 C. 12 D. 28

D. 28

In the worst case, how many letters do you need to check to find a specific letter in an ordered list of English letters. A. 11 B. 26 C. 9 D. 4

D. 4

Consider the following list. Using linear search, how many characters needs to be checked to locate the letter 'M'? 'A', 'C', 'E', 'G', 'I', 'K', 'M', 'O', 'Q', 'S', 'U', 'W', 'Y' A. 1 B. 3 C. 5 D. 7

D. 7

Look at the following program and tell what it will output when run: public class ExceptionTest { public static void main(String[] args) { int number; String str; try { str = "xyz"; number = Integer.parseInt(str); System.out.print("A"); } catch(NumberFormatException e) { System.out.print("B"); } catch(IllegalArgumentException e) { System.out.print("C"); } System.out.print("D"); } } A. ABCD B. ABD C. AB D. BD

D. BD

Criticize the following recursive function: public static String exR2(int n) { String s = exR2(n-3) + n + exR2(n-2) + n; if (n <= 0) return ""; return s; } A. This code doesn't have any problems. B. In line String s = exR2(n-3) + n + exR2(n-2) + n; we can't call the same method twice. C. return ""; is error D. Base case should be the first statement.

D. Base case should be the first statement.

Estimate the order of growth for the following method as a function of n. Do not multiply your answer by a coefficient not equal to 1. Example: If the number of operations equals n/2 your answer should be the order of growth = n. public static int getSum(int[] arr) { int sum = 0; int n = arr.length; for (int i = 0; i < Math.sqrt(n); i++) { sum += arr[i]; } return sum; } A. n B. n^2 C. lg(n) D.√n

D.√n

Tilde approximate is the same as Big Oh notation. A. True B. False

False

You cannot have more than one catch clause per try statement. A. True B. False

False

Define Selection Sort

First, find the smallest item in the array and exchange it with the first entry (itself if the first entry is already the smallest). Then, find the next smallest item and exchange it with the second entry. Continue in this way until the entire array is sorted.

What is the big O order for Shell Sort

NlogN

Define Insertion Sort

The algorithm that people often use to sort bridge hands is to consider the cards one at a time, inserting each into its proper place among those already considered (keeping them sorted). In a computer implementation, we need to make space to insert the current item by moving larger items one position to the right, before inserting the current item into the vacated position.

What is the equation for a Insertion Sort?

[(N+1)N]/2


Related study sets

Chapter 6: The Nonverbal Dimension of Communication

View Set

Chemistry-Unit 3: Investigating Patterns in the Periodic Table

View Set

ECON MID TERM HW QUESTIONS 4(part 1)

View Set

Chapter 17 - Fundamentals of Discounted Cash Flow Analysis

View Set