VIEWS: 5 PAGES: 7 POSTED ON: 10/2/2012 Public Domain
CSE 331 – Sample Midterm Questions 1. In a class, the ______________________ section identifies data and operations that can be used only by methods in the class. 2. The public member functions of a class provide the _______________ of the class for a programmer. (a) abstract operations (b) interface (c) encapsulation (d) utilities 3. The #include directive should enclose the file name in ____________ when accessing a programmer defined header file. The directive should enclose the file name in ____________ when accessing a system provided header file. 4.(a) Describe an advantage of using inline code to implement a member function of a class. (b) Given that inline code has some advantages, would you recommend that a programmer should use only this form of implementation? Explain briefly. 5. Use the following declarations for each of the parts from (a) to (h). string s = "abc12xya52cba", t; int index; (a) What is the value of index = s.find_last_of ('c')? // index = ___________ (b) What is the value of index = s.find_first_of('a',3)? // index = ___________ (c) What is the value of index = s.find_first_of('y',8)? // index = ___________ (d) What is the value of s[6]? // s[6] = ___________ 6. The mnemonics OOA, OOD, and OOP describe three phases in the program development life cycle. For each activity, indicate the phase. (a) Set up the data structures and their ADT. (b) Check whether the proposed software system would be cost effective. (c) Perform whitebox testing. (d) Setup the functional specifications for the problem. 7. Verifying that we are solving the correct problem is called _______________; testing that we are solving the problem correctly is called _________________ 8. __________ testing focuses on the input and output behavior of a program without concern for the internal structure of its classes and functions 9. Show the order of elements for intArr after the first two passes of the selection sort. int intArr[] = {5, 10, 7, 2, 24, 19}; Pass 0: _______ _______ _______ _______ _______ _______ Pass 1: _______ _______ _______ _______ _______ _______ 10. Trace the recursive function and give the output for the specified function calls void f(char ch) { if (ch >= 'a' && ch <= 'h') { cout << ch; f(ch+1); } else cout << endl; } (a) f('d') Output: _________________________________ (b) f('a') Output: _________________________________ 11. Assume that the output statement and the recursive call in Question 13 are reversed. f(ch+1); cout << ch; What is the output for the function call f('b')? _______________________ 12. Indicate the running time of each algorithm or code segment. (a) Executing the selection sort. 2 (i) O(n ) (ii) O(n) (iii) O(n log2 n) (iv) O(2n) (b) Finding the minimum of an n element array (unordered). 2 3 (i) O(n ) (ii) O(n) (iii) O(n log2 n) (iv) O(n ) (c) Finding the maximum of an n element ordered array. 2 (i) O(n ) (ii) O(n) (iii) O(1) (iv) O(log2 n) (f) for(i=1;i < n;i++) for(j=1;j < n;j++) sum += i+j; 2 3 (i) O(n ) (ii) O(n) (iii) O(1) (iv) O(n ) 13. From the following list of containers, chose the one that is most appropriate for the problem. (A) array (B) vector (C) list (D) stack (E) queue (F) priority queue (G) set (H) map (a) Teller line in a bank. (b) List of return addresses for a chain of recursive function calls. (c) Collection of insurance companies that provide good-driver discounts. (d) Sequence that efficiently maintains an ascending order when adding a new element. 14. The class storeMax is a container that holds a single integer value maxVal. The constructor initializes maxVal with default value 0. The member function update() takes an integer argument, which becomes the new stored value only when the argument is greater than maxVal. The current stored value is accessible through the function getValue(). The following is an declaration of storeMax. Rewrite it as a template class. class storeMax { public: storeMax(int value = 0); void update(int value); int getValue() const; private: int maxVal; } 15. Assume v is a vector of integer elements. For part (a) and (b), list the elements that result from executing the for-loop. (a) for (i = 5; i >= 0; i--) v.push_back(i+1); v 16. Using the following declarations for each part, identify the output. int x = 20, y = 5, *px = &x, *py; int a[] = {2, 4, 8, 1, 5, 3}, *pa = a; (a) cout << (*px)+3; // output = _______________ (b) py = px; *py += *px * y; cout << x: // output = _______________ 17. Apply the function f() for parts (a) and (b). template <typename T> void f(T *arr, int n) { T *p = arr, *q = arr+n/2, temp; for(int i = 0; i < n/2; i++) { temp = *p; *p++ = *q; *q++ = temp; } } (a) What are the contents of arr after calling f()? int arr[] = {3, 5, 7, -9, 6}; f(arr, 5); Contents 18. The word deque is a abbreviation for ___________________________________________________ 19. What is the output from the following sequence of instructions? int arr[] = {5, 9, 2, 7}; int arrSize = sizeof(arr)/sizeof(int); list<int> alist(arr, arr+arrSize); alist.pop_front(); cout << alist.back(); // Output: __________ cout << alist.front(); // Output: __________ alist.pop_back(); cout << alist.back(); // Output: __________ alist.pop_front(); alist.push_front(7); alist.back() = alist.front(); writeList(alist); Output: ______ ______ ______ ______ 20. After executing the following statements, what is the resulting sequential list? list<char> alist; list<char>::iterator iter; alist.push_front('t'); alist.push_front('a'); iter = alist.end(); alist.insert(iter,'m'); alist.push_back('x'); (a) a m t x (b) t a m x (c) a m t x (d) a t m x 21. What is the output from the following sequence of stack operations? stack<int> stk; int x = 3, y = 19; stk.push(6); stk.push(x); stk.push(y); cout << stk.top(); // Output 1 ___________________ stk.pop(); stk.top() *= 4; stk.push(35); while (!stk.empty()) { y = stk.top(); cout << y << " "; // Output 2 ___________________ stk.pop(); } 22. What is the infix expression for the postfix expression a bb*+cd+/e+ (a) (a + b2)/(c+d) + e (b) (a + b)*b/(c+d) + e (c) a/b2 *(c+d) + e (d) (a + b2)/c + d + e 23. In each part, give the postfix (RPN) expression for the corresponding infix expression (a) Infix: a * b + (c - d) Postfix: _____________________________ (a) Infix: (a + b * c) /(d - e) Postfix: _____________________________ 24. The question investigates the evaluation of a postfix (RPN) expression. After executing an operator, draw the operand stack. RPN expession: 1 6 4 - + 9 2 / * (a) After executing - (b) After executing + Stack: Stack: (c) After executing / (d) After executing * Stack: Stack: 25. Indicate whether the ordering specifies a stack (S) or a queue (Q) . (a) Last-In-First-Out (LIFO) _____________ (b) Last-In-Last-Out (LILO) _______________ (c) First-In-First-Out (FIFO) _____________ (d) First-In-Last-Out (FILO) _______________ 26. The integer array intArr initially contains the following sequence of elements. In parts (a) and (b), you use the radix sort to distribute elements into a bin of ten queues and then collect the elements out of the bins. intArr: {767, 41, 5, 212, 87, 543, 717, 18}. (a) Show the 10 queues based on the units position and give the ordering of intArr after distributing elements based on the units position and collecting them back into the array. 0 1 2 3 4 5 6 7 8 9 intArr: ______________________________________________________________________ 27. Assume an array implementation of a queue with a maximum size of 5. Initially, the queue has the characters 'A', 'B', 'C', and 'D'. Draw pictures of the initial array contents and then the contents after each queue operation. Identify the front and rear positions in each picture. Initial View q.push('E') q.pop() q.pop() q.push('F') 28. The pointer variable p identifies a node in a singly linked list, and L points to the first node in that list. node<T> *p, *L; Write a code segment that sets the value of p so it poits to the last node in the list. 29. (a)What is the runtime efficiency for inserting a node in a singly linked list? ____________________ (b)What is the runtime efficiency for deleting a node in a singly linked list? ____________________ (c)What is the runtime efficiency for locating the last node in a singly linked list? _______________ 30. The function g() takes a pointer to the front of a singly linked list and a list position pos, 0 pos < n, where n is the number of elements in the linked list. template <typename T> void g(node<T> * & front, int pos, const T& item) { node<T> *p = front, *newNode = new node<T>(item); int i; if (pos == 0) front = new node<T>(item, front); else { for (i = 1; i < pos; i++) p = p->next; newNode->next = p->next; p->next = newNode; } } (a) Assume the list contains the elements 6 -> 12 -> 7 -> 3 front Display the elements in the list after calling g(front, 2, 10); (c) In general, what is the action of the function? 1. with respect to quicksort (c) What is the complexity of the quicksort for the average case? O(_____). (i) O(n log2n) (ii) O(n2) (iii) O(n) (iv) O(1) (d) What is the worst case complexity of the quicksort? O(_____). (i) O(n log2n) (ii) O(n2) (iii) O(n) (iv) O(1) 31. The function f(n) is defined as follows. 0, n0 1, n 1 f(n) f(n 1) 2 * f(n 2), n2 Implement the recursive function f(n). int f(int n) { ... }