Java Exam: Fall 2010, CSE 8A, Exams of Computer Science

The final exam for the cse 8a course at the university of california, berkeley, given in fall 2010. It includes various java programming questions related to operators, memory allocation, loops, methods, and conditional statements.

Typology: Exams

2010/2011

Uploaded on 06/06/2011

koofers-user-gev
koofers-user-gev ๐Ÿ‡บ๐Ÿ‡ธ

4

(1)

10 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Student ID ____________________
Name ________________________
cs8f ____
Signature _____________________
CSE 8A
Final
Fall 2010
Page 1 ___________ (10 points)
Page 2 ___________ (9 points)
Page 3 ___________ (7 points)
Page 4 ___________ (12 points)
Page 5 ___________ (12 points)
Page 6 ___________ (11 points)
Page 7 ___________ (14 points)
Page 8 ___________ (14 points)
Page 9 ___________ (10 points)
Total ___________ (99 points = 94 base points + 5 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.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Java Exam: Fall 2010, CSE 8A and more Exams Computer Science in PDF only on Docsity!

Student ID ____________________ Name ________________________

cs8f ____ Signature _____________________

CSE 8A

Final

Fall 2010

Page 1 ___________ (10 points)

Page 2 ___________ (9 points)

Page 3 ___________ (7 points)

Page 4 ___________ (12 points)

Page 5 ___________ (12 points)

Page 6 ___________ (11 points)

Page 7 ___________ (14 points)

Page 8 ___________ (14 points)

Page 9 ___________ (10 points)

Total ___________ (99 points = 94 base points + 5 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

What gets printed?

public class F { public static void main( String[] args ) { System.out.println( -1 + 2 - 3 * 10 % 4 ); _________

System.out.println( 2 * 7 - 11 / 9 + 10 ); _________ } }

What gets printed?

public class F { public static void main( String[] args ) { int fire = 10, air = 2;

fire = fire * (air + 5); System.out.println( "fire = " + fire ); ________________________

fire = 5 โ€“ air--; System.out.println( "fire = " + fire + "; air = " + air );________________________

fire = 5; air = ++fire + 4; System.out.println( "fire = " + fire + "; air = " + air );________________________ } }

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 ); ____________

In the code for drawSquare( int size ) discussed in class, we used lines like

this.forward( size );

If drawSquare() is invoked as

koko.drawSquare( 42 );

what does this refer to? (Circle the correct answer.)

What does the following code do? Give a high level answer in plain English words. Assume this method is

stored in the file Picture.java

public void fubar() { Pixel[] pixelArray = this.getPixels();

for ( Pixel p : pixelArray ) { p.setRed( 255 ); p.setGreen( 255 ); p.setBlue( 255 ); } }

Which of the following correctly swaps two values a and b? Write the letter of the correct code here: ______

A B C

D E F

Which of the following are not valid Java identifiers? (Circle your answer(s).)

[+1 โ€“ correct; -1 โ€“ incorrect; No negative score]

this&that thisRthat This_2_That integer

nine2five n!ne_2_5 9_2_5 int

a = b tmp = a b = tmp;

tmp = a b = tmp; a = b

tmp = a b = a b = tmp;

tmp = a b = a a = tmp;

tmp = a a = b b = tmp;

tmp = b a = b b = tmp;

A) this shape

B) this square

C) the object referenced by koko

D) this size

Fill in the blanks below so the code correctly gets the height of a Picture object referenced by a variable named

var1 and stores it in the variable named var2. Be sure to fill in the correct type of variable var2.

Picture var1 = new Picture();

_________ var2; // Fill in the type variable var2 should be defined as.

var2 = _______________________ ; // Fill in the right side of this assignment stmt // to get the height of the Picture object var // references.

The following is a for loop. Write the equivalent as a while loop.

int i; for ( i = 0; i < 42; ++i ) { System.out.println( i * i );

}

Which of the following are instance methods? Circle your answers. (+1 if correct; -1 if incorrect. No neg. pts.)

pictureObj.show(); Math.abs( -3 ); turtle1.turnLeft();

FileChooser.pickAFile(); soundObj.play(); ColorChooser.pickAColor();

Assume there is an array properly defined and a variable named abc references this array.

Write the expression to access the first element in this array? __________________________________

Write the expression to access the last element in this array? __________________________________

What order are pixels changed given this code in Picture.java (assuming all variables are correctly defined and

set)? Write the letter of the correct order here: ______

int width = โ€ฆ ; int height = โ€ฆ ; Pixel p; for ( int j = 0; j < height; j++ ) { for ( int i = 0; i < width; i++ ) { p = getPixel( i, j ); p.setColor( Color.Black ); } }

A) L->R;Top->Down B) L->R;Bottom->Up

C) Bottom->Up;L->R D) Top->Down;L->R

The following method mirrors a Picture vertically left to right. this is a Picture that we are applying the

mirroring.

public void mirrorVertically() { int mirrorPoint = this.getWidth() / 2; Pixel leftPixel = null; Pixel rightPixel = null; Pixel mirrorPixel = null;

for ( int y = 0; y < this.getHeight(); ++y ) { for ( int x = 0; x < mirrorPoint; ++x ) { leftPixel = this.getPixel( x, y ); rightPixel = this.getPixel( this.getWidth() - 1 โ€“ x, y ); mirrorPixel = this.getPixel( mirrorPoint, y ); / MISSING LINE / } } }

Which missing line of code will correctly mirror this Picture vertically left to right? _______

A) mirrorPixel.setColor( leftPixel.getColor() );

B) mirrorPixel.setColor( rightPixel.getColor() );

C) rightPixel.setColor( leftPixel.getColor() );

D) leftPixel.setColor( rightPixel.getColor() );

E) rightPixel.setColor( mirrorPixel.getColor() );

F) leftPixel.setColor( mirrorPixel.getColor() );

Method overloading allows for more than one method with the same _________ as long as the _____________

are different.

What will be output from the following code:

int x = 4; int y = 2;

while ( x < 10 && y <= 3 ) { System.out.println( x + " " + y ); x++; y++; }

System.out.println( x + " " + y );

What gets printed by the following code? _______

int x = 12; if ( x > 7 ) { x += 3; // Same as x = x + 3; } if ( x >= 15 ) { x += 6; } System.out.println( x );

What gets printed by the following code? _______

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

What will be output from the following code:

int x = 2; int y = 0;

while ( x < 5 || y <= 1 ) { x++; y++; System.out.println( x + " " + y ); }

System.out.println( x + " " + y );

Which of the following loops will not cause an ArrayIndexOutOfBounds exception? Circle all the correct

answers. There may be more than one correct answer. [Correct +1; Incorrect: -1; No negative score]

A) for ( int i = 0; i <= this.getWidth(); i++ )

B) for ( int i = this.getWidth(); i > 0; i-- )

C) for ( int i = this.getWidth() โ€“ 1; i >= 0; i-- )

D) for ( int i = 8; i < this.getWidth(); i++ )

E) for ( int i = this.getWidth() โ€“ 8; i > 0; i-- )

F) for ( int i = -1; i < this.getWidth(); i++ )

Given the following code:

public void guess() { int array[] = { 20, 50, 30, 20, 100, 0, 40, 0 };

int a = 200; int b = 0;

for ( int i = 0; i < array.length; i++ ) { int foo = array[i];

if ( foo < a ) { a = foo; b = i; } } System.out.println( a + "," + b ); }

What gets printed? _______________

What is printed if the line if ( foo < a ) was changed to if ( foo > a )? _____________

What is printed if the line if ( foo < a ) was changed to if ( foo <= a )? _____________

What gets printed?

public class F { public static void main( String[] args ) { int x = 5; int y = 8;

System.out.println( false || (y >= 8) ); System.out.println( ++x < y ); System.out.println( true && !(x <= y) ); System.out.println( 4 + x < y ); } }

What is the default initial value of a local variable? _________________

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? ____________

What is the default initial value of an instance variable that is defined as a double? ____________

What gets printed if the value of the actual

argument passed to this method is 0? ________

public void f6( 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 f6( 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