



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Section day ____________________________ Section time ___________________________
This 90-minute exam has 6 questions (numbered 0..5) worth a total of 100 points. Spend a few minutes looking at all the questions before beginning. Use the back of the pages if you need more space. Question 0 (2 points). Fill in the information, legibly, at the top of each page. (Hint: do it now.) Question 1 ( 20 points). (a) Evaluate the call Tester.f(1) (see the class at the bottom of the page. Note that we have labeled each statement to help you identify them). As you evaluate the call, draw frames for all function calls that are evaluated. Stop executing just before executing one of the statements labeled m3 and m4, whichever comes first. Do not continue executing. That way, we will be able to see that you drew frames and exe- cuted correctly. Please be sure to maintain the instruction counters of the frames. (b) When is parameter k created? When is local variable p created? When is local variable s created? public class Tester { public static int f( int k) { int p= k+1; f1: if (k == 1) { f2: return f(p); } f3: return m(p); } public static int m( int n) { m1: if (n != 1) { int s; m2: s= n * n; m3: return s; } m4: return n; }
Section day ____________________________ Section time ___________________________ Question 2 (18 points). Below is a class Pres, which contains information about a president of the US. We have omitted many fields, the constructors, and other methods. We put in only the fields that you need to answer this question. Complete the bodies of the three functions shown in class Pres. In function equals, do not simply test whether this and p are equal; implement it as specified. /** An instance maintains info about a president / public class Pres { private int sYear; // Was president in years private int eYear; // sYear..eYear private String name; // name of president private Pres prev; // the previous president (null if none) /* = the previous president (or null if none)/ public Pres getPrevious() { } /* = number of presidents before this one / public int before() { } /* = "p is a Pres and has the same starting and ending year and the same name as this President" */ public boolean equals(Object p) { } }
Section day ____________________________ Section time ___________________________ Question 4. ( 20 points). In answering this question, it will help to draw the objects that are created and the frames for the function calls that are being evaluated. You don’t have to do this, but it should help. Consider the two classes given at the bottom of this page. Suppose the following assignments have been executed (we also give the declarations of the variables being assigned): Two a; a= new Two(2); Two b; b= new Two(3); Two c; c= new One(4); Two d; d= b; d.bV= 5; (a) Evaluate the following expressions and write their values to the right of the expressions.
public class One extends Two { public One( int n) { super (n); } public int mult() { return bV; } public int altMult() { return super .mult(); }
Section day ____________________________ Section time ___________________________ Question 5 ( 20 points). Write the body of recursive function merge, whose specification and header appear below. In writing it, think of the base cases first —parameter values where the answer is easy to calculate because a parameter is as small as possible. Then handle the other, recursive, cases. Do not use loops. Use only recursion. /** = the characters of s1 and s2, in alphabetical order. Precondition: the characters of s1are already in alphabetical order and the characters of s2 are already in alphabetical order. Examples: merge("ab", "") = "ab". merge("abbce", "cdg") = "abbccdeg" */ public static String merge(String s1, String s2) { }