created: 09/23/99

Further Quiz on for Loops

This is a practice quiz. The results are not recorded anywhere and do not affect your grade. The questions on this quiz might not appear in any quiz or test that does count toward your grade.

Instructions: For each question, choose the single best answer. Make your choice by clicking on its button. You can change your answers at any time. When the quiz is graded, the correct answers will appear in the box after each question.


1. Pick the for loop which duplicates this while loop:
 
int x = 0;
while ( x < 500 )
{
  System.out.println( x );
  x = x + 5;
}
a.
 
for ( int x = 5; x <= 500; x+=5 )
  System.out.println( x );

b.
 
for ( int x = 0; x < 500; x+=5 )
  System.out.println( x );

c.
 
for ( float x = 0.0; x < 500.0; x += 5.0 )
  System.out.println( x );

d.
 
for ( int x = 500; x >= 0; x-=5 )
  System.out.println( x );


2. Pick the for loop which duplicates this while loop:
 
double x = 0.0;
while ( x < 100.0 )
{
  System.out.println( x );
  x += 0.1;
}
a.
 
for ( int x = 0; x <= 1000; x += 1 )
  System.out.println( x );

b.
 
int x;
for ( x = 0.0; x < 100.0; x += 0.1 )
  System.out.println( x );

c.
 
double x;
for ( x = 0.0; x < 100.0; x += 0.1 )
  System.out.println( x );

d.
 
for ( double x = 0.0; x < 100.0;  )
{
  x += 0.1 ;
  System.out.println( x );
}


3. Pick the for loop which duplicates this while loop:
 
int sum =  0 ;
int j   = -5 ;
while ( sum <= 350 )
{
  sum += j ;
  j   += 5 ;
}
a.
 
int sum =  0 ;
int j   = -5 ;

for ( ; sum <= 350; j += 5  )
{
  sum += j ;
}

b.
 
for ( int sum = 0; sum <= 350; j += 5  )
{
  sum += j ;
}

c.
 
for ( int j = 0; sum <= 350; j += 5  )
  sum += 5 ;

d.
 
int sum =  0 ;

for ( ; sum <= 350; )
{
  sum += j ;
  j   += 5 ; 
}


4. What must the test be so that the following fragment prints out the integers -5 through and including 5?
for ( int j = -5;  ________ ; j++ )
{
  System.out.print( j + " " );
}
System.out.println( );
a. j<5 b. j<=5 c. j>5 d. j==5

5. Fill the blank so that the following fragment prints out 0.2, 0.4, 0.6, 0.8, 1.0,
for ( int j = 2; j <= 10; j++   )
  System.out.print( __________ + ", " );

System.out.println( );
a. j/10 b. j%10 c. (j+1.0)/10 d. j/10.0

6. Fill the blank e so that the following two loops are equivalent:

int sum   =  0;
for ( _______; count <= 25; count++    )    
  sum += count;
int sum   =  0;
int count = 10;
while ( count <= 25 )
{
  sum += count;
  count++;
}

a. int count = 0 b. int count = 10 c. count = 0 d. int j = 10

7. What does the following print on the monitor?
for ( int j = 0;  j < 5; j++ )
{
  for ( int k = 0;  k < 10 ; k++ )
    System.out.print( "*" );

  System.out.println( );
}
a.
**********
**********
**********
**********
**********
b.
**********
**********
**********
**********
**********
**********
**********
**********
**********
**********
c.
*****
*****
*****
*****
*****
*****
*****
*****
*****
*****
d.
*
*
*
*
*


8. What is the output of the following code fragment?
for ( int count=0;  count <= 9; ++count )
  System.out.print( count + " " );

System.out.println( );
a. 0 1 2 3 4 5 6 7 8
b. 0 1 2 3 4 5 6 7 8 9
c. 0 1 2 3 4 5 6 7
d. 1 2 3 4 5 6 7 8 9

9. What is the output of the following code fragment?

for ( int count = 0;  count <= 20;  count+=2 )
  System.out.print( count + " " );

System.out.println( );
a. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
b. 0 2 4 6 8 10
c. 0 2 4 6 8 10 12 14 16 18
d. 0 2 4 6 8 10 12 14 16 18 20

10. Fill in the blank so that the following adds up the odd numbers from 1 to 99
int sum = 0;
for ( int num = 1; num <=99; __________ )
  sum += num;
  
System.out.println( sum );
a. j++ b. num+2 c. num+=2 d. num--

The number you got right:       Percent Correct:       Letter Grade:   

(If you have just returned here from another page, or re-loaded this page, you will need to click again on each of your choices for the grading program to work.)


Click here to go back to the main menu.