CS210 Midterm Exam I

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

a. No names in the list begin with "L." *The worse case scenario would be "No names in the list begin with "L."", where all employee names in the list would be scanned plus possible operations after the whole list is traversed.

An algorithm is written to return the first name beginning with "L" in a list of employee names. Which of the following is the algorithm's worst case scenario? a. No names in the list begin with "L." b. The last name in the list begins with "L." c. The first name in the list begins with "L." d. The first half of the list has names beginning with alphabets before "L," and the second half of the list has names beginning with alphabets after "L."

c. 4 *Refer to the zyBook binary search algorithm to walk through a list of ordered alphabet letters (26 of them), it would need to check 4 letters ((int)log226) before the search reaches the letter "K".

Assume you have an ordered list containing the English alphabet (case insensitive, in other words, "a" is the same as "A" ). Using binary search, how many letters are checked to locate the letter "K"? a. 5 b. 26 c. 4 d. 6

b. Their implementations of data operations in accessing (read and write) elements in the data structure are different. *True. For reading, array allows direct / random read access, while linked list only allows sequential read access. For writing, inserting / removing operations on an array usually involve shifting of elements in the array with linear O(n) complexity; while inserting / removing operations on a linked list would only involve adjacent elements and would involve constant number of operations (O(1)) with much smaller complexity. c. They represent different models of computation. d. They both can be used to implement a same abstract data type. * True, an example would be both Array and Linked List can be used for implementing the List ADT.

Comparing the linear data structures, array and linked list, ______________? (check all apply) Note for each incorrect answer that you select, you would lose the same points allocated for a correct answer. The score cannot go negative. a. Array based data structures in general provide more efficient update operations of inserting or removing an element. b. Their implementations of data operations in accessing (read and write) elements in the data structure are different. c. They represent different models of computation. d. They both can be used to implement a same abstract data type. e. Linked list based data structures in general provide more efficient random / direct access of an element in the data structure.

c. The Insertion algorithm may have the smallest time complexity. d. Selection and Insertion algorithms always have smaller auxiliary space complexity than the Mergesort or Quicksort algorithms. e. Mergesort and Selection algorithms do not have different time complexities between the best and worst cases

Given the same input with an unsorted array or list, comparing the sorting algorithms among Selection, Insertion, Mergesort, and Quicksort, which of the following statement/s is/are true? (check all apply) Note for each incorrect answer that you select, you would lose the same points allocated for a correct answer. The score cannot go negative. a. Selection algorithm would always have a bigger time complexity than the other algorithms. b. Quicksort algorithm would always perform better in time complexity than Insertion. c. The Insertion algorithm may have the smallest time complexity. d. Selection and Insertion algorithms always have smaller auxiliary space complexity than the Mergesort or Quicksort algorithms. e. Mergesort and Selection algorithms do not have different time complexities between the best and worst cases

c. i = 0 while (i < listSize) { sum = sum + i i++ }

Identify the non-constant time operation, i.e., the time complexity of executing the operations would be related to input size. a. if (x > y) return x b. if (listSize > 100000)return; for (i = 0; i < 100000; i++) sum += num2; c. i = 0 while (i < listSize) { sum = sum + i i++ } d. num = arr[i] arr[i + 1] = num + 1

leftmost unsorted

In selection sort, the smallest element is selected and swapped with the _____ element. leftmost unsorted middle rightmost unsorted rightmost sorted leftmost sorted next unsorted

d. Error *iter <= sensorReadings.size(); would fail, iter is an iterator object, cannot be compared with an integer from sensorReadings.size()

What is output? a. 10 7.5 7.45 b. -2.5 7.5 7.45 c. 10 -2.5 -0.05 d. Error

c. Mixer Grinder Name: MyMixer Mixer Grinder Price: $100 *In the main function, mxCompany is declared as an object of the base class HomeAppliances, but it is assigned with an object of the MixerGrinder. mxCompany -> SetDetail(...) will call the HomeAppliances implementation of SetDetail, which would set the appName and appUse. mxCompany -> SetPrice(...) will call the HomeAppliances implementation of SetPrice, because the SetPrice is NOT a virtual function, it is called based on the declared type of mxCompany which is HomeAppliances. mxCompany -> PrintDetails(...) will call the MixerGrinder implementation of PrintDetails, because the PrintDetails is a virtual function, which is overriden by MixerGrinder class, this function is called based on the actual type of mxCompany which is MixerGrinder. Runtime polymorphism is applied here.

What is output? a. Home Appliance Name: MyMixer Home Appliance Price: $100 b. Home Appliance Name: MyMixer Home Appliance Price: $200 c. Mixer Grinder Name: MyMixer Mixer Grinder Price: $100 d. Home Appliance Name: MyMixer Home Appliance Price: $100 Mixer Grinder Name: MyMixer Mixer Grinder Price: $100 e. Mixer Grinder Name: MyMixer Mixer Grinder Price: $200

d. O(N2) *This algorithm involves too loops, one nested in the other, the complexity = N + N - 1 + N -2 + N - 3 + .... --> O(N2)

What is the Big O notation for the following algorithm? a. O(N) b. O(logN) c. O(1) d. O(N2)

d. f(N) = 5N + 2 *i = 0 gets executed once i < N gets executed N + 1 times ++i gets executed N times numbers[i] % 2 gets executed N times (numbers[i] % 2) == 1 gets executed N times in the worst case, factor = 2.5 gets executed N times --------------------------------------------------------------------------- Total number of operations = 1 + N + 1 + N + N + N + N = 5N + 2

Which function of the input size N best represents the number of operations in the worst-case? a. f(N) = 2N b. f(N) = 5N2 + 4 c. f(N) = 4N + 2 d. f(N) = 5N + 2

a. Inserting at the beginning, inserting in the middle, and inserting at the end would involve the same upper bound Big O time complexity. *Inserting at the beginning, inserting in the middle, and inserting at the end would involve the same upper bound Big O time complexity.

Which of the following statements is true about the writing operations on an array? a. Inserting at the beginning, inserting in the middle, and inserting at the end would involve the same upper bound Big O time complexity. b. Inserting at the beginning incurs more upper bound Big O time complexity time than deleting in the middle. c. Inserting in the middle or inserting at the end would have less upper bound Big O time complexity than inserting at the beginning. d. Inserting at the end would incurs constant time O(1) complexity.

d. The code will not compile. *CSMajor major; generates an error as CSMajor class is an abstract class because it inherits the virtual void PrintInfo() = 0; from the ComputingMajors which inherits the pure virtual function from the base class Majors, since CSMajor class does not implement this pure virtual function, so it is an abstract class and can NOT instantiated.

Which statement is true based on the following code? a. The code will compile and the execution of the main function will print "Majors" b. The code will compile and the execution of the main function will print "Computing Majors" c. The code will compile and the execution of the main function will print "CS Major" d. The code will not compile.

a. It can not be determined *This cannot be determined because in the worst case, quicksort could have N partitioning levels; while in the best case, it would involve Log2N partitioning levels.

With quicksort algorithm, using the mid point as pivot value for partitioning, how many more recursive partitioning levels are needed for sorting a list of 32 numbers than sorting a list of 8 numbers? a. It can not be determined b. 24 c. 3 d. 2


Kaugnay na mga set ng pag-aaral

CHAPTER 1: Political Thinking & Political Culture: Becoming a Responsible Citizen

View Set