




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
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Main points of this exam paper are: Public Int Head, Public Intlist Tail, Following Assertions, Case Time, Constant Time, Random Boolean, Boolean Value, Return Values, Partition Method, Result List
Typology: Exams
1 / 8
This page cannot be seen from the preview
Don't miss anything!
CS 61B, Fall, 2002, Midterm 1, Hilfinger
Throughout this test, assume that the following definition is available.
Class IntList { Public int head; Public IntList tail;
/** The IntList whose head is HEAD and whose tail is TAIL. */ public IntList (int head, IntList tail) { this.head = head; this.tail = tail;
} }
a. 1000 + 1/n
b. 1/n
c. x^2 + 1000x
For (int I = 0; i < N; i += 1) If (A[i] > x) { For (int j = I; j < N; j += 1) { S += A [j]; } break; } }
e. The worst-cast time for executing the call f(x) is
(2^x). Assume that the function flip takes constant time and generates a random Boolean value each time it’s called (as if according to the flip of a coin). It can, in principle, produce any possible sequence of return values.
Void f (int y) { If (y == 0) System.out.println (“Bingo!”); If (flip ()) F(y-1); If (flip()) F(y-1); }
/** The items in L, re-arranged so that all items < pivot come first,
While (_____________________ != null) { IntList q = last.tail; // q is next item to be processed
If (____________________) { // Move item at q from its current location to the front. Last.tail = q.tail; ________________ = first; first = ________________; } else if (____________________) { // Move item at q from its current location to middle last.tail = q.tail; ________________ = mid.tail; mid = mid.tail = _______________________; } else { // Just extend the result list. Last = ______________________; } } }
Interface command { /** Perform the action represented by THIS. / void doIt (); /* Reverse the action represented by THIS, returning thigs to
One of the classes of your editor handles overall command processing like this:
Class Controller { List transcript = new ArrayList (); /** Execute C and add it to the transcript */ void execute (command c) { c.doIt (); transcript.add (c) }
/** Remove the last command from the transcript and undo it. / void undo () { command c = ____________________________________; c.undo (); } …. } Finally, your editor interprets user commands and then carries them out by calling methods of an Editor class that holds and manipulate the actual text. Class Editor { /* The full text being edited. */ private StringBuffer theText = new StringBuffer(); // The StringBuffer class, a kind of modifiable version of String, // is described in 10.4 and Figure 10.4 of Programming in Java
private myController = new Controller (); …. /** Insert INSERTION into theText at position K. * void insert (String insertion, int k) { myController.execute (new Inserter (theText, insertion, k)); } … // and so on for deletion, substitution, etc. }
a. Show what is printed by
System.out.println ((15 << 28 >> 28); System.out.println ((15 << 28 >>> 29);
(Reminder: Java ints are 32 bits long).
b. Somewhere in your program you create an IntList object:
positionList = new IntList (x, otherList);
and you have checked with gjdb that otherList is non-null when you execute this statement. But later, your program blows up with a NullPointerException on
if (positionList.tail.head > positionList.head) {….
Even though positionList is unchaged. Assuming that there are a lot of statements executed between the assignment to positionList and this statement, how could you best use gjdb to find where your program is setting positionList.tail to null?
c. The following function cannot work as promised. Why not?
/** Assuming that the numbers in L are initially
d. A student decides to represent a matrix of doubles with an ArrayList of ArrayLists of Doubles (a Double is a “wrapper object” that contains a double). The idea is that the matrix should be expandable. He writes the following method to add a column to the matrix
/** Expand MATRIX on the right with a column of 0’s */ void addAColumn (List matrix) { for (int row = 0; row < matrix.size (); row += 1) ((List) matrix.get (row)).add (new Double (0.0)); }
Mysteriously, he discovers that after executing
addAColumn (B); His matrix B, which initially had 100 rows and 100 columns, now has 100 rows and 200 columns. What was probably wrong?
e. I use a text-formatting system called LaTex to write tests, readers, and lectures. By hand I can run it like this:
Latex lect20.tex
And it will produce an output file called lect20.dvi. I want to set up a makefile in that directory containing lectures so that gmake will run the latex program on any lectures that need it. Each time I create a new lecture, I just add the name of the new file to a line in the makefile that looks like this:
Latex_SRCS = lect1.text lect2.tex etc.
What else should I put in the makefile to make this all work—that is, so that I only have to change the LATEX_SRCS line when I add a new lecture?