CS 101 Exam 1
February 22, 2017
NAME _________________________________
This exam is worth 105 points. Each question is worth 3 points. 1. Choose the ordering of the functions below so that for large enough inputs, they will be in increasing order of running time: a. O(1), O(lg N), O(N), O(N2), O(N lg N) c. O(1), O(N), O(lg N), O(N lg N), O(N2) b. O(1), O(N), O(N2), O(lg N), O(N lg N) d. O(1), O(lg N), O(N), O(N lg N), O(N2) 2. What is the time complexity of this code fragment? for (K=0; K< n; K=K+2) for (j=K; j>0; j--) sum += j; a) O ( n2 ) b) O ( n )
c) O ( lg n ) d) O ( 1 )
3. What is the time complexity of this code fragment? for (K=1; K
c) O ( lg n ) d) O ( 1 )
4. What is the time complexity of this code fragment? i = 1; while (i < (n*n)) { cout << i; i *= 2; } a) O ( n2 ) b) O ( n )
c) O ( lg n ) d) O ( 1 )
5. A binary search takes how many steps? a) O(1) b) O(log n)
c) O(n) d) O(n log n)
6. When N = 1,000,000, binary search will be roughly ____ times faster than linear search. a. 20 d. 50,000 b. 100 e. 500,000 c. 10,000 7. What is the worst-case time for quicksort to sort an array of n elements? a) O ( n2 ) c) O ( n lg n ) b) O ( n ) d) O ( n3) 8. What is the worst-case time for mergesort to sort an array of n elements? c) O ( n lg n ) a) O ( n2 ) b) O ( n ) d) O ( n3)
CS 101 Exam 1
February 22, 2017
Page 2 of 4
9. Which sorting algorithm will perform best if the array is nearly sorted (only a few items out of position)? a) Mergesort c) Insertionsort b) Selectionsort 10. Which sorting algorithm’s runtime doesn’t depend much on the ordering of the input? a) Bubblesort c) Insertionsort b) Selectionsort 11. We want to search the following sorted array using a binary search. Which numbers will the binary search check while looking for 13? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 a) 1, 6, 12, 13 c) 7, 11, 13 b) 8, 12, 14, 13 d) 1, 2, 3, 4, … 11, 12, 13
12. Where in memory would the parameters to main(i.e. argv and argc), be stored? a. The heap c. The runtime stack b. Static ram d. Program storage For the next few questions, suppose that we have defined a class myClass: 13. Where in memory would the pointer p below be stored? myClass *p = new myClass; a. The heap c. The runtime stack b. Static ram d. Program storage 14. Where in memory would the myClass object created below be stored? myClass *p = new myClass; a. The heap c. The runtime stack b. Static ram d. Program storage 15. Suppose myClass has a large number of data elements, and you are passing a myClass object to a function foo. If nothing in foo changes the myClass data elements, which of these is best way to pass the object to foo: a. void foo( …. , myClass x, …) b. void foo( …. , myClass & x, …) 16. As above, except that foo updates the myClass data elements, which of these is best way to pass the object to foo: a. void foo( …. , myClass x, …) b. void foo( …. , myClass & x, …) 17. As above, except that foo destroys the myClass data elements, and after foo is called you want the non-destroyed values in the myClass object , which of these is best way to pass the object to foo: a. void foo( …. , myClass x, …) b. void foo( …. , myClass & x, …)
CS 101 Exam 1
Page 3 of 4
February 22, 2017
18. If myClass has _____________ contained in the class, then a copy constructor could be necessary: a. string objects d. primitive data types b. C-strings e. b and c c. dynamically allocated storage 19. What is the amortized cost of inserting an item into a dynamic array of size N? a. O ( n2 ) b. O ( n )
c. O ( lg n ) d. O ( 1 )
20. What is the worst case cost of inserting an item into a dynamic array of size N? a. O ( n2 ) b. O ( n )
c. O ( lg n ) d. O ( 1 )
True or False: 21. The C++ string is a primitive data type? a. True
b. False
22. The C++ bool type is a primitive data type? a. True
b. False
23. Class constructors return a void type? a. True
b. False
24. If myClass needs a copy constructor then should have a destructor? a. True b. False 25. Mergesort is an in-place method. a. True
b. False
26. The string constant “myString” can be passed to a function as a reference parameter, as in the call foo(x, “myString”, outputStream); a. True b. False 27. string f = “test”; f.length() is valid and will be 4? a. True
b. False
28. char *s = “test”; s.length() is valid and will be 4? a. True
b. False
29. What is the value of the expression (5 + 3/2) + (1/2 – 1) == 5 a. True b. False
CS 101 Exam 1
Page 4 of 4
February 22, 2017
For the remaining questions, consider the program below. #include using namespace std; class LL{ private: struct Node { char x; Node *next; } *head; public: LL(){head = NULL;} void addValue(char c){ Node *n = new Node(); n->x = c; n->next = head; head = n; } char delValue(){ Node *n = head; char ret = n->x; head = head->next; delete n; return ret; } };
30. 31. 32. 33.
int main() { LL a,b,*p,*q; p = &a; q = &b; a.addValue(‘a’); a.addValue(‘b’); a.addValue(‘c’); cout << a.delValue() << endl; b.addValue(‘d’); b.addValue(‘e’); (*q).addValue(‘a’); cout << b.delValue() << endl; q = p; cout << q->delValue() << endl; a = b; cout << a.delValue() << endl; return 0; }
What is the first line of output? (bubble the letter that is output) What is the second line of output? (bubble the letter that is output) What is the third line of output? (bubble the letter that is output) What is the fourth line of output? (bubble the letter that is output)
34. The class LL needs a destructor. a. True
b. False
35. Which of the following lines would cause the program to crash if they are inserted after the last output: a. a.delValue(); b. b.delValue(); c. (*q).delValue(); d. (*p).delValue();