VIEWS: 20 PAGES: 12 CATEGORY: Education POSTED ON: 12/31/2009
Mock Test Foundations of Computer Science Martín Escardó and Horia Corcalciuc Monday 7th December 2009 Student ID: . Read carefully before you start: 1. Calculators, notes and handouts are not allowed. 2. Discussions with colleagues are not allowed. 3. There are 10 questions worth 10% each. 4. At the end of the paper there are blank pages for scratch work. 5. The marks are for your and my information only, and do not contribute to- wards the nal mark of the module. Start at 12.00 Stop no later than 12.50 sharp 1 1. n 2n 0 1 1 2 2 4 3 8 4 16 5 32 6 64 7 128 8 256 Using the above table if necessary, calculate the upper and lower logarithms of the following numbers: (a) 7 (b) 50 (c) 100 (d) 1 (e) 1024 * 1024 * 1024 Answer(10%): 2 2. Consider the following algorithm: 1 int logarithmCeiling ( int x) { 2 int n = 1; 3 int y = 0; 4 while (n < x) { 5 n = n ∗ 2; 6 y = y + 1; 7 println (n + " " + y); 8 } 9 return y; 10 } (a) What does the method call logarithmCeiling(17) print? (b) What does it return? (c) Find a loop invariant involving n and y. It should be such that if the loop invariant is true and the condition of the while loop is false (that means n ≥ x) we can conclude that the returned y is equal to log(x) . Answer(10%): 3 3. Convert 14 and -5 into two's complement notation using 6 bits, and then perform binary addition. Show work. Answer(10%): 4 4. Recall the binary search algorithm that nds the smallest position a given element x is/should be in a given sorted array a: 1 int position ( int x, int [] a) { 2 // Pre− c o n d i t i o n : the array a is in ascending order . 3 // We calculate and return the number s such that : 4 // 5 // a[ i ] < x for all indices i < s, and 6 // a [ i ] >= x for all indices i >= s . 7 int l o w e r = 0 ; 8 int u p p e r = a . l e n g t h ; 9 while ( l o w e r != u p p e r ) // Invariant : 0 <= l o w e r <= s <= u p p e r <= a . l e n g t h 10 { 11 int mid = ( l o w e r+u p p e r ) / 2 ; 12 a s s e r t ( l o w e r <= mid && mid < upper ) ; 13 if ( x<=a [ mid ] ) // s <= mid 14 upper = mid ; 15 else // mid < s 16 lower = mid + 1 ; 17 } 18 // Because l o w e r == u p p e r is the negation of the w h i l e −l o o p condition , 19 // we conclude that l o w e r == s == u p p e r , by the invariant . 20 return ( l o w e r ) ; 21 } Modify the algorithm so that it nds the largest position at which x occurs in the array. Answer(10%): 5 5. As you may have observed, sometimes when launching Groovy or Netbeans, the machine you are working becomes unresponsive and if you are writing a document the letters don't appear immediately on the screen. However, when the machine recovers, all letters you typed are printed out. This means that the machine uses a buer to store the keypresses. (a) If you were to choose between a queue or a stack, which one of them would you say the machine uses to store your keypresses, and why? Answer(10%) : (b) What would happen if it were to use the other data structure you didn't choose in the previous question? Answer(10%): 6 6. Recall Eratosthenes' sieve algorithm: int n = 100000; boolean [] crossedOut = new boolean [n]; for int ( i = 2; i < n; i ++) if ( ! crossedOut [ i ] ) for ( j= i +1; j < n; j= j + i) crossedOut [ j ] = true ; (a) Modify the algorithm so that it additionally calculates how many prime numbers from 2 to n have been found. Answer(10%) : (b) Modify the original algorithm so that it additionally stores the prime numbers found in a collection. Answer(10%): 7 7. In the handout and exercise classes we have discussed an algorithm for visiting every node of a network: 1 NodeStack s = new NodeStack [ n ] ; 2 3 s . push ( s t a r t i n g node ) ; 4 w h i l e ( ! s . empty ( ) ) { 5 6 Node n = s . pop ( ) ; 7 8 for each node m l i n k e d from n 9 i f (m hasn ' t been visited yet ) { 10 visit m; 11 s . p u s h (m) ; 12 } 13 } How would you implement the check if(m hasn't been visited yet) using a collection? Explain your answer in detail. Answer(10%): 8 8. We have discussed how to check for mismatched brackets in a given character array using a stack. Now suppose you have only one type of bracket to consider, say ( and ). I claim that a stack is not needed any longer. It suces to use an integer that keeps track of how many brackets have been opened so far. Write a matching algorithm based on this idea. Answer(10%): 9 9. Convert the following expressions into reverse Polish notation: (a) (11 + 2) * 3 * 2 (b) 3 * (5+5) (c) 1 * (2 + 3 * (5 + 8)) * 3 (d) 11^2 + 5 Answer(10%): 10 10. Show how to express AND, OR, NOT and XOR using NAND gates only. You can show either equations or diagrams, as you prefer. Include explanations. Answer(10%): 11 Use this page for scratch work, if necessary, not for answers. End of paper 12