created: 12/05/99; revised: 10/05/03, 10/31/2012


Quiz on 2D Arrays

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. Examine the following:

double[][] values =
  { {1.2, 9.0, 3.2},
    {9.2, 0.5, 1.5, -1.2},
    {7.3, 7.9, 4.8} } ;

what is in   values[2][1] ?

A.    7.3

B.    7.9

C.    9.2

D.    There is no such array element.


2. Examine the following:

double[][] values =
  { {1.2, 9.0, 3.2},
    {9.2, 0.5, 1.5, -1.2},
    {7.3, 7.9, 4.8} } ;

what is in   values[3][0] ?

A.    7.3

B.    7.9

C.    9.2

D.    There is no such array element.


3. You want to create a table that looks like:

12-98
714
-32-10

Which of the following will work?

A.   

double[][]  table =
  { 12, -9, 8, 
    7, 14,
    -32, -1, 0} ;

B.   

double[][] table =
  { {12, -9, 8}, 
    {7, 14, 0},
    -32, -1, 0} };

C.   

double[][] table =
  { {12, -9, 8}
    {7, 14}
    {-32, -1, 0} };

D.   

double[][] table =
  { {12, -9, 8},
    {7, 14},
    {-32, -1, 0} };


4. Given the following:

double[][] things =
  { {1.2, 9.0},
    {9.2, 0.5, 0.0},
    {7.3, 7.9, 1.2, 3.9} } ;

What is the value of things.length ?

A.    2

B.    3

C.    4

D.    9


5. Given the following:

double[][] things =
  { {1.2, 9.0},
    {9.2, 0.5, 0.0},
    {7.3, 7.9, 1.2, 3.9} } ;

What is the value of things[2].length ?

A.    2

B.    3

C.    4

D.    9


6. Given the following:

long[][] stuff ;

Which of the following statements constructs an array with 5 rows of 7 columns each and assign its reference to stuff ?

A.    stuff = new stuff[5][7] ;

B.    stuff = new long[5][7] ;

C.    stuff = long[5][7] ;

D.    stuff = long[7][5] ;


7. Given the following:

int[][] items =
  { {0, 1, 3, 4},
    {4, 3, 99, 0, 7 },
    {3, 2} } ;

Which of the following statements replaces the 99 with 77?

A.    items[1][2] = 77;

B.    items[2][1] = 77;

C.    items[ 99 ] = 77;

D.    items[2][3] = 77;


8. Which of the following constructs and assigns to array a 2D array with 7 rows, but does not yet construct the rows?

A.    int[][] array = new int[7][];

B.    int[][] array = new int[7];

C.    int[][] array = new int[][7];

D.    int[] array[7] = new int[];


9. Given:

int[][] items =
  { {0, 1, 3, 4},
    {4, 3, 99, 0, 7 },
    {3, 2} } ;

Which of the following fragments prints out every element of items?

A.   

for ( int row=0; row < items.length; row++ )
{
  System.out.println();
  for ( int col=0; col < items.length; col++ )
    System.out.print( items[row][col] + " ");
}

B.   

for ( int row=0; row < items.length; row++ )
{
  System.out.println();
  for ( int col=0; col < items[col].length; col++ )
    System.out.print( items[row][col] + " ");
}

C.   

for ( int row=0; row < items.length; row++ )
{
  System.out.println();
  for ( int col=0; col < items[row].length; col++ )
    System.out.print( items[row][col] + " ");
}

D.   

for ( int row=0; row < items.length; row++ )
{
  for ( int row=0; row < items[row].length; row++ )
    System.out.print( items[row][col] + " ");
  System.out.println();
}


10. Given:

int[][] items =
  { {0, 1, 3, 4},
    {4, 3, 99, 0, 7 },
    {3, 2} } ;

Which of the following fragments replaces row 0 of items with an entierly new row?

A.   

items[0][0] = 8; 
items[0][1] = 12; 
items[0][2] = 6; 

B.   

items[0] = { 8, 12, 6 };

C.   

items[0] = new { 8, 12, 6 };

D.   

int[] temp = { 8, 12, 6 };
items[0] = temp;


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.