created: 09/23/99; revised: 10/04/03

# on Further 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 = 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 ; 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+=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:

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.