Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CS 240 Exam 2 Computer Science II Spring 2003 2/28/03 Page 1 Name _____________________________ 1. Mark an ‘X’ in each box that matches the trait to the data structure [10 pts] Data Structure Array Record Stack Set Queue Homogeneous elements Linear organization Sequential access 2. Rank the algorithm complexities from "fastest" to "slowest" (increasing dominance). 1 is fastest (good, low cost) and 6 is slowest (bad, high cost). [5 pts] linear exponential quadratic O(log n) constant O(n log n) 3. Estimate the running times using the big-O notation for each of these code segments. The size of the data set is n. [8 pts] ____________ ____________ ____________ ____________ for (i=1; i<=n; i++) for(j=i; j<=n; j++) for(k=9; k<=n; k++){ // 4 assignment and 2 i/o statements } i = n; while(i>0){ for(j=i; j<=n; j++){ // 3 assignments } if(i % 2 == 1){ // if i is odd // 2 assignment and 4 i/o statements } i = i-3; } j=n; while(j>0){ // 3 assignments j = j/2; } for (k=n; k>0; k--) j=0; while (j<n){ foo(j,n); //foo executes in quadratic time j++; } } CS 240 Exam 2 Page 2 4. Convert the following infix expression to postfix. [4 pts] a + b * (c – d) + e __________________________ 5. Evaluate the following postfix expression and prefix expression. [4 pts] 11 4 + 9 4 2 * + - = __________ + * - 4 1 5 / 12 4 = __________ 6. Memory management. [6 pts] a. In the memory layout scheme, where are the activation records stored? ______________________ b. The register that tracks which bytecode is to be executed next is called the ___________ ___________ c. Where are local variables (int, float, char) stored? ________________________________ d. Where is the return address stored during a method call? __________________________________ e. The area or data structure where objects are stored is called a ________________ f. The process of recovering memory from unreferenced objects is called _______________ ___________ 7. Give brief pre- and post-conditions as requested for each of the following queue operations. [10 pts] int size(); //post: void enqueue(Object element); //pre: //post: boolean isEmpty(); //post: Object dequeue(); //post: 7. Below is an int array. The top row values are the indexes of the elements. [6 pts] 0 2 1 5 2 8 3 10 4 11 5 16 6 19 7 20 8 22 9 23 10 25 11 28 12 30 13 33 a) What is the basic requirement of the data in order for a binary search to be applicable? b) List the sequence of probe indexes that would be computed in a binary search when looking for 16. c) What happens to the internal boundary indexes when a value is not found in a binary search? 14 34 CS 240 Exam 2 Page 3 8. Fill in the blanks below to complete a binary search function on a “full” integer array. [9 pts] public static int Lookup(int vec[],int key) { int lo = 0 ; int hi = _____________ ; while (lo ____ hi){ mid = (lo+hi)________; if(vec [_____] == _____) return _____ ; if (key __ vec [_____]){ lo = mid ________ ; } else { hi = mid ________ ; } } ___________ -1; } 9. Fill in the program segment to take a list of positive integers and print them out in reverse order using a stack. Assume the usual stack operations. [7 pts] ArrayStack stack = new ______________ ; int number = getNumber(); while(str.length() > 0){ stack.____________(( _________ )number); number = getNumber(); } while( ! stack.____________){ number = ( _____ ) stack.____________(); System.out.println(____________); } 10. Below are the methods for push and pop in an array implementation. Fill in the code to complete the implementation. [9 pts] public Object pop(){ if ( _______________ ) throw new StackUnderflowException ("Stack is empty—cannot pop"); __________ topItem = stack[top]; stack[_____] = _______; top_____; return ___________; } public void push(Object item){ if (item == null) throw new IllegalArgumentException ("Item is null—cannot push"); if (____________ ) throw new StackOverflowException ("Stack is full—cannot push"); top ____; stack[_______] = _________; } CS 240 Exam 2 Page 4 11. For each of the cost functions below give the “best” big-O equivalence. [8 pts] _______ f(n) = 3n2 + 900n + 50 _______ f(n) = 17 + log2n + 5n _______ f(n) = 10n5 +2n _______ f(n) = 2n2 + 5k7 12. For each of the data structure operations below, give the algorithm complexity running time cost. Choose from “constant”, “linear”, “exponential”, and “logarithmic” [7 pts] a) ________________ searching an unsorted list for the maximum value b) ________________ searching a list with a binary search c) ________________ adding an element to the end of an array (that still has room) d) ________________ deep copying an array over another array e) ________________ inserting an element to the middle of an array f) ________________ pushing an element onto a stack g) ________________ dequeueing from a circular queue 13. True/False [7 pts] ______ In the Model-View-Controller design of a GUI application, the controller manages the data. ______ In the MVC design, the view contains the placement of widgets and their interaction. ______ The record data structure is captured in Java’s class facility. ______ An abstract class must be extended to a concrete class for use. ______ An interface provides a set of abstract methods that can be implemented in one or more classes. ______ In event driven programming, a Java event handler is designated for one specific widget. ______ An action listener must be registered or connected with a widget before its events can be recognized. 14. [Extra credit] Consider the purpose of defining the Listable interface. Recall this interface was used in the RealEstate project and is actually a simplified version of the Java standard Comparable interface. Both interfaces define an abstract method compareTo. Why abstract out this particular method? [5 extra credit pts]