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

a.    1
b.    5
c.    6
d.    12

Correct Answer Is:


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

Correct Answer Is:


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

Correct Answer Is:


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

Correct Answer Is:


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.

Correct Answer Is:


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--

Correct Answer Is:


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

Correct Answer Is:


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

Correct Answer Is:


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

Correct Answer Is:


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;

Correct Answer Is:


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.)