old midterms

44
8/12/2019 old Midterms http://slidepdf.com/reader/full/old-midterms 1/44 Signature _____________________  Name ________________________ cs11f ____  Student ID ____________________ CSE 11 Midterm Fall 2008 Page 1 ___________ (10 points) Page 2 ___________ (22 points) Page 3 ___________ (23 points) Page 4 ___________ (17 points) Page 5 ___________ (12 points) Total ___________ (84 points = 80 base points + 4 points EC [5%])

Upload: themattchan

Post on 03-Jun-2018

234 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 1/44

Signature _____________________   Name ________________________

cs11f ____   Student ID ____________________

CSE 11

Midterm

Fall 2008

Page 1 ___________ (10 points)

Page 2 ___________ (22 points)

Page 3 ___________ (23 points)

Page 4 ___________ (17 points)

Page 5 ___________ (12 points)

Total  ___________ (84 points = 80 base points + 4 points EC [5%])

Page 2: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 2/44

  1

(Partial) Operator Precedence Table

Operators Associativity

* / % left to right

+ - left to right

< <= > >= left to right

== != left to right

&& left to right

|| left to right

= right to left

1) Which of the following are not valid Java identifiers? (Circle your answer(s).)

1stAndTen double_down First&Ten main 

choice? Upper-Case five5five _420_

2) Using the operator precedence table above, evaluate each expression and state what gets printed. Remember

short-circuit evaluation with && and ||.

int x = 6;int y = -2;int z = 13;boolean b = !(x – 6 < y || z == 2 * x + 1);

System.out.print( "b = " + b ); ____________________

b = (x + y < z || 4 * y + z > x) && x > y;

System.out.print( "b = " + b ); ____________________

x = z + x % 4 + y * 2;

System.out.print( "x = " + x ); ____________________

3) What gets printed with each of the following statements?

int a = 1;int b = 3;int c = 5;

System.out.println( a + (b + c) + " = " + (a + b) + c );

______________________________________

System.out.println( (a + b + c) + " = " + a + b + c );

______________________________________

System.out.println( (a + b) + c + " = " + a + (b + c) );

______________________________________

Page 3: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 3/44

  2

4) Write a single method that draws a filled circle on the canvas when the mouse is clicked. The circle should be

100 by 100 pixels centered at the point of the mouse click. Here is the signature for the FilledOval constructor:

FilledOval( double x, double y, double width, double height, DrawingCanvas canvas)

5) Assume we have a Java source file named Tunes.java and it uses at least one class in the objectdraw library.

Write the full Unix command to compile this Java program.

 ________________________________________________________________________

This command will produce a file named

 ________________________________________________________________________

Write the full Unix command to run this as a Java application.

 ________________________________________________________________________

Assume we have correctly written a Tunes.html file. Write the full Unix command to run the above program as

an applet. ________________________________________________________________________

6) What gets printed in the following program fragment?

final int MAX = 4;int i = 2;

int j;

while ( i++ < MAX ){j = 7;

while ( --j > MAX ){System.out.println( i + " " + j );

}

System.out.println( i + " " + j );}

Page 4: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 4/44

  3

Output:

this.a = __________

this.b = __________

c = __________

b = __________

a = __________

a = __________

b = __________

c = __________

this.a = __________

this.b = __________

method2() result = __________

this.a = __________

this.b = __________

Use the numbers below to identify various program parts.

1) class definition (type) 5) instance method

2) static variable 6) local variable3) instance variable 7) formal parameter

4) static method 8) constructor

 _____ Test7 on line 1 _____ a on line 3

 _____ b on line 4 _____ x on line 15

 _____ main() on line 6 _____ ref on line 8

 _____ Test7() on line 11 _____ a on line 17

 _____ method1() on line 15 _____ c on line 5

7) What output is produced by the following program?

1 public class Test72 {3 private int a;4 private boolean b;5 private static int c = 42;

6 public static void main( String[] args )7 {

8 Test7 ref = new Test7();

9 ref.method1( 5 );10 }

11 public Test7()12 {13 a = 1;14 }

15 public void method1( int x )16 {17 int a = x;18 int b;

19 b = this.a + 2;20 this.a = b * 3;

21 System.out.println( "this.a = " + this.a );22 System.out.println( "this.b = " + this.b );23 System.out.println( "c = " + c );24 System.out.println( "b = " + b );25 System.out.println( "a = " + a );26 System.out.println( "method2() result = " + method2( x + b ) );27 System.out.println( "this.a = " + this.a );28 System.out.println( "this.b = " + this.b );29 }

30 private int method2( int x )31 {

32 int a = x;33 int c = this.a + a;

34 b = a != c;

35 System.out.println( "a = " + a );36 System.out.println( "b = " + b );37 System.out.println( "c = " + c );38 System.out.println( "this.a = " + this.a );39 System.out.println( "this.b = " + this.b );

40 this.a = a + 2;41 this.b = b == false;

42 return x + 3;

43 }44 }

Page 5: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 5/44

  4

8) Given the following if – else if sequence below, fill in the blanks to produce an equivalent result with a

switch statement.

int x = /* some value */;String str;

if ( x / 2 == 2 )str = "2 stars";

else if ( x / 2 == 4 )str = "4 stars";

else if ( x / 2 == 6 )str = "6 stars";

elsestr = "a comet";

System.out.println( str );

9) What is the output of this recursive method if it is invoked with the actual argument of 5, as in

ref.mystery( 5 ); ? Draw Stack Frames to help you answer this question.

int mystery( int a ){int b = a + 3;

if ( b > 5 ){a = mystery( a - 1 );System.out.println( a + " " + b );

}else{a = b + 3;System.out.println( a + " " + b );

}

return b;}

switch( _____________ ){____________:str = "2 stars"; 

____________;

____________:str = "4 stars"; 

____________;

____________:str = "6 stars"; 

____________;

____________:str = "a comet"; 

____________;}

System.out.println( str );

Page 6: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 6/44

  5

10) Given the following definitions:

And the following variable definitions:

private Puppy puppy;private Kitty kitty;private Speakable speakable;

Indicate what gets printed with the following statements (each statement is executed in the order it appears).

puppy = new Puppy();kitty = new Kitty();

System.out.println( puppy.speak() ); ____________________________

System.out.println( puppy.wag() ); ____________________________

System.out.println( kitty.speak() ); ____________________________

System.out.println( kitty.sleep( 2000 ) ); ____________________________

speakable = puppy;

System.out.println( speakable.getClass().getName() ); ____________________________

System.out.println( speakable.speak() ); ____________________________

speakable = kitty;

System.out.println( speakable.getClass().getName() ); ____________________________

System.out.println( speakable.speak() ); ____________________________

What two things would we need to change in Speakable.java, Puppy.java, and/or Kitty.java in order for the

statement speakable.sleep( 1000 ); to compile and work properly? Be specific.

public interface Speakable{public String speak();

}

public class Puppy implements Speakable{private static final String

PUPPY_SPEAK = "Bark";

public Puppy()

{// ctor initialization here

}

public String speak(){return PUPPY_SPEAK;

}

public String wag(){return "wag wag";

}}

public class Kitty implements Speakable{private static final String

KITTY_SPEAK = "Meow";

public Kitty()

{// ctor initialization here

}

public String speak(){return KITTY_SPEAK;

}

public String sleep( int time ){return time + " second cat nap";

}}

1)

2)

Page 7: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 7/44

  6

Scratch Paper

Page 8: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 8/44

Signature _____________________  Name ________________________

cs11f ____  Student ID ____________________

CSE 11

Midterm

Fall 2009

Page 1 ___________ (12 points)

Page 2 ___________ (24 points)

Page 3 ___________ (30 points)

Page 4 ___________ (23 points)

Page 5 ___________ (12 points)

Total  ___________ (101 points = 96 base points + 5 points EC [5%])

Page 9: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 9/44

  1

(Partial) Operator Precedence Table

Operators Associativity

! ++ -- (pre & post inc/dec) right to left* / % left to right+ - left to right< <= > >= left to right== != left to right&& left to right|| left to right= right to left

1) What are the values of   x  and   y  (left) and   a  and   b  (right) after the following code segments are executed?

Assume we have a Java source file named Program.java and it uses at least one class in the objectdraw library.

Write the full Unix command to compile this Java program.

 ________________________________________________________________________

This command will produce a file named:

 ________________________________________________________________________

Write the full Unix command to run this as a Java application.

 ________________________________________________________________________

Assume we have correctly written a Program.html file. Write the full Unix command to run the above programas an applet.

 ________________________________________________________________________

x =

y =

i nt x = 2, y = 4;

i f ( x++ >= 3 | | - - y >= 3 )x = x++ + - - y;

el sex = ++x + y- - ;

a =

b =

i nt a = 2, b = 4;

i f ( a++ >= 3 && - - b >= 3 )a = a++ + - - b;

el sea = ++a + b- - ;

Page 10: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 10/44

  2

2) Given the following definition of class Thing1, what is the output of the Java application Question2?

publ i c cl ass Thi ng1{

pri vat e i nt count ;

publ i c Thi ng1( i nt count ){

t hi s . count = count ;}

publ i c i nt get Count ( ){

r et urn t hi s. count ;}

publ i c voi d set Count ( i nt count ){

t hi s . count = count ;}

publ i c St r i ng toStr i ng(){

i f ( th i s .count == 1 )r eturn "one";

el se i f ( thi s . count == 2 )r eturn "t wo";

el se i f ( thi s . count == 3 )

return "three";el se

r eturn "t oo many";}

publ i c st ati c voi d swap1( Thi ng1 t 1, Thi ng1 t 2 ){

 Thi ng1 t emp;

t emp = t 1;t 1 = t 2;t 2 = t emp;

}}

publ i c cl ass Questi on2{

publ i c stati c voi d mai n( St r i ng[ ] ar gs ){

 Thi ng1 f i r st = new Thi ng1( 3 ) ; Thi ng1 second = new Thi ng1( 4 ) ;

Sys tem. out . pr i nt l n( f i rs t . toSt r i ng( ) ) ;System. out . pri nt l n( second. t oSt ri ng( ) ) ;

 Thi ng1. swap1( f i r st , second ) ;

Sys tem. out . pr i nt l n( f i rs t . toSt r i ng( ) ) ;System. out . pri nt l n( second. t oSt ri ng( ) ) ;

 Thi ng1 t hi r d = new Thi ng1( 1 ) ; Thi ng1 f our t h = new Thi ng1( 2 ) ; ;second. set Count ( t hi r d. get Count ( ) ) ;f i r s t = f our t h;

Sys tem. out . pr i nt l n( f i rs t . toSt r i ng( ) ) ;System. out . pri nt l n( second. t oSt ri ng( ) ) ;System. out. pr i nt l n( t hi rd. t oStr i ng() ) ;System. out. pr i nt l n( f ourt h. t oSt r i ng() ) ;

System. out . pri nt l n(

f i rs t . toSt r i ng( ) . equal s ( f ourt h. toStr i ng( ) )) ;

System. out . pri nt l n(second. toStr i ng() . equal s( thi rd. t oStr i ng() )

) ;System. out. pr i nt l n( f i rst == f ourt h ) ;Syst em. out . pr i nt l n( second == t hi r d ) ;

}}

Output

 ____________

 ____________

 ____________

 ____________

 ____________

 ____________

 ____________

 ____________

 ____________

 ____________

 ____________

 ____________

Page 11: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 11/44

  3

Output

 Test 3. a = ________  

t hi s. b = ________  

t hi s. c = ________  

c = ________  

b = ____ ____  

a = ________  

 Test 3. a = ________  

t hi s. b = ________  

t hi s. c = ________  

a = ________  

b = ____ ____  

c = ________  

r esul t = _____ ___  

 Test 3. a = ________  

t hi s. b = ________  

t hi s. c = ________  

a = ________  

b = ____ ____  

c = ________  

x = ________  

Use the numbers below to identify various program parts.

1) static method 2) constructor3) class definition (type) 4) instance method5) static variable 6) local variable7) instance variable 8) formal parameter9) actual argument

 _____ Test 3( )  on line 11 _____ a on line 38

 _____ met hod2( ) on line 36 _____ c on line 5

 _____ Test 3 on line 1 _____ a on line 3

 _____ ref . c on line 9 _____ x on line 15

 _____ mai n( )  on line 6 _____ re f on line 8

3) What output is produced by the following program?

1 publ i c cl ass Test 32 {3 pr i vate stat i c i nt a;4 pr i vat e i nt b;5 pr i vate i nt c;

6 publ i c stati c voi d mai n( St ri ng[ ] args )7 {8 Test 3 r ef = new Test 3( ) ;

9 r ef . met hod1( r ef . c ) ;10 }

11 publ i c Test 3( )12 {13 c = 3;14 }

15 publ i c voi d method1( i nt x )16 {17 i nt c = x++;18 i nt b;

19 b = c + 3;20 a = b + 2;

21 System. out . pri nt l n( "Test3. a = " + Test3. a ) ;22 System. out. pr i nt l n( "t hi s . b = " + t hi s . b ) ;23 Sys tem. out . pr i nt l n( " th i s .c = " + th i s .c ) ;24 System. out. pr i nt l n( "c = " + c ) ;25 System. out . pri nt l n( "b = " + b ) ;26 System. out . pri nt l n( "a = " + a ) ;27 System. out . pri nt l n( "r esul t = " + met hod2( c + b ) ) ;28 System. out . pri nt l n( "Test3. a = " + Test3. a ) ;29 System. out. pr i nt l n( "t hi s . b = " + t hi s . b ) ;30 Sys tem. out . pr i nt l n( " th i s .c = " + th i s .c ) ;31 System. out . pri nt l n( "a = " + a ) ;32 System. out . pri nt l n( "b = " + b ) ;33 System. out. pr i nt l n( "c = " + c ) ;34 System. out. pr i nt l n( "x = " + x ) ;35 }

36 pr i vat e i nt method2( i nt x )

37 {38 i nt a = x;39 i nt c = thi s .c + Test3. a;

40 x = b = a + c;

41 System. out . pri nt l n( "Test3. a = " + Test3. a ) ;42 System. out. pr i nt l n( "t hi s . b = " + t hi s . b ) ;43 Sys tem. out . pr i nt l n( " th i s .c = " + th i s .c ) ;44 System. out . pri nt l n( "a = " + a ) ;45 System. out . pri nt l n( "b = " + b ) ;46 System. out. pr i nt l n( "c = " + c ) ;

47 Test3. a = a + 2;48 thi s .b = b + c;

49 r et ur n x + 3;

50 }51 }

Page 12: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 12/44

  4

4) What gets printed in the following code fragment?

f i nal i nt MAX = 6;i nt i = 3;i nt j ;

whi l e ( ++i < MAX ){

 j = 12;

whi l e ( j > MAX + i ){Syst em. out . pr i nt l n( i + " " + j ) ; j - - ;

}

Syst em. out . pr i nt l n( i + " " + j ) ;}

What is the output of this recursive method if it is invoked as r ef . myst er y( 6 ) ; ? Draw Stack Frames tohelp you answer this question.

i nt myst er y( i nt a ){i nt b = a + 3;

i f ( b > 5 ){

System. out . pr i nt l n( a + " " + b ) ;a = b + myst ery( a - 2 ) ;System. out . pr i nt l n( a + " " + b ) ;

}el se{

Syst em. out . pr i nt l n( " Cease" ) ;System. out . pr i nt l n( a + " " + b ) ;b = a - 3;

System. out . pr i nt l n( a + " " + b ) ;}

r eturn a + b;}

Output

Output

Page 13: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 13/44

  5

5) Given the following definitions:

And the following variable definitions:

pri vate Puppy puppy;pr i vat e Ki t ty ki tt y;pr i vat e Speakabl e speakabl e;

Indicate what gets printed with the following statements (each statement is executed in the order it appears).

puppy = new Puppy( ) ;ki t t y = new Ki t t y( ) ;

speakabl e = ki t t y;

Syst em. out . pr i nt l n( speakabl e. get Cl ass( ) . get Name( ) ) ; _______ ________ ________ _____

Syst em. out . pr i nt l n( speakabl e. speak() ) ; ____________________________

speakabl e = puppy;

Syst em. out . pr i nt l n( speakabl e. get Cl ass( ) . get Name( ) ) ; _______ ________ ________ _____

Syst em. out . pr i nt l n( speakabl e. speak() ) ; ____________________________

Syst em. out . pr i nt l n( puppy. speak() ) ; _______ ________ ________ _____

Syst em. out . pr i nt l n( puppy. wag( ) ) ; _______ ________ ________ _____

Syst em. out . pr i nt l n( ki t t y. speak( ) ) ; ____________________________

Syst em. out . pr i nt l n( ki t t y. sl eep( 1000 ) ) ; ____________________________

What two things would we need to change in Speakable.java, Puppy.java, and/or Kitty.java in order to haveKitty and Puppy objects listen for and handle ActionEvents? Be specific what needs to change in which file(s).

publ i c i nt erf ace Speakabl e{

publ i c St r i ng speak() ;}

publ i c cl ass Puppy i mpl ement s Speakabl e{

pr i vat e stat i c f i nal St r i ngPUPPY_SPEAK = "Bark" ;

publ i c Puppy()

{ / / ctor i ni t i al i zat i on here}

publ i c St r i ng speak(){

r eturn PUPPY_SPEAK;}

publ i c St r i ng wag( ){

r eturn "wag wag";}

}

publ i c cl ass Ki t t y i mpl ement s Speakabl e{

pr i vat e stat i c f i nal St r i ngKI TTY_SPEAK = "Meow";

publ i c Ki tt y( )

{ / / ctor i ni t i al i zat i on here}

publ i c St r i ng speak(){

r eturn KI TTY_SPEAK;}

publ i c St ri ng sl eep( i nt t i me ){

r eturn t i me + " second cat nap" ;}

}

1)

2)

Page 14: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 14/44

  6

Scratch Paper

Page 15: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 15/44

Signature _____________________  Name ________________________

cs11f ____  Student ID ____________________

CSE 11

Midterm

Fall 2010

Page 1 ___________ (16 points)

Page 2 ___________ (14 points)

Page 3 ___________ (30 points)

Page 4 ___________ (16 points)

Page 5 ___________ (10 points)

Total  ___________ (86 points = 82 base points + 4 points EC [5%])

This exam is to be taken by yourself with closed books, closed notes, no electronic devices.You are allowed one side of an 8.5"x11" sheet of paper handwritten by you.

Page 16: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 16/44

  1

(Partial) Operator Precedence Table

Operators Associativity

! ++ -- (pre & post inc/dec) right to left* / % left to right+ - left to right< <= > >= left to right== != left to right&& left to right|| left to right= right to left

1) What are the values of the indicated variables after the following code segments are executed?

What gets printed?

publ i c cl ass Loops{

publ i c stati c voi d mai n( St r i ng[ ] ar gs ){

f i nal i nt MAX = 6, MI N = 4;i nt i = - 2, j = - 3;

f or ( i = MAX; i > MI N; - - i ){

 j = 5;whi l e ( j <= MAX )

{Syst em. out . pr i nt l n( i + " " + j ) ;++j ;

}}

Syst em. out . pr i nt l n( i + " " + j ) ;

} / / end mai n( )}

i nt x = 3, y = 5;bool ean z = ! ( ( x > 4) | | ( y <= 6) ) == ( ( y <= 4) && ( x > 6) ) ;

i f ( x++ >= 4 | | - - y >= 3 )x = x++ + - - y;

el se

x = ++x + y- - ;

i nt a = 3, b = 5;bool ean c = ! ( b > 4) && ( a <= 6) && ( a <= 4) | | ( b > 6) ;

i f ( a++ >= 4 && - - b >= 3 )a = a++ + - - b;

el sea = ++a + b- - ;

x =

y =

z =

a =

b =

c =

Page 17: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 17/44

  2

2) Given the following definition of class Thing2, what is the output of the Java application Question2?

cl ass Thi ng2{

pr i vat e i nt count ;

publ i c Thi ng2( i nt count ){

thi s . count = count;}

publ i c i nt get Count ( ){

return t hi s. count ;}

publ i c voi d set Count ( i nt count ){

thi s . count = count;}

publ i c Str i ng t oSt r i ng(){

i f ( t hi s . count == 5 )return "f i ve";

el se i f ( t hi s . count == 6 )return "s i x";

el se i f ( t hi s . count == 7 )

r et ur n "seven";el se

r eturn "need more" ;}

publ i c voi d swap1( Thi ng2 t 2 ){

 Thi ng2 t emp; Thi ng2 t 1 = t hi s;

t emp = t 1;t 1 = t 2;t 2 = t emp;

}

publ i c voi d swap2( Thi ng2 t 2 ){

i nt t emp;

t emp = t hi s. get Count ( ) ;t hi s. set Count ( t 2. get Count ( ) ) ;t 2. setCount ( t emp ) ;

}}

publ i c cl ass Quest i on2{

publ i c stati c voi d mai n( St ri ng[ ] args ){

 Thi ng2 f i r st = new Thi ng2( 5 ) ; Thi ng2 second = new Thi ng2( 4 ) ;

 Thi ng2 t emp = f i r st ;f i rs t = second;

second = t emp;

System. out . pr i nt l n( f i rs t . toSt r i ng( ) ) ;System. out . pri nt l n( second. t oSt ri ng( ) ) ;

 Thi ng2 t hi r d = new Thi ng2( 7 ) ; Thi ng2 f our t h = new Thi ng2( 6 ) ; ;

t hi rd. swap2( f ourt h ) ;

System. out. pr i nt l n( t hi rd. t oSt r i ng() ) ;System. out. pr i nt l n( f ourt h. toStr i ng( ) ) ;

f i rst . setCount ( thi rd.get Count () ) ;f our t h = second;

System. out. pr i nt l n( f i rst == t hi rd ) ;

System. out . pr i nt l n( second == f our t h ) ;Sys tem. out . pr i nt l n( f i rs t . toSt r i ng( ) . equal s ( th i rd. toStr i ng( ) ) ) ;System. out . pri nt l n( second. t oSt ri ng( ) . equal s( f ourt h. t oSt ri ng( ) ) ) ;

Sys tem. out . pr i nt l n( f i rs t . toSt r i ng( ) ) ;System. out . pri nt l n( second. t oSt ri ng( ) ) ;System. out. pr i nt l n( t hi rd. t oSt r i ng() ) ;System. out. pr i nt l n( f ourt h. toStr i ng( ) ) ;

f i r st = new Thi ng2( 4 ) ;second = new Thi ng2( 7 ) ;

f i r st . swap1( second ) ;

Sys tem. out . pr i nt l n( f i rs t . toSt r i ng( ) ) ;System. out . pri nt l n( second. t oSt ri ng( ) ) ;

}}

Output

 ____________

 ____________

 ____________

 ____________

 ____________

 ____________

 ____________

 ____________ ____________

 ____________

 ____________

 ____________

 ____________

 ____________

Page 18: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 18/44

  3

Output

 Test 3. a = _ _______  

t hi s. b = ________  

t hi s. c = ________  

c = ________  

b = __ ____ __  

a = ________  

 Test 3. a = _ _______  

t hi s. b = ________  

t hi s. c = ________  

a = ________  

b = __ ____ __  

c = ________  

r esul t = _____ ___  

 Test 3. a = _ _______  

t hi s. b = ________  

t hi s. c = ________  

a = ________  

b = __ ____ __  

c = ________  

x = ________  

Use the letters below to identify various program parts.

A) class definition (type) F) instance methodB) local variable G) static variableC) static method H) constructorD) instance variable I) formal parameterE) actual argument

 _____ Test 3( )  on line 11 _____ a on line 38

 _____ met hod2( ) on line 36 _____ c on line 5

 _____ Test 3 on line 1 _____ a on line 3

 _____ ref . c on line 9 _____ x on line 15

 _____ mai n( )  on line 6 _____ re f on line 8

3) What output is produced by the following program?

1 publ i c cl ass Test 32 {3 pr i vate stat i c i nt a;4 pr i vat e i nt b;5 pr i vate i nt c;

6 publ i c stati c voi d mai n( St ri ng[ ] args )7 {8 Test 3 r ef = new Test 3( 5 ) ;

9 r ef . met hod1( r ef . c ) ;10 }

11 publ i c Test 3( i nt c )12 {13 thi s .c = c;14 }

15 publ i c voi d method1( i nt x )16 {17 i nt c = ++x;18 i nt b;

19 b = c + 3;20 a = b + 2;

21 System. out . pri nt l n( "Test3. a = " + Test3. a ) ;22 System. out. pr i nt l n( "t hi s . b = " + t hi s . b ) ;23 Sys tem. out . pr i nt l n( " th i s .c = " + th i s .c ) ;24 System. out. pr i nt l n( "c = " + c ) ;25 System. out . pri nt l n( "b = " + b ) ;26 System. out . pri nt l n( "a = " + a ) ;27 System. out . pri nt l n( "r esul t = " + met hod2( c + b ) ) ;28 System. out . pri nt l n( "Test3. a = " + Test3. a ) ;29 System. out. pr i nt l n( "t hi s . b = " + t hi s . b ) ;30 Sys tem. out . pr i nt l n( " th i s .c = " + th i s .c ) ;31 System. out . pri nt l n( "a = " + a ) ;32 System. out . pri nt l n( "b = " + b ) ;33 System. out. pr i nt l n( "c = " + c ) ;34 System. out. pr i nt l n( "x = " + x ) ;35 }

36 pr i vat e i nt method2( i nt x )

37 {38 i nt a = x;39 i nt c = thi s .c + Test3. a;

40 x = b = a + c;

41 System. out . pri nt l n( "Test3. a = " + Test3. a ) ;42 System. out. pr i nt l n( "t hi s . b = " + t hi s . b ) ;43 Sys tem. out . pr i nt l n( " th i s .c = " + th i s .c ) ;44 System. out . pri nt l n( "a = " + a ) ;45 System. out . pri nt l n( "b = " + b ) ;46 System. out. pr i nt l n( "c = " + c ) ;

47 Test3. a = a + 2;48 thi s .b = b + c;

49 r et ur n x + 3;

50 }51 }

Page 19: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 19/44

  4

4)  What gets printed as a result of the call Q4( 3, - 1 ) ? __________

publ i c voi d Q4( i nt a, i nt b ){

i f ( ( a > 0) && ( b > 0) ){

i f ( a > b ){

Syst em. out . pr i nt l n( "A" ) ;}el se{

Syst em. out . pr i nt l n( "B" ) ;}

}el se i f ( ( a < 0) | | ( b < 0) ){

Syst em. out . pr i nt l n( "C" ) ;}el se{

Syst em. out . pr i nt l n( "D" ) ;}

}

Give an example of values passed as arguments to Q4() that would result in the method printing "D".

Q4( _____ , _____ ) ;

What is the output of this recursive method if it is invoked as r ef . myst er y( 9 ) ; ? Draw Stack Frames tohelp you answer this question.

i nt myst er y( i nt a ){

i nt b = a + 3;

i f ( b > 6 ){

Syst em. out . pr i nt l n( a + " " + b ) ;a = b + myst ery( a - 3 ) ;Syst em. out . pr i nt l n( a + " " + b ) ;

}el se{

Syst em. out . pr i nt l n( a + " " + b ) ;b = a - 4;Syst em. out . pr i nt l n( a + " " + b ) ;System. out . pr i nt l n( "Whoa" ) ;

}

r eturn a + b;}

Output

Page 20: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 20/44

  5

5) Given the following definitions:

And the following variable definitions:pri vate Puppy puppy;pr i vat e Ki t ty ki tt y;pr i vat e Speakabl e speakabl e;

Indicate what gets printed with the following statements (each statement is executed in the order it appears). Ifthere is a compile time error, write "Error".

puppy = new Puppy( ) ;ki t t y = new Ki t t y( ) ;

speakabl e = puppy;

Syst em. out . pr i nt l n( speakabl e. get Cl ass( ) . get Name( ) ) ; _______ ________ ________ _____

Syst em. out . pr i nt l n( speakabl e. wag( ) ) ; _______ ________ ________ _____

Syst em. out . pr i nt l n( speakabl e. speak() ) ; ____________________________

Syst em. out . pr i nt l n( puppy. wag( ) ) ; _______ ________ ________ _____

speakabl e = ki t t y;

Syst em. out . pr i nt l n( speakabl e. get Cl ass( ) . get Name( ) ) ; _______ ________ ________ _____

Syst em. out . pr i nt l n( ki t t y. wag( ) ) ; ____________________________

Syst em. out . pr i nt l n( speakabl e. speak() ) ; ____________________________

Syst em. out . pr i nt l n( speakabl e. sl eep( 1000 ) ) ; _______ ________ ________ _____

What two things would we need to change in order to have Puppy objects listen for and handle ActionEvents?Be specific what needs to change in which file(s).

publ i c i nt erf ace Speakabl e{

publ i c St ri ng speak() ;}

publ i c cl ass Puppy i mpl ement s Speakabl e{

pr i vate stat i c f i nal St r i ngPUPPY_SPEAK = "Bar k" ;

publ i c Puppy( )

{ / / ctor i ni t i al i zat i on here}

publ i c St r i ng speak(){

r et urn PUPPY_SPEAK;}

publ i c St r i ng wag( ){

r et urn " wag wag" ;}

}

publ i c cl ass Ki t t y i mpl ement s Speakabl e{

pr i vat e stat i c f i nal St r i ngKI TTY_SPEAK = "Meow";

publ i c Ki t t y()

{ / / ctor i ni t i al i zat i on here}

publ i c St r i ng speak(){

r eturn KI TTY_SPEAK;}

publ i c St r i ng sl eep( i nt t i me ){

r eturn t i me + " second cat nap" ;}

}

1)

2)

Page 21: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 21/44

  6

Scratch Paper

Page 22: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 22/44

Signature _____________________   Name ________________________

cs11f ____   Student ID ____________________

CSE 11

Midterm

Fall 2011

Page 1 ___________ (18 points)

Page 2 ___________ (18 points)

Page 3 ___________ (31 points)

Page 4 ___________ (13 points)

Page 5 ___________ (8 points)

Total  ___________ (88 points = 84 base points + 4 points EC [5%])(84 points = 100%)

This exam is to be taken by yourself with closed books, closed notes, no electronic devices.

You are allowed one side of an 8.5"x11" sheet of paper handwritten by you.

Page 23: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 23/44

  1

(Partial) Operator Precedence Table

Operators Associativity

! ++ -- (pre & post inc/dec) right to left

* / % left to right

+ - left to right

< <= > >= left to right

== != left to right

&& left to right

|| left to right

= right to left

1) What are the values of the indicated variables after the following code segments are executed?

What gets printed?

public class While{public static void main( String[] args ){

final int MAX = 9, MIN = 5;int i = 7, j = 8;

while ( i <= MAX ){while ( j > MIN ){

++j;System.out.println( i + " " + j );j -= 4;

}i++;j = i;

}

System.out.println( i + " " + j );}

}

int x = 5, y = 3, z;boolean bool1 = !((x > 4) || (y <= 6)) == ((y <= 4) && !(x > 6));

if ( x++ >= 4 || --y >= 3 )z = x++ + --y;

else

z = ++x + y--;

int a = 5, b = 3, c;boolean bool2 = !(b > 4) && (a <= 6) && (a <= 4) || (b > 6);

if ( a++ >= 4 && --b >= 3 )c = a++ + --b;

elsec = ++a + b--;

bool1 =

x =

y =

z =

bool2 =

a =

b =

c =

Page 24: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 24/44

  2

2) Which of the following are valid Java identifiers? (Circle your answer(s).)

1stJavaClass My-First-Java-Class sEvEnTeEn CSE11Is_1_ 

CSE11Is#1 CSE_11 My1stJavaClass float

Given the following definition of class Thing2, what is the output of the Java application Test2?

class Thing2{private int count;

public Thing2( int count ){this.count = count;

}

public int getCount(){return this.count;

}

public void setCount( int count )

{ this.count = count;}

public String toString(){String s = " ";

switch( this.count ){case 3:

s = s + "tres ";break;

case 2:s = s + "duo ";

case 1:s = s + "uno ";break;

default:s = s + "mucho ";break;

}

return s;}

public void swap1( Thing2 t2 )

{Thing2 temp;Thing2 t1 = this;

temp = t1;t1 = t2;t2 = temp;

}

public void swap2( Thing2 t2 ){int temp;

temp = this.getCount();this.setCount( t2.getCount() );t2.setCount( temp );

}}

public class Test2{public static void main( String[] args ){Thing2 first = new Thing2( 4 );Thing2 second = new Thing2( 2 );

Thing2 temp = first;first = second;second = temp;

System.out.println( first.toString() );System.out.println( second.toString() );

Thing2 third = new Thing2( 1 );

Thing2 fourth = new Thing2( 3 );

third.swap2( fourth );

System.out.println( third.toString() );System.out.println( fourth.toString() );

first.setCount( third.getCount() );fourth = second;

System.out.println( first == third );System.out.println( second == fourth );System.out.println( first.toString().equals( third.toString() ) );System.out.println( second.toString().equals( fourth.toString() ) );

System.out.println( first.toString() );System.out.println( second.toString() );

System.out.println( third.toString() );System.out.println( fourth.toString() );

first = new Thing2( 5 );second = new Thing2( 2 );

first.swap1( second );

System.out.println( first.toString() );System.out.println( second.toString() );

}}

Output

 ____________

 ____________

 ____________

 ____________

 ____________

 ____________

 ____________

 ____________

 ____________

 ____________

 ____________

 ____________

 ____________

 ____________

Page 25: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 25/44

  3

Output

this.a = ________ 

Test3.b = ________ 

this.c = ________ 

c = ________ 

b = ________ 

a = ________ 

this.a = ________ 

Test3.b = ________ 

this.c = ________ 

x = ________ 

a = ________ 

b = ________ 

c = ________ 

result = ________ 

this.a = ________ 

Test3.b = ________ 

this.c = ________ 

x = ________ 

a = ________ 

b = ________ 

c = ________ 

Use the numbers below to identify various program part

1) static method 2) constructor

3) class definition (type) 4) instance method5) local variable 6) static variable

7) instance variable 8) formal parameter

9) actual argument

 _____ method1() on line 15 _____ b on line 18

 _____ Test3() on line 11 _____ a on line 3

 _____ b + c on line 27 _____ a on line 11

 _____ main() on line 6 _____ ref on line 8

 _____ Test3 on line 1 _____ b on line 4

3) What output is produced by the following program?

1 public class Test32 {3 private int a;4 private static int b = 2;5 private int c;

6 public static void main( String[] args )7 {

8 Test3 ref = new Test3( 3 );

9 ref.method1( ref.a );10 }

11 public Test3( int a )12 {13 this.a = a;14 }

15 public void method1( int x )16 {17 int c = x--;18 int b;

19 b = a + 2;20 a = c + 3;

21 System.out.println( "this.a = " + this.a );22 System.out.println( "Test3.b = " + Test3.b );23 System.out.println( "this.c = " + this.c );24 System.out.println( "c = " + c );25 System.out.println( "b = " + b );26 System.out.println( "a = " + a );27 System.out.println( "result = " + method2( b + c ) );28 System.out.println( "this.a = " + this.a );29 System.out.println( "Test3.b = " + Test3.b );30 System.out.println( "this.c = " + this.c );31 System.out.println( "x = " + x );32 System.out.println( "a = " + a );

33 System.out.println( "b = " + b );34 System.out.println( "c = " + c );35 }

36 private int method2( int x )37 {38 int b = x;39 int c = this.c + Test3.b;

40 x = a = b + c;

41 System.out.println( "this.a = " + this.a );42 System.out.println( "Test3.b = " + Test3.b );43 System.out.println( "this.c = " + this.c );44 System.out.println( "x = " + x );

45 System.out.println( "a = " + a );46 System.out.println( "b = " + b );47 System.out.println( "c = " + c );

48 Test3.b = b + 2;49 this.c = a + c;

50 return x + 5;51 }52 } 

Page 26: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 26/44

  4

4) 

What is the output of this recursive method if it is invoked as ref.mystery( 10 );? Draw Stack Frames tohelp you answer this question.

int mystery( int a ){

int b = a - 2;

if ( b >= 7 )

{System.out.println( a + " " + b );a = b - mystery( b + 1 );

}else{System.out.println( "Stop" );b = a + 2;

}

System.out.println( a + " " + b );return a + b;

Output

What gets printed if the value of the actual

argument passed to this method is 0? ________ 

public void f5( int x )

{int y = 0;

if ( x <= 1 )y = 3;

if ( x <= 2 )

y = 5;

if ( x == 3 || x >= 4 )y = 7;

else

y = 9;

System.out.println( y );}

What gets printed if the value of the actual

argument passed to this method is 0? ________ 

public void f5( int x )

{int y = 0;

if ( x <= 1 )y = 3;

else if ( x <= 2 )

y = 5;

else if ( x == 3 || x >= 4 )y = 7;

else

y = 9;

System.out.println( y );}

Page 27: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 27/44

  5

 

5) Given the following definitions:

And the following variable definitions:

Thing1 thing1 = new Thing1();Thing2 thing2 = new Thing2();Doable doable;

What gets printed with the following statements (each statement is executed in the order it appears). If there is acompile time error, write "Error".

doable = thing1;

System.out.println( doable.getClass().getName() ); ____________________________

System.out.println( doable.doit() ); ____________________________

System.out.println( thing1.speak() ); ____________________________

doable = thing2;

System.out.println( doable.getClass().getName() ); ____________________________

System.out.println( doable.doit() ); ____________________________

System.out.println( thing2.speak( " Here" ) ); ____________________________

What two changes/additions would be needed to the above interface and class definitions so 

doable.speak() would compile and run for all valid assignments to doable? Be specific what needs to be

added to which file(s). Do not remove or change any of the existing code.

1)

2)

public interface Doable{public abstract String doit();

}

public class Thing1 implements Doable{private String str;

public Thing1()

{this.str = "Me";

}

public String speak(){return this.str;

}

public String doit(){return "Thing1 did it!";

}}

public class Thing2 implements Doable{private String str;

public Thing2()

{this.str = "No, Me";

}

public String speak( String s ){return this.str + s;

}

public String doit(){return "Thing2 does it too!";

}}

Page 28: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 28/44

  6

Scratch Paper

Page 29: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 29/44

Signature _____________________   Name ________________________

cs11f ____   Student ID ____________________

CSE 11

Midterm

Fall 2012

Page 1 ___________ (20 points)

Page 2 ___________ (17 points)

Page 3 ___________ (31 points)

Page 4 ___________ (15 points)

Page 5 ___________ (8 points)

Page 6 ___________ (20 points)

Total  ___________ (111 points = 105 base points + 6 points EC [>5%])(105 points = 100%)

This exam is to be taken by yourself with closed books, closed notes, no electronic devices.

You are allowed one side of an 8.5"x11" sheet of paper handwritten by you.

Page 30: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 30/44

  1

(Partial) Operator Precedence Table

Operators Associativity

! ++ -- (pre & post inc/dec) right to left

* / % left to right

+ - left to right

< <= > >= left to right

== != left to right

&& left to right

|| left to right

= right to left

1) What are the values of the indicated variables after the following code segments are executed?

What gets printed?

public class While{public static void main( String[] args ){

final int MAX = 10, MIN = 5;int i = 7, j = 7;

while ( i < MAX ){while ( j >= MIN ){

++j;System.out.println( i + " " + j );j -= 4; // j = j - 4;

}i++;j = i;

}

System.out.println( i + " " + j );}

}

int x = 4, y = 6, z;boolean bool1 = !((x > 4) || (y <= 6)) == ((y <= 4) && !(x > 6));

if ( x++ >= 4 || --y <= 3 )z = x++ + --y;

else

z = ++x + y--;

int a = 4, b = 6, c;boolean bool2 = !(b > 4) && (a <= 6) && (a <= 4) || (b > 6);

if ( a++ >= 4 && --b <= 3 )c = a++ + --b;

elsec = ++a + b--;

bool1 =

x =

y =

z =

bool2 =

a =

b =

c =

Page 31: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 31/44

  2

2) What is printed by the following code?

Given the following definition of class Thing1, what is the output of the Java application Test2?

class Thing1{private int count;

public Thing1( int count ){this.count = count;

}

public int getCount(){return this.count;

}

public void setCount( int count ){this.count = count;

}

public String toString(){String s = " ";

switch( this.count ){case 1:s = s + "1st ";break;

case 2:s = s + "2nd ";break;

case 3:s = s + "3rd ";

default:s = s + "rest ";break;

}

return s;}

public void swap1( Thing1 t1 ){Thing1 temp;Thing1 t2 = this;

temp = t1;t1 = t2;t2 = temp;

}

public void swap2( Thing1 t1 ){int temp;

temp = this.getCount();this.setCount( t1.getCount() );t1.setCount( temp );

}}

public class Test2{public static void main( String[] args ){Thing1 first = new Thing1( 1 );Thing1 second = new Thing1( 2 );

first.swap1( second );

System.out.println( first.toString() );System.out.println( second.toString() );

Thing1 third = new Thing1( 3 );Thing1 fourth = new Thing1( 4 );;

Thing1 temp = third;third = fourth;fourth = temp;

System.out.println( third.toString() );System.out.println( fourth.toString() );

third = first;fourth.setCount( second.getCount() );

System.out.println( first == third );System.out.println( second == fourth );System.out.println( first.toString().equals( third.toString() ) );System.out.println( second.toString().equals( fourth.toString() ) );

System.out.println( first.toString() );System.out.println( second.toString() );

System.out.println( third.toString() );System.out.println( fourth.toString() );

first = new Thing1( 1 );second = new Thing1( 3 );

first.swap2( second );

System.out.println( first.toString() );System.out.println( second.toString() );

}}

Output

 ____________

 ____________

 ____________

 ____________

 ____________

 ____________

 ____________

 ____________ ____________

 ____________

 ____________

 ____________

 ____________

 ____________

int foo = 37;int bar = 42;boolean foobar = ( foo == bar );System.out.println( foobar ); ____________bar = 37;System.out.println( foobar ); ____________

System.out.println( foo == bar ); ____________

Page 32: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 32/44

  3

Output

this.a = ________ 

Test3.b = ________ 

this.c = ________ 

c = ________ 

b = ________ 

a = ________ 

this.a = ________ 

Test3.b = ________ 

this.c = ________ 

x = ________ 

a = ________ 

b = ________ 

c = ________ 

result = ________ 

this.a = ________ 

Test3.b = ________ 

this.c = ________ 

x = ________ 

a = ________ 

b = ________ 

c = ________ 

Use the numbers below to identify various program part

1) local variable 6) static variable

2) instance variable 7) formal parameter3) static method 8) constructor

4) class definition (type) 9) instance method

5) actual argument

 _____ main() on line 6 _____ x on line 40

 _____ Test3 on line 1 _____ b on line 4

 _____ method2() on line 36 _____ c on line 39

 _____ Test3() on line 11 _____ c on line 5

 _____ ref.a on line 9 _____ c on line 11

3) What output is produced by the following program?

1 public class Test32 {3 private int a;4 private static int b = 2;5 private int c;

6 public static void main( String[] args )7 {

8 Test3 ref = new Test3( 4 );

9 ref.method1( ref.a );10 }

11 public Test3( int c )12 {13 this.c = c;14 }

15 private void method1( int x )16 {17 int c = x--;18 int b;

19 b = a + 2;20 a = c + 3;

21 System.out.println( "this.a = " + this.a );22 System.out.println( "Test3.b = " + Test3.b );23 System.out.println( "this.c = " + this.c );24 System.out.println( "c = " + c );25 System.out.println( "b = " + b );26 System.out.println( "a = " + a );27 System.out.println( "result = " + method2( b + c ) );28 System.out.println( "this.a = " + this.a );29 System.out.println( "Test3.b = " + Test3.b );30 System.out.println( "this.c = " + this.c );31 System.out.println( "x = " + x );32 System.out.println( "a = " + a );

33 System.out.println( "b = " + b );34 System.out.println( "c = " + c );35 }

36 public int method2( int x )37 {38 int b = x;39 int c = this.c + Test3.b;

40 x = a = b + c;

41 System.out.println( "this.a = " + this.a );42 System.out.println( "Test3.b = " + Test3.b );43 System.out.println( "this.c = " + this.c );44 System.out.println( "x = " + x );

45 System.out.println( "a = " + a );46 System.out.println( "b = " + b );47 System.out.println( "c = " + c );

48 Test3.b = b + 2;49 this.c = a + c;

50 return x + 5;51 }52 } 

Page 33: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 33/44

  4

4) 

What is the output of this recursive method if it is invoked as ref.mystery( 8 );? Draw Stack Frames tohelp you answer this question.

int mystery( int a ){

int b = a + 2;

if ( b <= 11 ){System.out.println( a + " " + b );a = b + mystery( b - 1 );

}else{System.out.println( "Whoa" );b = a - 2;

}

System.out.println( a + " " + b );return a - b;} 

Output

What gets printed by the following code? _______

int x = 12;if ( x < 7 ){x += 3; // Same as x = x + 3;

}

else if ( x <= 10 ){x += 6;

}System.out.println( x );

What gets printed by the following code? _______

int x = 12;if ( x > 7 ){x += 2; // Same as x = x + 2;

}

else if ( x >= 10 ){x += 6;

}System.out.println( x );

What gets printed by the following code? _______

int x = 12;if ( x < 7 ){x += 3; // Same as x = x + 3;

}

else{x += 6;

}System.out.println( x );

What gets printed by the following code? _______

int x = 12;if ( x > 7 ){x += 3; // Same as x = x + 3;

}

else{x += 6;

}System.out.println( x );

Page 34: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 34/44

  5

5) Given the following definitions:

And the following variable definitions:Thing1 thing1 = new Thing1();Thing2 thing2 = new Thing2();Speakable speakable;

What gets printed with the following statements (each statement is executed in the order it appears). If there is a

compile time error, write "Error".

speakable = thing1;

System.out.println( speakable.speak() ); ____________________________

System.out.println( speakable.doit() ); ____________________________

System.out.println( thing1.doit( "Here" ) ); ____________________________

speakable = thing2;

System.out.println( speakable.speak() ); ____________________________

System.out.println( speakable.doit() ); ____________________________

System.out.println( thing2.doit() ); ____________________________

What two changes/additions would be needed to the above interface and class definitions so 

speakable.doit( "Do it" ) would compile and run for all valid assignments to speakable? Be

specific what needs to be added to which file(s). Do not remove or change any of the existing code.

1)

2)

public interface Speakable{public abstract String speak();

}

public class Thing1 implements Speakable{private String str;

public Thing1(){

this.str = "Thing1";}

public String speak(){return this.str;

}

public String doit(){return "Thing1 did it!";

}}

public class Thing2 implements Speakable{private String str;

public Thing2(){

this.str = "Thing2";}

public String speak(){return this.str;

}

public String doit( String s ){return "Thing2 " + s;

}}

Page 35: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 35/44

  6

6) Trace the following program and specify its output.

public class Trace{public static void main( String[] args ){foo1();System.out.println( "main1" );foo2();System.out.println( "main2" );

foo3();System.out.println( "main3" );foo2();

}

public static void foo1(){foo2();System.out.println( "A" );

}

public static void foo2(){System.out.println( "B" );foo3();

System.out.println( "C" );}

public static void foo3(){System.out.println( "D" );

}

}

What is the default initial value of a local variable that is defined as an int? _________________

What is the default initial value of an instance variable that is defined as a boolean? ____________

What is the default initial value of an instance variable that is defined as an object reference? ____________

What is the default initial value of an instance variable that is defined as a double? ____________

Will the following code compile? ___________If not, what change do you need to make to the method header (not the method body) so that it will compile?

Explain. Be specific.

public boolean test( int x ){System.out.println( "In test" );return x * x;

}

Page 36: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 36/44

  7

Scratch Paper

Page 37: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 37/44

Signature _____________________   Name ________________________

cs11f ____   Student ID ____________________

CSE 11

Midterm

Fall 2013

Page 1 ___________ (20 points)

Page 2 ___________ (9 points)

Page 3 ___________ (32 points)

Page 4 ___________ (15 points)

Page 5 ___________ (13 points)

Page 6 ___________ (21 points)

Total  ___________ (110 points = 105 base points + 5 points EC [~5%])(105 points = 100%)

This exam is to be taken by yourself with closed books, closed notes, no electronic devices.

You are allowed one side of an 8.5"x11" sheet of paper handwritten by you.

Page 38: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 38/44

  1

 

(Partial) Operator Precedence Table

Operators Associativity

! ++ -- (pre & post inc/dec) right to left

* / % left to right

+ - left to right

< <= > >= left to right

== != left to right

&& left to right

|| left to right

= right to left

1) What are the values of the indicated variables after the following code segments are executed?

What gets printed?

public class While{public static void main( String[] args ){

final int MAX = 11, MIN = 4;int i = 6, j = 8;

while ( i <= MAX ){while ( j > MIN ){

j -= 2;System.out.println( i + " " + j );

}i += 3;j = i;

}

System.out.println( i + " " + j );}

}

int a = 6, b = 2, d;boolean c = !(b > 6) && (a >= 3) && (a <= 4) || (b < 6);

if ( a++ >= 4 && --b >= 2 )d = ++a + b--;

else

d = a++ + --b;

int x = 6, y = 2, w;boolean z = !((x > 4) || (y <= 6)) == ((y <= 4) && (x > 6));

if ( x++ >= 4 || --y >= 3 )w = --x + y++;

elsew = x-- + ++y;

a =

b =

c =

d =

w =

x =

y =

z =

Page 39: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 39/44

  2

2) What gets printed?

int a = 2;int b = 4;int c = 6;

System.out.println( a + b + (c + " = ") + a + (b + c) ); ________________________________

What is the output produced by the following program? (Hint: Draw stack frames)

public class Swap

{private int a;

public Swap( int a ){this.a = a;

}

public void swap( int a, int b ){int tmp;

tmp = a;a = b;b = tmp;

}

public void swap( Swap ref ){int tmp;

tmp = this.a;this.a = ref.a;ref.a = tmp;

}

public static void swap( Swap ref1, Swap ref2 ){Swap tmp;

tmp = ref1;ref1 = ref2;ref2 = tmp;

}

public static void main( String[] args ){int a = 44; Swap ref1;int b = 11; Swap ref2;

ref1 = new Swap(3);ref2 = new Swap(7);

Swap.swap( ref1, ref2 );System.out.println( ref1.a );System.out.println( ref2.a );

ref1 = new Swap(3);ref2 = new Swap(7);

ref1.swap( a, b );System.out.println( a );System.out.println( b );

ref1 = new Swap(3);ref2 = new Swap(7);

ref1.swap( ref2 );System.out.println( ref1.a );System.out.println( ref2.a );

}}

Output

The different swap() method definitions have the same

name but differ in their formal parameters. This is an

example of method _______________________ .

Page 40: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 40/44

  3

Output

Test3.a = ________

this.b = ________

this.c = ________

c = ________

b = ________

a = ________

x = ________Test3.a = ________

this.b = ________

this.c = ________

x = ________

a = ________

b = ________

c = ________

result = ________

Test3.a = ________this.b = ________

this.c = ________

x = ________

a = ________

b = ________

c = ________ 

Use the letters below to identify various program parts.

A) static method F) constructor

B) local variable G) static variableC) instance variable H) actual argument

D) class definition (type) I) instance method

E) formal parameter

 _____ 5 on line 8 _____ b on line 18

 _____ main() on line 6 _____ x on line 37

 _____ Test3() on line 11 _____ c on line 40

 _____ Test3 on line 1 _____ b on line 4

 _____ method1() on line 15 _____ a on line 3

3) What output is produced by the following program?

1 public class Test32 {3 private static int a;4 private int b;5 private int c = 3;

6 public static void main( String[] args )7 {

8 Test3 ref = new Test3( 5 );

9 ref.method1( Test3.a );10 }

11 public Test3( int b )12 {13 this.b = b;14 }

15 private void method1( int x )16 {17 int c = x + 4;18 int b;

19 b = a + 2;20 a = c + 3;

21 System.out.println( "Test3.a = " + Test3.a );22 System.out.println( "this.b = " + this.b );23 System.out.println( "this.c = " + this.c );24 System.out.println( "c = " + c );25 System.out.println( "b = " + b );26 System.out.println( "a = " + a );27 System.out.println( "x = " + x );28 System.out.println( "result = " + method2( 11 ) );29 System.out.println( "Test3.a = " + Test3.a );30 System.out.println( "this.b = " + this.b );31 System.out.println( "this.c = " + this.c );32 System.out.println( "x = " + x );

33 System.out.println( "a = " + a );34 System.out.println( "b = " + b );35 System.out.println( "c = " + c );36 }

37 public int method2( int x )38 {39 int a = x;40 int c = b;

41 x = b;

42 System.out.println( "Test3.a = " + Test3.a );43 System.out.println( "this.b = " + this.b );44 System.out.println( "this.c = " + this.c );

45 System.out.println( "x = " + x );46 System.out.println( "a = " + a );47 System.out.println( "b = " + b );48 System.out.println( "c = " + c );

49 Test3.a = a + 2;50 this.c = x + c;

51 return x + 5;52 }53 }

Page 41: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 41/44

  4

4) 

What is the output of this recursive method if it is invoked as ref.mystery( 5 );? Draw Stack Frames tohelp you answer this question.

int mystery( int a ){

int b = a + 3;

if ( b <= 11 ){System.out.println( a + " " + b );a = b - mystery( b - 1 );

}else{System.out.println( "Stop" );b = a - 2;

}

System.out.println( a + " " + b );return a + b;} 

Output

What gets printed by the following code? _______

int x = 13;if ( x < 7 ){x += 3; // Same as x = x + 3;

}

if ( x >= 10 ){x += 4;

}System.out.println( x );

What gets printed by the following code? _______

int x = 13;if ( x < 7 ){x += 3; // Same as x = x + 3;

}

if ( x >= 15 ){x += 4;

}System.out.println( x );

What gets printed by the following code? _______

int x = 13;if ( x > 7 ){x += 3; // Same as x = x + 3;

}

if ( x <= 12 ){x += 4;

}System.out.println( x );

What gets printed by the following code? _______

int x = 13;if ( x > 7 ){x += 3; // Same as x = x + 3;

}

if ( x >= 15 ){x += 4;

}System.out.println( x );

Page 42: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 42/44

  5

5) Given the following definitions:

And the following variable definitions in another class:Thing1 thing1 = new Thing1();Thing2 thing2 = new Thing2();Printable printable;

What gets printed with the following statements (each statement is executed in the order it appears). If there is a

compile time error, write "Error".

printable = thing1;

System.out.println( printable.print( true ) ); ____________________________________________

System.out.println( thing1.print() ); ____________________________________________

System.out.println( printable.print() ); ____________________________________________

printable = thing2;

System.out.println( printable.print( "CS11FZZ" ) ); ____________________________________________

System.out.println( printable.print( false ) ); ____________________________________________

System.out.println( thing2.print( "CS11FZZ" ) ); ____________________________________________

What two additions would be needed to the above interface and class definitions so printable.print()

would compile and run for all valid assignments to printable? Be specific what needs to be added to which

file(s). Do not remove or change any of the existing code.

1)

2)

public interface Printable{public abstract String print( boolean duplex );

}

class Thing1 implements Printable{private String str;

public Thing1(){

this.str = "Thing 1";}

public String print( boolean duplex ){return this.str + " duplex = " + duplex;

}

public String print(){// print single sided by defaultreturn this.print( false );

}}

class Thing2 implements Printable{private String str;

public Thing2(){

this.str = "Thing 2";}

public String print( boolean duplex ){return this.str + " duplex = " + duplex;

}

public String print( String user ){System.out.print( user + ": " );

// print double sided by defaultreturn this.print( true );

}

}

Page 43: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 43/44

  6

6) Trace the following program and specify its output.

public class Trace{public static void main( String[] args ){System.out.println( "main1" );foo3();System.out.println( "main2" );foo2();System.out.println( "main3" );

foo1();}

public static void foo1(){System.out.println( "A" );

}

public static void foo2(){System.out.println( "B" );foo1();System.out.println( "C" );

}

public static void foo3(){System.out.println( "D" );foo2();System.out.println( "E" );

}}

What is the equivalent Java expression for the following expression such that no ! operators are used?

( !=  is a different operator than ! )

!( x >= 42 || y != 37 )  _______________________________________________

What is the default initial value of an instance variable that is defined as a boolean? ____________

What is the default initial value of an instance variable that is defined as an object reference? ____________

What is the default initial value of an instance variable that is defined as an int? ____________

What is the default initial value of a local variable that is defined as a double? _________________

If  b is a boolean variable, then the statement

b = ( b == false );

has what effect? _____

A) It causes a compile-time error message.B) It causes a run-time error message.

C) It causes b to have the value false regardless

of its value just before the statement was executed.D) It always changes the value of b.

E) It changes the value of b if and only if b had value

true just before the statement was executed.

Which of the following is equivalent to and has

the same effect as

b = ( b == false ); ? _____

A) b = ( b == true ); 

B) b = ( b != true ); 

C) b = ( b != false ); 

D) b = ( b == b ); 

E) b = ( b != b ); F) More than one of the above statements is

equivalent

Page 44: old Midterms

8/12/2019 old Midterms

http://slidepdf.com/reader/full/old-midterms 44/44

 

Scratch Paper