0% found this document useful (0 votes)
12 views

C Final - Fa13

This exam tests students on their knowledge of Java programming concepts. It contains multiple choice and short answer questions testing topics like operators, conditionals, loops, methods, and recursion. Students must complete the exam without outside assistance or electronic devices.

Uploaded by

Akhil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

C Final - Fa13

This exam tests students on their knowledge of Java programming concepts. It contains multiple choice and short answer questions testing topics like operators, conditionals, loops, methods, and recursion. Students must complete the exam without outside assistance or electronic devices.

Uploaded by

Akhil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Signature _____________________ 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

Page 1 ___________ (13 points)

Page 2 ___________ (29 points)

Page 3 ___________ (29 points)

Page 4 ___________ (20 points)

Page 5 ___________ (8 points)

Page 6 ___________ (12 points)

Page 7 ___________ (17 points)

Page 8 ___________ (35 points)

Page 9 ___________ (11 points)

Page 10 ___________ (11 points)

Page 11 ___________ (23 points)

Total ___________ (208 points = 198 base points + 10 points EC)


(100%) [>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) 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 ); ____________

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; a =
boolean bool1 = !(b > 6) && (a >= 3) && (a <= 4) || (b > 6);
b =
if ( a++ >= 4 && --b >= 2 )
c = ++a + b--; c =
else
c = a++ + --b; bool1 =

int x = 7, y = 3, z; x =
boolean bool2 = !((x > 4) && (y <= 6)) == ((y <= 4) || (x > 6));
y =
if ( x++ >= 4 || --y >= 3 )
z = --x + y++; z =
else
z = x-- + ++y; bool2 =

1
5) What gets printed?
public class Question5
{
public static void main( String[] args )
{
final int MAX = 7, MIN = 2; 24
int i = 2, j = 5; 21
31
while ( i < MAX )
42
{
while ( j >= MIN ) 53
{ 64
--j; 61
System.out.println( i + " " + j ); 76
j -= 2;
}
j = i;
i++;
}

System.out.println( i + " " + j );


}
}

6) What gets printed by this code?


public class Number6
{
public static void main( String[] args )
{
int[] array = { 35, 95, 125, 105, 125, 105, 65, 35, 95, 65 };
int a = 42, b = 0;
for ( int i = 0; i < array.length; ++i )
{
int foo = array[i];
if ( foo > a ) // line that changes in questions below
{
a = foo;
b = i;
}
}
System.out.println(a + ", " + b); // separate the value of a and b with a comma and space
}
}

125, 2
Put your answer here: __________
What is printed if the line if ( foo > a ) was changed to if ( foo <= a )? 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

What is printed if the line if ( foo > a ) was changed to if ( foo == a )? __________
42, 0

What is printed if the line if ( foo > a ) was changed to if ( foo != a )? __________
65, 9

7) In the statement
A) Center point of oval and x diameter and y diameter
g.drawOval( 30, 30, 50, 50 );
B) Upper left corner and lower right corner of bounding box
the arguments represent ______ C) Center point of oval and width and height of bounding box
(write the letter representing your D) Upper left corner and width and height of bounding box
answer in the blank above.) E) Center point of oval and x radius and y radius
2
8) What is the output of the following program?
public class Tricky
{
public static void main( String[] args )
{
System.out.println( "I" ); I
message1( "Java" );
System.out.println( "II" );
Java-0
message2( "Finals" ); II
System.out.println( "III" ); Finals-1
message3( "Fall" ); Finals-2-0
}
Finals-3
public static void message1( String s ) III
{ Fall-4
System.out.println( s + "-0" );
} Fall-5-1
Fall-5-2-0
public static void message2( String s ) Fall-5-3
{
System.out.println( s + "-1" );
Fall-6
message1( s + "-2" );
System.out.println( s + "-3" );
}

public static void message3( String s )


{
System.out.println( s + "-4" );
message2( s + "-5" );
System.out.println( s + "-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
{ Output
public static void main( String[] args )
{
System.out.println( mystery( 6 ) ); // Print returned value 1: 1
}
2: 4
public static int mystery( int n ) 3: 9
{ 4: 16
int result; 5: 25
if ( n > 1 ) // A 6: 36
{
result = 2 * n - 1 + mystery( n - 1 );
System.out.println( n + ": " + result );
}
else // B
{
result = 1;
System.out.println( n + ": " + result );
}

return result;
}
}

10) What gets printed?


int a = 3;
int b = 5;
int c = 7;

87 = 312
System.out.println( a + b + (c + " = ") + a + (b + c) ); ________________________________

3
11) Given the following class definitions and hierarchy:
Snow
class Snow
{
public void method2() method2
{
System.out.println("Snow 2"); method3
method3();
}

public void method3()


{
System.out.println("Snow 3"); Rain Sleet
}
}

class Rain extends Snow


{
method1 method2
public void method1() method2 method3
{
method3(); (method3)
System.out.println("Rain 1");
}

public void method2()


{ Fog
method3();
System.out.println("Rain 2");
super.method2();
} method1
}
(method2)
class Sleet extends Snow
{ method3
public void method2()
{
method3();
System.out.println("Sleet 2");
super.method2();
What is the output given the following code: Put your answer here:
}
Snow ref1; Fog1
public void method3()
{
ref1 = new Fog(); -----
System.out.println("Sleet 3");
((Fog) ref1).method1();
}
System.out.println( "-----" );
Snow 2
}
ref1.method2();
class Fog extends Sleet
System.out.println( "-----" ); Snow 3
ref1.method3();
{
public void method1() -----
{
System.out.println("Fog 1"); Snow 3
}

public void method3() Put your answer here:


{
System.out.println("Fog 3"); Snow 3
super.method3();
} Rain 1
}
-----
Snow 2
What is the output given the following code:
Snow ref1; Snow 3

ref1 = new Rain(); -----


((Rain) ref1).method1(); Snow 3
System.out.println( "-----" );
ref1.method2();
System.out.println( "-----" );
ref1.method3();

4
12)
What gets printed by the following code? _______
16 13
What gets printed by the following code? _______

int x = 13; int x = 13;


if ( x > 7 ) if ( x < 7 )
{ {
x += 3; // Same as x = x + 3; x += 3; // Same as x = x + 3;
} }
else else if ( x <= 10 )
{ {
x += 6; x += 6;
} }
System.out.println( x ); System.out.println( x );

What gets printed by the following code? _______


19 15
What gets printed by the following code? _______

int x = 13; int x = 13;


if ( x < 7 ) if ( x > 7 )
{ {
x += 3; // Same as x = x + 3; x += 2; // Same as x = x + 2;
} }
else else if ( x >= 10 )
{ {
x += 6; x += 6;
} }
System.out.println( x ); System.out.println( x );

What gets printed by the following code? _______


20 What gets printed by the following code? _______
17

int x = 13; int x = 13;


if ( x > 7 ) if ( x < 7 )
{ {
x += 3; // Same as x = x + 3; x += 3; // Same as x = x + 3;
} }

if ( x >= 15 ) if ( x >= 10 )
{ {
x += 4; x += 4;
} }
System.out.println( x ); System.out.println( x );

16
What gets printed by the following code? _______ What gets printed by the following code? _______
13

int x = 13; int x = 13;


if ( x > 7 ) if ( x < 7 )
{ {
x += 3; // Same as x = x + 3; x += 3; // Same as x = x + 3;
} }

if ( x <= 12 ) if ( x >= 15 )
{ {
x += 4; x += 4;
} }
System.out.println( x ); System.out.println( x );

5
public interface Printable
{
13) Given the following definitions: public abstract String print( boolean duplex );
}

class Thing1 implements Printable class Thing2 implements Printable


{ {
private String str; private String str;

public Thing1() public Thing2()


{ {
this.str = "Thing 1"; this.str = "Thing 2";
} }

public String print( boolean duplex ) public String print( boolean duplex )
{ {
return this.str + " duplex = " + duplex; return this.str + " duplex = " + duplex;
} }

public String print() public String print( String user )


{ {
// print single sided by default System.out.print( user + ": " );
return this.print( false );
} // print double sided by default
} return this.print( true );
}
And the following variable definitions: }

Thing1 thing1 = new Thing1(); Hint: What does the compiler know about
Thing2 thing2 = new Thing2(); any reference variable at compile time (vs.
Printable printable; run time)?

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() ); Thing1 duplex = false


______________________________________________

System.out.println( thing1.print( true ) ); Thing 1 duplex = true


______________________________________________

System.out.println( thing1.print( "CS11FZZ" ) ); Error


______________________________________________

System.out.println( thing2.print() ); Error


______________________________________________

System.out.println( thing2.print( true ) ); Thing 2 duplex = true


______________________________________________

System.out.println( thing2.print( "CS11FZZ" ) ); CS11FZZ: Thing 2 duplex = true


______________________________________________

printable = thing1;
Not sure about the following:
System.out.println( printable.print() ); Error
______________________________________________

System.out.println( printable.print( true ) ); null This duplex = true


______________________________________________

System.out.println( printable.print( "CS11FZZ" ) ); Error


______________________________________________

printable = new Thing2();

System.out.println( printable.print() ); Error


______________________________________________

System.out.println( printable.print( false ) ); null This duplex = false


______________________________________________

System.out.println( printable.print( "CS11FZZ" ) ); Error


______________________________________________

6
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 )
A) this.forward( size );
{ B) this.turn( 90 ); // right
_____
B C) this.forward( -size );
D) 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 ) ____________________________________________


x < 42 && y = 37

What gets printed if the value of the actual What gets printed if the value of the actual
argument passed to this method is 5? ________
7 5
argument passed to this method is 2? ________

public void m6( int x ) public void m6( int x )


{ {
int y = 0; int y = 0;

if ( x <= 1 ) if ( x <= 1 )
y = 3; y = 3;
else if ( x <= 2 ) if ( x <= 2 )
y = 5; y = 5;
else if ( x == 3 || x >= 4 ) if ( x == 3 || x >= 4 )
y = 7; y = 7;
else else
y = 9; y = 9;

System.out.println( y ); System.out.println( y );
} }

7
Use the numbers below to identify various program parts.
16) Consider the following program?
A) local variable F) static variable
1 public class Test16 B) instance variable G) formal parameter
2 {
3 private int a; C) static method H) constructor
4 private int b; D) class definition (type) I) instance method
5 private static int c = 5; E) actual argument
6 public static void main( String[] args ) F c on line 5
7 {
_____
H Test16() on line 11 _____
8 Test16 ref = new Test16( 2 ); E 2 on line 8
_____ _____
G a on line 11
9 ref.method1( ref.b );
10 } _____
C main() on line 6 A? x on line 17
_____

11 public Test16( int a ) _____


D Test16 on line 1 _____
B a on line 3
12 {
13 this.a = a; I
_____ method1() on line 15 A c on line 17
_____
14 }

15 public void method1( int x ) Where in the Java Runtime environment does each of the
16 {
17 int c = x;
following live?
18 int b; stack heap
c on line 39 ___________ a on line 3 ____________
19 b = a;
20 a = c; a on line 11 ___________
stack c on line 5 ____________
heap

21 System.out.println( "this.a = " + this.a );


22 System.out.println( "this.b = " + this.b );
23 System.out.println( "Test16.c = " + Test16.c );
24 System.out.println( "c = " + c ); I doubt these are
25 System.out.println( "b = " + b ); Output
correct tbh
26 System.out.println( "a = " + a ); 2
this.a = ________
27 System.out.println( "result = " + method2( a ) );
28 System.out.println( "this.a = " + this.a ); this.b = ________
2
29 System.out.println( "this.b = " + this.b );
30 System.out.println( "Test16.c = " + Test16.c ); Test16.c = ________
5
31 System.out.println( "x = " + x ); c = ________
32 System.out.println( "a = " + a );
33 System.out.println( "b = " + b ); b = ________
2
34 System.out.println( "c = " + c );
35 } a = ________
2
7
this.a = ________
36 private int method2( int x )
37 { 2
this.b = ________
38 int b = x;
5
Test16.c = ________
39 int c = this.b + Test16.c;
x = ________
7
40 x = a = b + c;
7
a = ________
41 System.out.println( "this.a = " + this.a );
2
b = ________
42 System.out.println( "this.b = " + this.b );
43 System.out.println( "Test16.c = " + Test16.c ); 5
c = ________
44 System.out.println( "x = " + x );
45 System.out.println( "a = " + a ); result = ________
12
46 System.out.println( "b = " + b );
14
this.a = ________
47 System.out.println( "c = " + c );
this.b = ________
2
48 Test16.c = c + 2;
49 this.a = a + c; Test16.c = ________
7

50 return x + 5; x = ________
51 } a = ________
14
52 }
b = ________
2
c = ________
7

8
Given the following class definitions for class Foo, class Fubar, and class FubarTest:
public class Foo public class FubarTest
{ {
public Foo() public static void main( String[] args )
{ {
this( 42, 420 ); Foo ref = new Fubar( 42, 420 );
System.out.println( "Foo ctor #1" );
} System.out.println( "+++++" );

public Foo( int x, int y ) System.out.println( ref.toString() );


{ }
System.out.println( "Foo ctor #2" ); }
}

public String toString() 17) What is the output when we run FubarTest as in
{ java FubarTest
System.out.println( "Foo" );
return "Foo.toString";
} Foo ctor #2
} Foo ctor #1
Fubar ctor #3
Fubar ctor #1
Fubar ctor #2
public class Fubar extends Foo +++++
{ Fubar
public Fubar( int x, int y, int z ) Foo
{ Fubar + Foo.toString
this();
System.out.println( "Fubar ctor #1" );
}

public Fubar( int x, int y )


{
this( x, y, 4200 );
System.out.println( "Fubar ctor #2" );
}

public Fubar()
{
System.out.println( "Fubar ctor #3" );
}

public String toString()


{
System.out.println( "Fubar" );
String s = "Fubar" + " + " +
super.toString();
return s;
}
}

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
signature was being used in the subclass as was defined in the superclass? ____________________________
this?

9
18) Given the definition of class Swap below, indicate the output of each println statement?
(Hint: Draw stack frames)
public class Swap public class SwapTest
{ {
private int a; public static void main( String[] args )
{
public int getA() int a = 42; Swap ref1;
{ int b = 64; Swap ref2;
return a;
} ref1 = new Swap(7);
ref2 = new Swap(2);
public void setA(int a)
{ ref2 = ref1.swap(ref2, a);
this.a = a;
} 42
System.out.println(ref1.getA()); _________

public Swap(int a) 2
System.out.println(ref2.getA()); _________
{
this.a = a;
} ref1 = new Swap(7);
ref2 = new Swap(2);
public int swap(int a)
{ ref1.setA(ref1.swap(ref2.getA()));
this.a = a;
2
System.out.println(ref1.getA()); _________
return a;
} 2
System.out.println(ref2.getA()); _________

public void swap(int a, int b)


{ ref1 = new Swap(7);
int tmp; ref2 = new Swap(2);

tmp = a; Swap.swap(ref1, ref2);


a = b;
b = tmp; 7
System.out.println(ref1.getA()); _________
}
2
System.out.println(ref2.getA()); _________
public void swap(Swap ref)
{
Swap tmp; ref1 = new Swap(7);
ref2 = new Swap(2);
tmp = ref;
ref.a = this.a; ref1.swap(a, b);
this.a = tmp.a;
} System.out.println(a); 7
_________

public Swap swap(Swap ref, int a) System.out.println(b); 2


_________
{
this.a = a;
ref1 = new Swap(7);
return ref; ref2 = new Swap(2);
}
ref1.swap(ref2);
public static void swap(Swap ref1, Swap ref2)
{ 2
System.out.println(ref1.getA()); _________
Swap tmp;
7
System.out.println(ref2.getA()); _________
tmp = ref1; }
ref1 = ref2; }
ref2 = tmp;
}
}

The different swap() method definitions have the same


name but differ in their formal parameters. This is an
example of method _______________________ .

10
0
19) What is the default initial value of a local variable that is defined as an int? ____________
false
What is the default initial value of an instance variable that is defined as a boolean? ____________
What is the default initial value of an instance variable that is defined as an object reference? ____________
null
0.0
What is the default initial value of an instance variable that is defined as a double? ____________

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;
What results would be produced by evaluating the following expressions?
p1 == p2 false
____________ p1 == p3 false
____________ p2 == p3 true
____________

true
p1.equals(p2) ____________ true
p1.equals(p3) ____________ true
p2.equals(p3) ____________

p3.translate(1, 1); // Add 1 to the x and y coordinates in the Point object ref'ed by p3
false
p1.equals(p2) ____________ false
p1.equals(p3) ____________ true
p2.equals(p3) ____________

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)

Is this a compile time or a run time error? ________________________


run time
"123b5"
What is the value of the string we were trying to convert to an int? _____________________
What method in what class in what file and line number in your code did this occur?
FooBar
Method _______________________________
Class _______________________________
Foo2
File Foo2
________________________________
28
Line # ________________________________
A) overriding
B) overwriting
Regarding the Snow, Rain, Sleet, Fog program on page 4 (#11), C) overloading
D) inheriting
class Rain is ______
D method3() from class Snow
E) finalizing
class Rain is ______
A method2() from class Snow 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.
no.
1)

2)
11
Scratch Paper

12
Scratch Paper

13

You might also like