CS61B CS61B,SUMMER 2003 Exam 1, Julia Kempe Directions: You have 80 minutes to complete this exam. This exam is open book,open notes in paper form only.
1.a (1 point) Describe two differences between a vector and an array. b. (2 points) Consider the following lines of code: Integer i,j,k; i= new Interger(5); j=i; k= new Integer(5); What is the value of the following(true,false or syntax error): i==j ____________________
i==k ______________________ i.equals(k) ________________ 5==j.intValue() ______________
c)1 point What does a call to print() print? static void print(){ int i=1; while(i<=5 || test(i-5)){ System.out.println("Homer!"); i++; }
1
CS61B
} static boolean test(int i){ if (i<4){ System.out.println("Homer!"); System.out.println("Homer!"); return true; }else{ return false; } }
d)(2 points) Consider the following code and fill in the blanks: outer: for(i=1;i<10;i++){ inner:{ if(z==0){ break; } else if (z==1){ continue; } else{ break inner; } statementi(); continue outer; //jumps to ______________________ //jumps to __________________ //jumps to _________________ //jumps to _____________________
2
CS61B
// location 1 } statement 2(); //location 3 } statement3(); // location 4
e)(2 points) Complete a java method below that performs an array reference that is possibly out of bounds, and if it is out of bounds , the program catches that exception and prints "dont try buffer overflow attacks in java!". Regardless of whether or not there is an exception or not the method should then print "bye bye". Use the RuntimeException ArrayIndexOutOfBoundsException and the array and ReferenceIndex passed to the method. public void BufferAttack(Object A[], int ReferenceIndex)__________________________{ //FILL IN!!
} f)(1 point) Describe the difference between an interface and an abstract class.
Also answer these questions: Can you create an object whose type is an abstract class ? (Y/N)______ an interface?(Y/N)______
3
CS61B
Can you declare a reference variable whose type is an abstract class ? (Y/N)______ an interface?(Y/N)______
2.(5 points Soda) machine The class SodaMachine below represents a soda machine with a fixed capacity. Sodas are stored in an array. The method buySoda removes and returns the soda at the current position and advances the remaining sodas. The method stealSoda removes and returns the soda at the current position but does not advance the remaining sodas(a call to buySoda after a call to stealSoda will return null and advance, since the soda has been stolen) The method restock places new instances of the class Soda in all empty positions in the array, resets position , and returns the number of sodas added during restocking. Fill in all the method declarations for the class SodaMachine such that they will obey their comments. class Soda{ /*contains no declared fields or methods*/} class SodaMachine{ Soda[] sodas;/*contains the sodas held by this SodaMachine*/ int position; //The current position //constructs an empty SodaMachine with the given capacity public SodaMachine(int capacity){ sodas=new Soda[capacity]; position=0; } public Soda buySoda(){ //fill in }
public Soda stealSoda(){ //fill in }
4
CS61B
publik=c int restock(){ //fill in } }
3. (4 points) Unknown methods Below are definitions for the methods hip and hop static void hip(int[] x,int i,int p){ int z =x[i]; x[i]=x[p]; x[p]=z; } static void hop(int[] x,int s){ for(int i=s;i=x[p]) p=j; hip(x,i,p); } }
5
CS61B
a.What does the method hip do? How does it work? What effect does it have on its arguments?
b.What does the method hop do? How does it work? What does its second argument do?
4.(7 points) Inheritance Use the following definitions to answer questions a and b.
-------------------------------------------------------------------------------------------------------------Definitions: public class Homer{ public void whine(){ System.out.print("whine() in Homer"); } public void whine2(){ System.out.print("whine2() in" + who()); } private static String who(){ return "Homer"; } }
public interface Whiner{ public void whine2(); }
6
CS61B
public class Bart extends Homer implements Whiner{ public void whine(){ System.out.print("whine() in Bart"); } public void whine2(){ super.whine2(); whine(); } public static String who(){ return "Bart"; } }
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------Answer a and b: a)answer whether compilation and execution of the shown code results in a compile time error, run time error (explain your answer in either case) , or proper compilation and execution(write what it prints if anything) Each question is independent of the other questions.
Homer h1=new Homer(); Homer h2=new Bart(); Whiner b1= new Bart(); Bart b2; Whiner w1; 1.h2.whine2();_______________________________________________________ 2.b2 = (Bart) h1;
7
CS61B
b2.whine2(); ___________________________________________________________
3.w1=b1; h1 = (Homer) w1;________________________________________________ 4.((Homer)b2).who();__________________________________________________
5.w1=(Whiner) h2; w1.whine(); ___________________________________________________________
6.((Homer) (new Bart())).whine(); _________________________________________________
b) Suppose the following definition is added to the class definitions given already: public class Dummy extends Homer{ public void whine2(){ System.out.print("whine() in" + who()); } } Will the definitions still compile, and if not, why not?
c)Suppose the following lines of code are executed within some method and after the call to foo, the value of s1 is "Hip" s1="Sixty-one B"; foo(s1); //s1 is now "Hip"
8
CS61B
s1 is a non final variable of type String. What else must be true about s1 and foo in terms of static/non-static and assignment?
9