Quiz Questions on Java Programming, Quizzes of Computer Science

A set of programming quiz questions for a cse 8a course, focusing on java programming concepts such as loops, conditional statements, and arrays. Students are required to determine the output of given code snippets and identify which loops will not cause an arrayindexoutofbounds exception.

Typology: Quizzes

2010/2011

Uploaded on 06/06/2011

koofers-user-gev
koofers-user-gev 🇺🇸

4

(1)

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Signature _____________________
CSE 8A
Name ________________________
Quiz 4
cs8w ____
Winter 2011
Student ID ____________________
This quiz is to be taken by yourself with closed books, closed notes, no electronic devices.
What will be output from the following code:
int x = 0;
int y = 5;
while ( x < 10 && y <= 7 )
{
x++;
y++;
System.out.println( x + " " + y );
}
System.out.println( x + " " + y );
What will be output from the following code:
int x = 2;
int y = 5;
while ( x < 3 || y <= 6 )
{
System.out.println( x + " " + y );
x++;
y++;
}
System.out.println( x + " " + y );
Write the equivalent statement using the other variable for each statement (A and B) below. In A) write the
equivalent statement using
noise
, in B) write the equivalent statement using
foo
. Assume the following
declarations:
Sound noise = new Sound( FileChooser.pickAFile() );
SoundSample[] foo = noise.getSamples();
A)
foo[17].setValue( 4444 );
B)
noise.getSampleValueAt( i );
pf2

Partial preview of the text

Download Quiz Questions on Java Programming and more Quizzes Computer Science in PDF only on Docsity!

Signature _____________________ CSE 8A Name ________________________

Quiz 4

cs8w ____ Winter 2011 Student ID ____________________

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

What will be output from the following code:

int x = 0; int y = 5;

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

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

What will be output from the following code:

int x = 2; int y = 5;

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

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

Write the equivalent statement using the other variable for each statement (A and B) below. In A) write the

equivalent statement using noise, in B) write the equivalent statement using foo. Assume the following

declarations:

Sound noise = new Sound( FileChooser.pickAFile() ); SoundSample[] foo = noise.getSamples();

A) foo[17].setValue( 4444 );

B) noise.getSampleValueAt( i );

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 = 16; i < this.getWidth(); i++ )

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

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

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

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

F) for ( int i = this.getWidth() – 1; i >= 0; i-- )

What gets printed by this code? Assume the calling object (this) is a Sound object and this code is in Sound.java

public void guess()

SoundSample[] noiseArray = this.getSamples();

int a = 60, b = 0;

for (int i = 0; i < noiseArray.length; i++)

SoundSample sample = noiseArray[i];

int foo = sample.getValue();

if (foo > a)

a = foo;

b = i;

System.out.println(a + "," + b);

Put your answer here: __________

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 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 is printed if the line if ( foo > a ) was changed to if ( foo != a )? __________