CS61B Midterm Test in Computer Science from University of California, Exams of Data Structures and Algorithms

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

2012/2013

Uploaded on 04/02/2013

shashidhar_p43
shashidhar_p43 šŸ‡®šŸ‡³

4.5

(53)

72 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
UNIVERSITY OF CALIFORNIA
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:
1. /10
2. /10
3. /10
4. /
5. /10
TOT /40
Login of person to your Left: Right:
Discussion section number or time:
Discussion TA:
Lab section number or time:
Lab TA:
1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download CS61B Midterm Test in Computer Science from University of California and more Exams Data Structures and Algorithms in PDF only on Docsity!

UNIVERSITY OF CALIFORNIA

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:

TOT /

Login of person to your Left: Right:

Discussion section number or time:

Discussion TA:

Lab section number or time:

Lab TA:

  1. [10 points]

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 (); } }

  1. [10 points] Provide simple and tight asymptotic bounds for each of the following. Here, ā€œsimpleā€ means roughly ā€œno unnecessary terms or constantsā€ and ā€œtightā€ means ā€œeither the largest Ī©(Ā·) and smallest O(Ā·) bounds you can find or, if possible, a Θ(Ā·) bound.ā€

a. 9x^2 + 3x + 14 log x

b.

āˆ‘N

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

  • that are equal to k modulo N, in their original order. For
  • example, if N is 3 and L contains [9, 2, 7, 12, 8, 1, 6],
  • then the result is [ [9, 12, 6], [7, 1], [2, 8] ]. The operation
  • is nondestructive (the original contents of L are not changed). */ static IntList2 slice (IntList L, int N) { // FILL THIS IN

// ADDITIONAL METHODS MAY GO HERE.

  1. The class FilteredList represents a read-only view of a List that selects only certain of its members. In this problem, you are to fill in part of its implementation. For example, if L is any kind of object that implements List (that is, the standard java.util.List), then writing

List FL = new FilteredList (L, filter);

gives a list containing all items, x, in L for which filter.test (x) is true. Here, filter is of type Predicate:

interface Predicate { boolean test (T x); }

(Don’t worry about that Predicate, even though we haven’t talked about it explicitly. It just means ā€œFor any type, T,... .ā€) The object pointed to by FL above is supposed to be a view of L; when L changes, so does FL (since FL is a read-only view, we aren’t going to worry about the other direction).

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 { // Put private instance variables here.

public FilteredList (List L, Predicate filter) { // FILL IN

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

  • that are equal to k modulo N, in their original order. For
  • example, if N is 3 and L contains [9, 2, 7, 12, 8, 1, 6],
  • then the result is [ [9, 12, 6], [7, 1], [2, 8] ]. The operation
  • is nondestructive (the original contents of L are not changed). */ static List<List> slice (List L, int N) { // FILL THIS IN

// Put any other methods and classes you need here.