Docsity

Java Midterm Exam Solutions, Exams of Java Programming

Toggle sidebar
bg1
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%])
pf3
pf4
pf5

Partial preview of the text

Download Java Midterm Exam Solutions and more Exams Java Programming in PDF only on Docsity!

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%])

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

______________________________________

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 variable

3) 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 Test 2 { 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 }

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"; else str = "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 );

Scratch Paper