created: 09/23/99; revised: 10/04/03, 10/19/08


on Further for Loops

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 = 0; x < 500; x+=5 )
  System.out.println( x );
B.   
 
for ( float x = 0.0; x < 500.0; x += 5.0 )
  System.out.println( x );
C.   
 
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 ;
for ( j = 0; sum <= 350; j += 5  )
{
  sum += j ;
}
B.   
int sum = 0;
for ( int j = -5; 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 up to 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 values close to 0.2, 0.4, 0.6, 0.8, 1.0,

for ( int j = 2; j <= 10; j+=2   )
  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 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:   


Click here

If you have returned here from another page, or have re-loaded this page, you will need to click again on each of your choices for the grading program to work correctly. You may want to press the SHIFT KEY while clicking to clear the old answers.