revised: 10/05/03


Quiz on Loops and Arrays

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 is the length of the following array: byte[] data = { 12, 34, 9, 0, -62, 88 };

A.    1
B.    5
C.    6
D.    12

2. What is the output of the following code fragment:

    int[] egArray = { 2, 4, 6, 8, 10, 1, 3, 5, 7, 9 };

    for ( int index= 0 ; index < 5 ; index++  )
      System.out.print(  egArray[ index ] + "  "  );

A.    2 4 6 8
B.    2 4 6 8 10
C.    2 4 6 8 10 1
D.    2 4 6 8 10 1 3 5 7 9

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

    int[] egArray = { 2, 4, 6, 8, 10, 1, 3, 5, 7, 9 };

    for ( int index= 0 ; index < egArray.length ; index++  )
      System.out.print(  egArray[ index ] + "  "  );

A.    2 4 6 8
B.    2 4 6 8 10
C.    2 4 6 8 10 1
D.    2 4 6 8 10 1 3 5 7 9

4. What is the output of the following code fragment:

    int[] egArray = { 2, 4, 6, 8, 10, 1, 3, 5, 7, 9 };

    for ( int index= 0 ; index < egArray.length ; index = index + 2  )
      System.out.print(  egArray[ index ] + "  "  );

A.    2 4 6 8 10 1 3 5 7 9
B.    4 8 1 5 9
C.    2 6 10 3 7
D.    2 6 10 3 7 0

5. Does a programmer always know how long an array will be when the program is being written?

A.    Yes---the program will not compile without the length being declared.
B.    No---the array object is created when the program is running, and the length might change from run to run.
C.    Yes---otherwise the program will not run correctly.
D.    No---arrays can grow to whatever length is needed.

6. Fill in the blanks of the following code fragment so that the elements of the array are printed in reverse order, starting with the last element.

    int[] egArray = { 2, 4, 6, 8, 10, 1, 3, 5, 7, 9 };

    for ( int index= ________ ; _____________ ; ______________ )
      System.out.print(  egArray[ index ] + "  "  );

A.    index = 0; index < egArray.length; index--
B.    index = length; index < 0; index--
C.    index = length-1; index > 0; index--
D.    index = egArray.length-1; index >= 0; index--

7. Examine the following program fragment:


  int[] array = { 1, 4, 3, 6, 8, 2, 5};
  int what = array[0];

  // scan the array
  for ( int index=0; index < array.length; index++ )
  { 
    if ( array[ index ] > what )   
      what = array[ index ];  
  }
  System.out.println( what ); 
What does the fragment write to the monitor?

A.    1
B.    5
C.    1 4 3 6 8 2 5
D.    8

8. Examine the following program fragment:


  int[] array = { 1, 4, 3, 6, 8, 2, 5};
  int what = array[0];

  // scan the array
  for ( int index=0; index < array.length; index++ )
  { 
    if ( array[ index ] < what )   
      what = array[ index ];  
  }
  System.out.println( what ); 
What does the fragment write to the monitor?

A.    1
B.    5
C.    1 4 3 6 8 2 5
D.    8

9. Examine the following program fragment:


  int[] array = { 1, 4, 3, 6 };
  int what    = 0;

  // scan the array
  for ( int index=0; index < array.length; index++ )
  { 
      what = what + array[ index ] ;  
  }
  System.out.println( what ); 
What does the fragment write to the monitor?

A.    14
B.    1
C.    6
D.    1 4 3 6

10. Fill in the blank in the following code fragment so that each element of the array is assigned twice the value of its index.


  int[] array = new int[10];

  // scan the array
  for ( int index=0; index < array.length; index++ )
  { 
     _______________________ 
  }

A.    index = 2*index;
B.    array[ 2*index ] = 2*index;
C.    array[ index ] = 2*array[ index ];
D.    array[ index ] = 2*index;

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.