Docsity

Java Programming Quiz: Picture Looping and Relational Operators, Quizzes of Computer Science

Toggle sidebar
bg1
Signature _____________________
CSE 8A
Name ________________________
Quiz 2
cs8w ____
Winter 2011
Student ID ____________________
This quiz is to be taken by yourself with closed books, closed notes, no electronic devices.
Fill in the blanks in the following code to correctly loop through all the pixels in "this" Picture object.
public void someMethodInClassPicture()
{
// Create an array of all the pixels in this Picture
Pixel[] pixelArray = this.______________________________();
int i = __________; // Initialize index variable
while ( i < _____________________________________________ ) // Loop thru all pixels
{
Pixel pixel = _______________________ [ ______ ]; // Access the pixel at each index
// Do something with this pixel
i_____; // Change index variable to access the next pixel
}
}
The following is a while loop. Write the equivalent as a for loop (not a foreach loop). Be sure to put the
comment
// Some code here
in the correct location in relation to the other parts of the for loop you write.
int i = 42;
while ( i >= 17 )
{
// Some code here
i--;
}
Fill in the blanks below so the code correctly gets the width of a Picture object referenced by a variable named
bar
and stores it in the variable named
foo
. Be sure to fill in the correct type of variable
foo
.
Picture bar = new Picture();
_________ foo; // Fill in the type variable foo should be defined as.
foo = ___________________________ ; // Fill in the right side of this assignment stmt
// to get the width of the Picture object bar
// references.
(More on other side โ€“ turn over)
pf2

Partial preview of the text

Download Java Programming Quiz: Picture Looping and Relational Operators and more Quizzes Computer Science in PDF only on Docsity!

Signature _____________________ CSE 8A Name ________________________

Quiz 2

cs8w ____ Winter 2011 Student ID ____________________

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

Fill in the blanks in the following code to correctly loop through all the pixels in "this" Picture object.

public void someMethodInClassPicture() { // Create an array of all the pixels in this Picture

Pixel[] pixelArray = this.______________________________();

int i = __________; // Initialize index variable

while ( i < _____________________________________________ ) // Loop thru all pixels { Pixel pixel = _______________________ [ ______ ]; // Access the pixel at each index

// Do something with this pixel

i_____; // Change index variable to access the next pixel } }

The following is a while loop. Write the equivalent as a for loop (not a foreach loop). Be sure to put the

comment // Some code here in the correct location in relation to the other parts of the for loop you write.

int i = 42;

while ( i >= 17 ) { // Some code here i--; }

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

bar and stores it in the variable named foo. Be sure to fill in the correct type of variable foo.

Picture bar = new Picture();

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

foo = ___________________________ ; // Fill in the right side of this assignment stmt // to get the width of the Picture object bar // references.

(More on other side โ€“ turn over)

Trace the following program and specify its output.

public class Trace { public static void main( String[] args ) { System.out.println( "main#1" ); foo1(); System.out.println( "main#2 ); foo2(); System.out.println( "main#3" ); }

public static void foo2() { foo3(); System.out.println( "foo#2" ); }

public static void foo1() { System.out.println( "foo#1" ); foo2(); }

public static void foo3() { System.out.println( "foo#3" ); } }

List the six relational operators in Java that evaluate to a boolean value. Not English words; Java operators.

__________ __________ __________

__________ __________ __________

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

What about this code? ______

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

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

for ( int i = 0; i < width; i++ ) { for ( int j = height โ€“ 1; j >= 0; j-- ) { p = getPixel( i, j ); p.setColor( Color.BLACK ); } }