CSE11 Final 2013
CSE11 Final 2013
Name ________________________
cs11f ____
Student ID ____________________
By filling in the above and signing my name, I confirm I will complete this exam with the
utmost integrity and in accordance with the Policy on Integrity of Scholarship.
CSE 11
Final
Fall 2013
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.
!
*
+
<
==
&&
||
=
1) What is stored in the memory location allocated for the variable x for the following: _______
int x = -99;
A) x
B) the value -99
C) int
D) a reference (address in memory) to an object which has the value -99 stored
2) What is printed by the following code?
int foo = 42;
int bar = 42;
boolean foobar = ( foo == bar );
System.out.println( foobar );
foo = 37;
System.out.println( foobar );
System.out.println( foo == bar );
_____true_______
_______true_____
____false________
3) What is stored in the memory location allocated for the variable x for the following: _______
String x = "-99";
A) x
B) the value "-99"
C) String
D) a reference (address in memory) to an object which has the characters "-99" stored
4) What are the values of the indicated variables after the following code segments are executed? Remember
short-circuit evaluation with && and ||.
int a = 7, b = 3, c;
boolean bool1 = !(b > 6) && (a >= 3) && (a <= 4) || (b > 6);
if ( a++ >= 4 && --b >= 2 )
c = ++a + b--;
else
c = a++ + --b;
int x = 7, y = 3, z;
boolean bool2 = !((x > 4) && (y <= 6)) == ((y <= 4) || (x > 6));
if ( x++ >= 4 || --y >= 3 )
z = --x + y++;
else
z = x-- + ++y;
a = 9
b = 1
c = 11
bool1 = false
x = 7
y = 4
z = 10
bool2 = false
24
21
31
42
53
64
61
76
}
System.out.println(a + ", " + b);
}
}
35, 7 _
What is printed if the line if ( foo > a ) was changed to if ( foo >= a )?
125, 4_____
What is printed if the line if ( foo > a ) was changed to if ( foo < a )?
35, 0____
42, 0_____
What is printed if the line if ( foo > a ) was changed to if ( foo != a )? 65, 9______
7) In the statement
g.drawOval( 30, 30, 50, 50 );
A)
B)
C)
D)
E)
I
Java-0
II
Finals-1
Finals-2-0
Finals-3
III
Fall-4
Fall-5-1
Fall-5-2-0
Fall-5-3
Fall-6
9) Which part of the method mystery() below is the base case (part labeled A or B)? _B____
Which part of the method mystery() below is the recursive case (part labeled A or B)? _A___
What is printed when this program is run? Drawing stack frames for each method call will probably help.
public class Test9
{
public static void main( String[] args )
{
System.out.println( mystery( 6 ) ); // Print returned value
}
public static int mystery( int n )
{
int result;
if ( n > 1 )
// A
{
result = 2 * n - 1 + mystery( n - 1 );
System.out.println( n + ": " + result );
}
else
// B
{
result = 1;
System.out.println( n + ": " + result );
}
Output
1: 1
2: 4
3: 9
4: 16
5: 25
6: 36
36
return result;
}
}
Snow
method2
method3
Rain
Sleet
method1
method2
(method3)
Fog
method1
(method2)
method3
}
class Sleet extends Snow
{
public void method2()
{
method3();
System.out.println("Sleet 2");
super.method2();
}
public void method3()
{
System.out.println("Sleet 3");
}
}
class Fog extends Sleet
{
public void method1()
{
System.out.println("Fog 1");
}
method2
method3
12)
What gets printed by the following code? _16____
int x = 13;
if ( x > 7 )
{
x += 3; // Same as x = x + 3;
}
else
{
x += 6;
}
System.out.println( x );
int x = 13;
if ( x < 7 )
{
x += 3; // Same as x = x + 3;
}
else if ( x <= 10 )
{
x += 6;
}
System.out.println( x );
int x = 13;
if ( x < 7 )
{
x += 3; // Same as x = x + 3;
}
else
{
x += 6;
}
System.out.println( x );
int x = 13;
if ( x > 7 )
{
x += 2; // Same as x = x + 2;
}
else if ( x >= 10 )
{
x += 6;
}
System.out.println( x );
int x = 13;
if ( x > 7 )
{
x += 3; // Same as x = x + 3;
}
int x = 13;
if ( x < 7 )
{
x += 3; // Same as x = x + 3;
}
if ( x >= 15 )
{
x += 4;
}
System.out.println( x );
if ( x >= 10 )
{
x += 4;
}
System.out.println( x );
int x = 13;
if ( x > 7 )
{
x += 3; // Same as x = x + 3;
}
int x = 13;
if ( x < 7 )
{
x += 3; // Same as x = x + 3;
}
if ( x <= 12 )
{
x += 4;
}
System.out.println( x );
if ( x >= 15 )
{
x += 4;
}
System.out.println( x );
public Thing1()
{
this.str = "Thing 1";
}
public Thing2()
{
this.str = "Thing 2";
}
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" and assume that line is commented out when run.
System.out.println( thing1.print() );
___Error_______________________________
System.out.println( thing2.print() );
____Error________________________________
printable = thing1;
System.out.println( printable.print() );
_______Error________________________________
___________Error_____________________________
______________Error_________________________
_____________Error__________________________
14) Using only the statements below, select the order of the statements to draw an E such that the width of the E
is size pixels and the height of the E is twice size pixels. Do not worry about where it is drawing. Assume
the turtle is pointing up when the method is called, the pen is down, and it is positioned at the upper left corner
of where we want to draw the E. Start drawing the E at the upper left corner of the E. Have the turtle end at the
bottom right corner of the E.
Write the letter corresponding to each statement in the correct order to draw an E. Do it in exactly 12
statements.
public void drawE( int size )
{
____B_
A)
B)
C)
D)
this.forward( size );
this.turn( 90 ); // right
this.forward( -size );
this.turn( -90 ); // left
____A_
____C_
____B_
____A_
____D_
____A_
____C_
___B__
____A_
____D_
___A__
}
15) What is the equivalent Java expression for the following expression such that no ! operators are used?
!( x > 42 && y != 37 )
if (
y
else
y
else
y
else
y
x <= 1 )
= 3;
if ( x <= 2 )
= 5;
if ( x == 3 || x >= 4 )
= 7;
if (
y
if (
y
if (
y
else
y
= 9;
System.out.println( y );
}
x
=
x
=
x
=
<= 1 )
3;
<= 2 )
5;
== 3 || x >= 4 )
7;
= 9;
System.out.println( y );
}
A) local variable
B) instance variable
C) static method
D) class definition (type)
E) actual argument
6
7
8
9
10
ref.method1( ref.b );
11
12
13
14
15
16
17
18
19
20
b = a;
a = c;
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
System.out.println(
System.out.println(
System.out.println(
System.out.println(
System.out.println(
System.out.println(
System.out.println(
System.out.println(
System.out.println(
System.out.println(
System.out.println(
System.out.println(
System.out.println(
System.out.println(
}
36
37
38
39
"this.a = " +
"this.b = " +
"Test16.c = "
"c = " + c );
"b = " + b );
"a = " + a );
"result = " +
"this.a = " +
"this.b = " +
"Test16.c = "
"x = " + x );
"a = " + a );
"b = " + b );
"c = " + c );
F) static variable
G) formal parameter
H) constructor
I) instance method
__F___ c on line 5
___E__ 2 on line 8
__G_ a on line 11
__A_ x on line 17
_B___ a on line 3
____A_ c on line 17
on line 39 ____stack _
on line 3 __heap_____
on line 11 ___stack___
this.a );
this.b );
+ Test16.c );
Output
this.a = __0______
method2( a ) );
this.a );
this.b );
+ Test16.c );
this.b = ____0____
Test16.c = ____5____
c = __0______
b = ___2_____
a = ___0_____
this.a = ____5____
this.b = ____0____
Test16.c = ___5_____
x = ____5____
40
x = a = b + c;
41
42
43
44
45
46
47
System.out.println(
System.out.println(
System.out.println(
System.out.println(
System.out.println(
System.out.println(
System.out.println(
48
49
Test16.c = c + 2;
this.a = a + c;
50
51
52
return x + 5;
a = ____5____
"this.a = " + this.a );
"this.b = " + this.b );
"Test16.c = " + Test16.c );
"x = " + x );
"a = " + a );
"b = " + b );
"c = " + c );
b = _____0___
c = ____5____
result = __10_____
this.a = ___10____
this.b = _____0___
}
}
Test16.c = ___7_____
x = ___0_____
a = ___10____
b = ____2____
c = _____0___
Given the following class definitions for class Foo, class Fubar, and class FubarTest:
public class Foo
{
public Foo()
{
this( 42, 420 );
System.out.println( "Foo ctor #1" );
}
public Foo( int x, int y )
{
System.out.println( "Foo ctor #2" );
}
public String toString()
{
System.out.println( "Foo" );
return "Foo.toString";
}
}
Given the initial order of ints in an array as: 4, 7, 10, 9, 1, 2, 6 what is the order of the elements after 3
iterations of the selection sort algorithm? Recall the selection sort algorithm finds the index of the smallest
value in the unsorted partition and exchanges (swaps) that value with the value at the index of the first element
of the unsorted partition, then increments the index of the unsorted partition.
____
____ ____ ____ ____ ____ ____
1,2,4,9,10,7,6
What Java annotation did we use for methods like equals() and toString() in subclasses to ensure the same
@Override
signature was being used in the subclass as was defined in the superclass? ____________________________
9
18) Given the definition of class Swap below, indicate the output of each println statement?
(Hint: Draw stack frames)
public class Swap
{
private int a;
public int getA()
{
return a;
}
public Swap(int a)
{
this.a = a;
}
ref1.setA(ref1.swap(ref2.getA()));
System.out.println(ref1.getA()); ____2_____
return a;
System.out.println(ref2.getA()); ___2______
}
public void swap(int a, int b)
{
int tmp;
tmp = a;
a = b;
b = tmp;
System.out.println(ref1.getA()); ______7___
}
System.out.println(ref2.getA()); ______2___
public void swap(Swap ref)
{
Swap tmp;
tmp = ref;
ref.a = this.a;
this.a = tmp.a;
ref1.swap(a, b);
System.out.println(a);
_______7__
System.out.println(b);
_____2____
return ref;
}
ref1.swap(ref2);
public static void swap(Swap ref1, Swap ref2)
{
Swap tmp;
tmp = ref1;
ref1 = ref2;
ref2 = tmp;
System.out.println(ref1.getA()); ___2______
System.out.println(ref2.getA()); __2_______
}
}
}
}
10
19) What is the default initial value of a local variable that is defined as an int? _____undefined_______
What is the default initial value of an instance variable that is defined as a boolean? ______false______
What is the default initial value of an instance variable that is defined as an object reference? null________
What is the default initial value of an instance variable that is defined as a double? _____0.0_______
Assume a program had the following definitions (a Point has an x and a y value):
Point p1 = new Point( 420, 42 );
Point p2 = new Point( p1 );
Point p3 = p2;
____false________
p1.equals(p2) __true__________
p3.translate(1, 1);
p1 == p3
false______
p1.equals(p3) __true____
p2 == p3
__true____
p2.equals(p3) __true____
p1.equals(p2) ___false_________
p1.equals(p3) __false__________
p2.equals(p3) __true________
You type java Foo2 at the command line and you get the following:
Exception in thread "main" java.lang.NumberFormatException: For input string: "123b5"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:63)
at java.lang.Integer.parseInt(Integer.java:490)
at java.lang.Integer.parseInt(Integer.java:531)
at FooBar.foo2(BarNone.java:69)
at Foo2.main(Foo2.java:28)
__________Foo2_____________________
File
______________Foo2.java__________________
Line # ___________28_____________________
Regarding the Snow, Rain, Sleet, Fog program on page 4 (#11),
class Rain is __D____ method3() from class Snow
class Rain is ___A___ method2() from class Snow
A) overriding
B) overwriting
C) overloading
D) inheriting
E) finalizing
G) abstracting
H) all of the above
I) none of the above
Regarding class Snow on page 4 (#11), specify the two things the
Java compiler will automatically insert into the resulting Snow.class bytecode file. Be specific. Write code.
1) Snow Extends Object
2) public Snow (){
super();
}
11
Scratch Paper
12
Scratch Paper
13