Math 140 Exam 2

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

Assume str.substring(1) returns a substring of str begins with the character at index 1 and extends to the end of this string. So, if str = "Hello!" then str. substring(1) will be "ello". What is the output of the following code" System.out.println(exam2("Hello!")); if exam2 is a recursive method as follows: public static String exam2(String str) { -----if(str.length() == 1) --------return str; -----return exam2(str.substring(1) = str.charAt(0); } ____________________________________________________________ A) Hello! B) !alleH C)!elloH D) This code doesn't have a base case.

!olleH

Which code does sorting in Quick Sort?(Not partitioning)

*public* static void sort(Comparable[] a) { sort(a, 0, a.length - 1); } private static void sort(Comparable[] a, int lo, int hi) { if (hi <= lo) return; int j = partition(a, lo, hi); sort(a, lo, j-1); sort(a, j+1, hi); //No partition } ANSWER D

Here you see the order of growth of some algorithms. Sort the order of growths from the fastest algorithm to the slowest algorithm. N is the size of the list. N2,NlgN,N,lgN,1 A) 1, N, lg^N, N^2, Nlg^N B) 1, N, lg^N, Nlg^N, N^2 C) lg^N, N, N^2, Nlg^N, 1 D) 1, lg^N, N, Nlg^N, N^2

1, lg^N, N, Nlg^N, N^2

Which statement describes the steps in a Binary Search algorithm?

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.

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

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

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

13?

Given Integer[] a = {2,3,5,1,6,4}; What is the order of the elements during insertion sort?

2 3 5 1 6 4 2 3 5 1 6 4 1 2 3 5 6 4 1 2 3 5 6 4 1 2 3 4 5 6 1 2 3 4 5 6

How many swaps do you perform to sort the following list using Insertion sort? Character[] arr2 = {'A' ,'K' ,'K' ,'K' ,'B' ,'K'};; A) 3 B) 4 C) 5 D) 6

3

Consider the following list: Integer[] a = {7,8,9,3,4}; Which of the following shows the steps in the selection sort? A) 7 8 9 3 4 7 8 9 3 4 3 7 8 9 4 3 4 7 8 9 3 4 7 8 9 B) 9 8 7 3 4 9 8 7 3 4 9 8 7 4 3 9 8 7 4 3 9 8 7 4 3 C) 3 8 9 7 4 3 4 9 7 8 3 4 7 9 8 3 4 7 8 9 3 4 7 8 9 D) 3, 4, 7, 8, 9

3, 4, 7, 8, 9

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

4

Assume the following array. Which of the follwoing will be the contents of the array before calling the merging method. int mid = lo + (hi - lo) /2; sort(a , aux, lo, mid); sort(a, aux, mid + 1, hi); merge(a, aux, lo, mid, hi); A) 3, 5, 6, 19, 14 ,17, 7, 8 B) 3, 6 ,17, 19, 5, 7, 8, 14 C) 6, 7, 17, 19, 3, 5, 8, 14 D) 7, 8, 17, 19, 3, 5, 6, 14

7, 8, 17, 19, 3, 5, 6, 14

Constant time operationsWhich of the following statements is true with reference to an algorithm's runtime?

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

An algorithm is designed to compress an image based on the numbers of different pixels. The pixels are saved in a 3D array and all of the pixels should be traversed for a best compression result.What is the order of growth of the growth if the size of the 3D array is NNN? A) Linear B) Quadratic C) Cubic D) Constant time alg.

Cubic

We consider the CPU speed when we analyze an algorithm True or False

False

The order of growth of which code is not constant? A) Enqueue operation on a queue B) Pop operation on a stack C) Inserting a new Node to a linked list D) Finding largest object in a list.

Inserting a new Node to a linked list

Which one is not correct about Insertion sort? A) The items to the left of the current index are in sorted order during the sort. B) The worst case of insertion sort is when the case is reversed. C) The best case of insertion sort is when the list is nearly sorted. D) Insertion sort doesn't have a practic al use.

Insertion sort doesn't have a practic al use.

Which of the following statements is true about the order of growth of linear and binary search algorithms?

Linear search is linear and Binary search is logarithmic.

Which option is not correct about the 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

Lower bound on cost

Which of the following algorithms has the same best, average, and worst case runtime complexity? A) Quicksort B) Merge Sort C) Shell Sort D) Insertion Sort

Merge Sort

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.

N exchanges

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

N^2

O notationThe number of instructions in the most time consuming part of a code as a function of input size N is: NlgN+N What is the order of growth of the code?

Nlg^N

Which is the worst case runtime for the quicksort algorithm? A) O(N) B) O(N*log(N)) C) O(N^2) D) O(2^N)

O(N^2)

What does exam2 method calculate? public static int exam2(int[] arr, int index) { ------ if(index == arr.length ) ----------return 0; ------return arr[index]+ exam2(arr, index+1); } Hint:call the exam2 as follows int[] arr = {1,2,3,4,5}; System.out.println(exam2(arr,0)); ____________________________________________________________ A) Recursively calculates the sum of all numbers in the arr B) Prints all of the elements of the array backward. C) Prints all of the elements of the array randomly. D) exam2 is nor well designed.

Recursively calculates the sum of all numbers in the arr

The order of growth of which code is constant? N is input size. A) int COUNT = N; int total += 0; for(int i = 0; i < COUNT * COUNT; i++) ---total += i; B) int count = N; System.out.printf("%d %d %d", count, count*2, count/2); C) int sum = 0; for(int i = 0; i < N/2; i+=2) ---sum++; D) Scanner sc = new Scanner(System.in); string str1 = sc.nextLine(); String str2 = sc.nextLine(); string str3 = concat(str1, str2)

Scanner sc = new Scanner(System.in); string str1 = sc.nextLine(); String str2 = sc.nextLine(); string str3 = concat(str1, str2)

Which one is not true about selection sort? A) The items in the left side of the current index are sorted, in their final places and not examined again. B) The best case of selection sort is O(N) C) The worst case of selection sort is O(N^2) D) Selection sort has less exchanges comparing to insertion sort.

The best case of selection sort is O(N)

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); ---} }

There is no base case for myMethod

An algorithm is developed to find a name starting with a given letter in a list.Which scenario is not correct when you look for a name that starts with an 'X'

There is no need to find all names starting with "X".

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

True

Big oh notation develops the -------- of an algorithm. A) Upper bound B) Lower bound C) average D) optimal

Upper bound

The ________ is at least one case in which a problem can be solved without recursion. ____________________________________________________________

base case

------ scenario of an algorithm happens when the algorithm is working on the easiest input or the minimum number of operations.

best case

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) quadratic or O(N^2)

linearithmic or O(Nlg^N)

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.

minimum, where it belongs in the array

Given the list {13, 16, 15, 14, 10, 19, 12}, what are the new partitioned lists if the pivot is 13? A) {10, 12} and {14, 15, 19, 16} B) P12, 16, 15, 14, 10} and {19} C) {10} {15, 14, 16, 19 12} D) {14, 12, 10} {15, 19, 16}

{10, 12} and {14, 15, 19, 16}


Set pelajaran terkait

MicroEconomics - Module 2 - Homework Quiz

View Set

Jeopardy questions/clicker questions

View Set

How would you describe yourself?

View Set

수교론 4부 수학 문제해결교육론

View Set