revised: 09/23/99

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. What are the three general types of looping structures?

a. counting loop,  sentinel-controlled loop,  and result-controlled loop.
b. infinite loop, counting loop, nested loop
c. while loop, for loop, do loop
d. count up loop, count down loop, infinite loop

2. What is the output of the following program fragment?

for ( int j = 0;  j <  5; j++ )
{
  System.out.print( j + " " );
}
System.out.println( );

a. 0 1 2 3 4 5
b. 0 1 2 3 4
c. 0 1 2 3 4 5
d. j j j j j

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

for ( int j = 10;  j >  5; j-- )
{
  System.out.print( j + " " );
}
System.out.println( );

a. 10 11 12 13 14 15
b. 9 8 7 6 5 4 3 2 1 0
c. 10 9 8 7 6 5
d. 10 9 8 7 6

4. What must the test be so that the following fragment prints out the integers 5 through and including 15?

for ( int j = 5;  ________ ; j++ )
{
  System.out.print( j + " " );
}
System.out.println( );

a. j<15
b. j<=16
c. j<16
d. j==15

5. What must the change be so that the following fragment prints out the even integers 0 2 4 6 8 10?

for ( int j = 0; j <= 10; _______   )
  System.out.print( j + " " );
System.out.println( );

a. j+2
b. j = j+2
c. j++++
d. ++j++

6. What must the initialization be so that the following fragment prints out the integers -3 -2 -1 ?

for ( _______; j < 0; j++    )
  System.out.print( j + " " );
System.out.println( );

a. int j = 0
b. int j < 0
c. int j = -3
d. int j = -4

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

for ( int j = 5;  j > -5; j-- )
  System.out.print( j + " " );

System.out.println( );

a. -5 -4 -3 -2 -1 0
b. 5 4 3 2 1 0
c. 5 4 3 2 1 0 -1 -2 -3 -4 -5
d. 5 4 3 2 1 0 -1 -2 -3 -4

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

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

System.out.println( );

a. 0 1 2 3 4 5 6 7 8
b. Nothing --- the program will not compile.
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 < 9;   )
  System.out.print( count + " " );
  count++ ;
System.out.println( );

a. 0 1 2 3 4 5 6 7 8
b. Nothing --- the program will not compile.
c. count count count count count count count count count
d. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

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

int j;
for ( j = 0;  j < 5;   )
{
  System.out.print( j + " " );
  j++ ;
}
  
System.out.println( );

a. 0 1 2 3 4
b. Nothing --- the program will not compile.
c. 1 2 3 4 5
d. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

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

Click here (If you have just 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 on reload to clear the old answers.)