Docsity

Java Programming: Comparing Object References and Method Calls, Exams of Java Programming

Toggle sidebar
bg1
Signature _____________________
Name ________________________
cs11f ____
Student ID ____________________
CSE 11
Final
Fall 2011
Page 1 ___________ (17 points)
Page 2 ___________ (25 points)
Page 3 ___________ (31 points)
Page 4 ___________ (14 points)
Page 5 ___________ (10 points)
Page 6 ___________ (19 points)
Page 7 ___________ (20 points)
Page 8 ___________ (12 points)
Page 9 ___________ (31 points)
Page 10 ___________ (9 points)
Total ___________ (188 points = 179 base points + 9 points EC [5%])
(100%)
This exam is to be taken by yourself with closed books, closed notes, no electronic devices.
You are allowed both sides of an 8.5"x11" sheet of paper handwritten by you.
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Java Programming: Comparing Object References and Method Calls and more Exams Java Programming in PDF only on Docsity!

Signature _____________________ Name ________________________

cs11f ____ Student ID ____________________

CSE 11

Final

Fall 2011

Page 1 ___________ (17 points)

Page 2 ___________ (25 points)

Page 3 ___________ (31 points)

Page 4 ___________ (14 points)

Page 5 ___________ (10 points)

Page 6 ___________ (19 points)

Page 7 ___________ (20 points)

Page 8 ___________ (12 points)

Page 9 ___________ (31 points)

Page 10 ___________ (9 points)

Total ___________ (188 points = 179 base points + 9 points EC [5%])

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

You are allowed both sides 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) Which of the following are valid Java identifiers? (Circle your answer(s).)

FiveStar Five-Star 5_Star INT

5Star Five_Star Five***** integer

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

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

System.out.println( !( a + b > c ) ); ________

System.out.println( a + b * c - c / a % c ); ________

System.out.println( a / c + a % c ); ________

System.out.println( !(a > 4 || b <= 6) == a >= 4 && c > 6 ); ________

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

short-circuit evaluation with && and ||.

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

!( x < -10 && y != 7 ) ____________________________________________

int x = 7, y = 5, z;

boolean bool2 = !((x > 4) || (y <= 6)) == ((y <= 4) && (x > 6));

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

z = x++ + --y;

else

z = ++x + y--;

int a = 7, b = 5, c;

boolean bool1 = !(b > 4) && (a <= 6) && (a <= 4) || !(b > 6);

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

c = a++ + --b;

else

c = ++a + b--;

bool1 =

a =

b =

c =

bool2 =

x =

y =

z =

7) A bear is an animal and a zoo has many animals, including bears. Three classes Animal, Bear, and Zoo are

declared to represent animal, bear, and zoo objects. Which of the following is the most appropriate set of

declarations? ________

A D

class Bear extends Animal, Zoo { ... } class Bear extends Animal implements Zoo { ... }

B E

class Animal extends Bear { ... } class Bear extends Animal { ... } class Zoo { private Animal[] zooAnimals; } class Zoo { private Animal[] zooAnimals; }

C F

class Animal extends Zoo { class Zoo extends Animal { private Bear zooBear; private Bear[] zooAnimals; } }

8) Complete the following method which is intended to return the index of the smallest value in the array

numbers. You can assume the array is not empty. If the smallest value appears more than once in the array,

return the index of the last occurrence of this smallest value.

public static int findSmallest( int[] numbers ) { int smallest = _______________;

for ( int i = _____________; ______________________________________________________; __________ ) { if ( __________________________________________________________________ ) { _______________________________________________________ ; } } return _______________________; }

9) What is the output produced by the following recursive program? (Hint: draw stack frames)

public class Mystery { public static void main( String[] args ) { Mystery ref = new Mystery();

System.out.println( ref.mystery( 6 ) ); }

public int mystery( int a ) { int b = a - 3; int c = a + 5;

if ( b > 0 ) { System.out.println( a + " " + b + " " + c ); c = b + mystery( --a ); System.out.println( a + " " + b + " " + c ); } else { c = a - b; System.out.println( "Whoa!" ); System.out.println( a + " " + b + " " + c ); }

return c; } }

Output

10) Given the following definition of class Thing2, what is the output of the Java application Test10?

class Thing { 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 1: s = s + "first ";

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

case 3: s = s + "last "; break;

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

return s; }

public void swap1( Thing2 t2 ) { int temp;

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

public void swap2( Thing2 t2 ) { Thing2 temp; Thing2 t1 = this;

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

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

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

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

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

third.swap1( fourth );

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

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

System.out.println( third == first ); System.out.println( fourth == second ); 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( 1 ); second = new Thing2( 2 );

first.swap2( second );

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

Output

____________

____________

____________

____________

____________

____________

____________

____________

____________

____________

____________

____________

____________

____________

13) Given the following definitions:

And the following variable definitions:

Thing1 thing1; Thing2 thing2; Doable doable;

Indicate which are valid Java statements. Consider each statement executed sequentially in the order it appears.

A) Valid Java statement – No Compiler Error

B) Invalid Java statement – Compiler Error

public interface Doable { public abstract void doit(); }

public class Thing1 implements Doable { private static final String SPEAK = "Me";

public Thing1() { // ctor initialization here }

public String speak() { return SPEAK; }

public void doit() { // Thing1 does its thing } }

public class Thing2 implements Doable { public static final String SPEAK = "No, Me";

public Thing2() { // ctor initialization here }

public String speak( String s ) { return SPEAK + s; }

public void doit() { // Thing2 does its thing } }

Hint: What does the compiler know about

any reference variable at compile time (vs.

run time)?

thing1 = new Thing1(); _______

thing1.speak(); _______

thing1.doit(); _______

thing1.speak( " Mine" ); _______

String s1 = Thing1.SPEAK; _______

thing2 = new Thing2(); _______

thing2.speak(); _______

thing2.doit(); _______

thing2.speak( " Mine" ); _______

String s2 = Thing2.SPEAK; _______

doable = new Thing1(); _______

doable.speak(); _______

doable.doit(); _______

doable = thing2; _______

doable.speak( " Mine" ); _______

doable.doit(); _______

thing1 = thing2; _______

thing1 = doable; _______

doable = new Doable(); _______

14) Given the following class definitions:

abstract class Animal { private String name; public Animal() { this( "Animal" ); } public Animal( String name ) { this.name = name; } public String toString() { return this.name; } public abstract String speak(); }

class Cat extends Animal { public Cat() { this( "Ko Ko" ); } public Cat( String name ) { super( name + " Cat" ); } public String speak() { return "Meow"; } public String speak( String name ) { return name + " Meow"; } }

class Tiger extends Cat { public Tiger( String name ) { super( name + " Tiger" ); } public String speak() { return super.speak( "Great" ); } }

class BigTiger extends Tiger { public BigTiger() { super( "Brina" ); } public BigTiger( String name ) { super( name ); } public String speak() { return "Roar"; } public String speak( String name ) { return name + " Roar"; } }

final class Lion extends Cat { public String speak() { return "Chung Lion " + super.speak(); } public String softer() { return "Katherine " + super.speak(); } }

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

Animal a;

a = new Lion(); System.out.println( a + " says " + a.speak() );

a = new BigTiger(); System.out.println( a + " says " + a.speak() );

a = new Cat(); System.out.println( a + " says " + a.speak() );

a = new Tiger( "Zach" ); System.out.println( a + " says " + a.speak() );

a = new BigTiger( "Katherine" ); System.out.println( a + " says " + ((Cat) a).speak( "Big" ) );

What gets printed when this program is run?

Output

this.a = ________

Test16.b = ________

this.c = ________

c = ________

b = ________

a = ________

this.a = ________

Test16.b = ________

this.c = ________

x = ________

a = ________

b = ________

c = ________

result = ________

this.a = ________

Test16.b = ________

this.c = ________

x = ________

a = ________

b = ________

c = ________

Use the letters below to identify various program parts.

A) instance variable F) formal parameter

B) static method G) constructor

C) class definition (type) H) instance method

D) local variable I) static variable

E) actual argument

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

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

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

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

_____ Test16 on line 1 _____ b on line 4

16) What output is produced by the following program?

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

6 public static void main( String[] args ) 7 { 8 Test16 ref = new Test16( 4 );

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

11 public Test16( 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( "Test16.b = " + Test16.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( "Test16.b = " + Test16.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 + Test16.b;

40 x = a = b + c;

41 System.out.println( "this.a = " + this.a ); 42 System.out.println( "Test16.b = " + Test16.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 Test16.b = b + 2; 49 this.c = a + c;

50 return x + 5; 51 } 52 }

Given the following class definitions for class Foo, class Fubar, and class FubarTest:

public class Fubar { public Fubar( int x, int y ) { System.out.println( "Fubar ctor #1" ); }

public Fubar() { this( 10, 20 ); System.out.println( "Fubar ctor #2" ); }

public String toString() { System.out.println( "Fubar.toString" ); return "Fubar.toString"; } }

public class Foo extends Fubar { public Foo( int x, int y, int z ) { this( x, y ); System.out.println( "Foo ctor #1" ); }

public Foo( int x, int y ) { super(); System.out.println( "Foo ctor #2" ); }

public Foo() { this( 4, 2, 0 ); System.out.println( "Foo ctor #3" ); }

public String toString() { System.out.println( "Foo.toString" + " + "

  • super.toString() ); return "Foo.toString"; } }

public class FubarTest { public static void main( String[] args ) { Fubar ref = new Foo();

System.out.println( "-----" );

System.out.println( ref.toString() ); } }

17) What is the output when we run FubarTest as in

java FubarTest

Scratch Paper