






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The midterm test for the cs61b course offered by the university of california's department of electrical engineering and computer sciences. The test covers various topics in computer science such as bit operators, program compilation, function results, and asymptotic bounds. It is an open-book test worth 40 points and lasts for two hours.
Typology: Exams
1 / 10
This page cannot be seen from the preview
Don't miss anything!







Department of Electrical Engineering and Computer Sciences Computer Science Division
CS61B P. N. Hilfinger Fall 2007
Midterm Test
READ THIS PAGE FIRST. Please do not discuss this exam with people who havenāt taken it. Your exam should contain 5 problems on 10 pages. Officially, it is worth 40 points (out of a total of 200). This is an open-book test. You have two hours to complete it. You may consult any books, notes, or other inanimate objects available to you. You may use any program text supplied in lectures, problem sets, or solutions. Please write your answers in the spaces provided in the test. Make sure to put your name, login, and lab section in the space provided below. Put your login and initials clearly on each page of this test and on any additional sheets of paper you use for your answers. Be warned: my tests are known to cause panic. Fortunately, this reputation is entirely unjus- tified. Just read all the questions carefully to begin with, and first try to answer those parts about which you feel most confident. Do not be alarmed if some of the answers are obvious. Should you feel an attack of anxiety coming on, feel free to jump up and run around the outside of the building once or twice.
Your name: Login:
Login of person to your Left: Right:
Discussion section number or time:
Discussion TA:
Lab section number or time:
Lab TA:
a. How can you check to see if a number is less than 0 using only == and the bit operators (&, |, ^, ~, <<, >>,>>>)?
b. The following program compiles correctly. What does the main program (in D) print?
class A { int z = 2; void f () { this.g (); } void g () { System.out.printf ("A:%d%n", z); } int h () { return z; } }
class B extends A { int z = 15; void g () { System.out.printf ("h:%d z:%d%n", h(), z); } }
class C extends A { int z = 42; void f () { this.g (); } }
class D { public static void main (String[] args) { A c1 = new C(); C c2 = new C(); A b1 = new B(); B b2 = new B(); c1.f (); c2.f (); b1.f (); b2.f (); } }
a. 9x^2 + 3x + 14 log x
b.
i=
ā^ i j=
j
c. The running time, as a function of N , for foo(N ), for foo declared. (Iām asking for running time here, not worst-case time. So weāre asking for upper and lower bounds on how long the program will run with input N.)
void foo(int N){ int x; for(x = 0; x < N; x += 1) { int y; for (y = 0; y < x; y += 1) { bar(x,y); // bar runs in constant time } } }
More parts on the next page.
d. The running time, as some function of low and high, for search, declared below. That is, define a suitable function s(low, high), and then give a bound for Csearch(N ), in terms of N , where N = s(low, high). (E.g., āCost of search(N ) = O(N 2 ), where N = lg(high + low)ā). (Again, Iām asking for upper and lower bounds on the time, not just a bound on the worst- case time).
bool search (int A[], int value, int low, int high) { if(high < low) return false; int mid = (low + high) / 2; if (A[mid] > value) return search(A, value, low, mid - 1); else if (A[mid] < value) return search(A, value, mid + 1, high); return true; }
e. The running time (not just worst-case), as a function of n, for G(n), for G declared
void G(int n) { if (n == 1) return; for(int i = 0; i < n; i += 1) G(n-1); }
/* b. / /* A list of N lists such that list #k contains all the items in L
List
gives a list containing all items, x, in L for which filter.test (x) is true. Here, filter is of type Predicate:
interface Predicate
(Donāt worry about that Predicate
a. Fill in the indicated places below to achieve this effect. Do this āfrom scratch.ā That is, do not use the standard AbstractList or AbstractSequentialList classes.
public class FilteredList
public FilteredList (List
public int size () { // FILL IN
public T get (int k) { // FILL IN
continues on next page
b. Here is a second formulation of the slice problem from above. Fill it in, using the FilteredList abstraction from part (a) above. We suggest that the returned value be an ArrayList<List
/** A list of N lists such that list #k contains all the items in L
// Put any other methods and classes you need here.