Download Unix Command - Java- Past Exam Paper and more Exams Java Programming in PDF only on Docsity!
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%])
(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 program
as an applet.
________________________________________________________________________
x =
y =
int x = 2, y = 4;
if ( x++ >= 3 || --y >= 3 )
x = x++ + --y;
else
x = ++x + y--;
a =
b =
int a = 2, b = 4;
if ( a++ >= 3 && --b >= 3 )
a = a++ + --b;
else
a = ++a + b--;
Output
Test3.a = ________
this.b = ________
this.c = ________
c = ________
b = ________
a = ________
Test3.a = ________
this.b = ________
this.c = ________
a = ________
b = ________
c = ________
result = ________
Test3.a = ________
this.b = ________
this.c = ________
a = ________
b = ________
c = ________
x = ________
Use the numbers below to identify various program parts.
1) static method 2) constructor
3) class definition (type) 4) instance method
5) static variable 6) local variable
7) instance variable 8) formal parameter
9) actual argument
_____ Test3() on line 11 _____ a on line 38
_____ method2() on line 36 _____ c on line 5
_____ Test3 on line 1 _____ a on line 3
_____ ref.c on line 9 _____ x on line 15
_____ main() on line 6 _____ ref on line 8
3) What output is produced by the following program?
1 public class Test 2 { 3 private static int a; 4 private int b; 5 private int c;
6 public static void main( String[] args ) 7 { 8 Test3 ref = new Test3();
9 ref.method1( ref.c ); 10 }
11 public Test3() 12 { 13 c = 3; 14 }
15 public void method1( int x ) 16 { 17 int c = x++; 18 int b;
19 b = c + 3; 20 a = b + 2;
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( "result = " + method2( c + b ) ); 28 System.out.println( "Test3.a = " + Test3.a ); 29 System.out.println( "this.b = " + this.b ); 30 System.out.println( "this.c = " + this.c ); 31 System.out.println( "a = " + a ); 32 System.out.println( "b = " + b ); 33 System.out.println( "c = " + c ); 34 System.out.println( "x = " + x ); 35 }
36 private int method2( int x ) 37 { 38 int a = x; 39 int c = this.c + Test3.a;
40 x = b = a + c;
41 System.out.println( "Test3.a = " + Test3.a ); 42 System.out.println( "this.b = " + this.b ); 43 System.out.println( "this.c = " + this.c ); 44 System.out.println( "a = " + a ); 45 System.out.println( "b = " + b ); 46 System.out.println( "c = " + c );
47 Test3.a = a + 2; 48 this.b = b + c;
49 return x + 3; 50 } 51 }
4) What gets printed in the following code fragment?
final int MAX = 6;
int i = 3;
int j;
while ( ++i < MAX )
j = 12;
while ( j > MAX + i )
System.out.println( i + " " + j );
j--;
System.out.println( i + " " + j );
What is the output of this recursive method if it is invoked as ref.mystery( 6 );? Draw Stack Frames to
help you answer this question.
int mystery( int a )
int b = a + 3;
if ( b > 5 )
System.out.println( a + " " + b );
a = b + mystery( a - 2 );
System.out.println( a + " " + b );
else
System.out.println( "Cease" );
System.out.println( a + " " + b );
b = a - 3;
System.out.println( a + " " + b );
return a + b;
Output
Output
Scratch Paper