Java Midterm Exam, Fall 2011, Exams of Java Programming

A java midterm exam from the computer science and engineering (cse) department, taken in the fall semester of 2011. The exam covers various topics such as operator precedence, java identifiers, class definitions, and method invocations. It includes multiple-choice questions, programming exercises, and coding tasks.

Typology: Exams

2012/2013

Uploaded on 04/09/2013

gajkaraan
gajkaraan 🇮🇳

4.6

(16)

45 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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.
pf3
pf4
pf5

Partial preview of the text

Download Java Midterm Exam, Fall 2011 and more Exams Java Programming in PDF only on Docsity!

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.

(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; else c = ++a + b--;

bool1 =

x =

y =

z =

bool2 =

a =

b =

c =

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 parts.

1) static method 2) constructor

3) class definition (type) 4) instance method

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

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

help 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 ); }

Scratch Paper