Preview

01 - 2D Arrays

 1. A one-dimensional array can be thought to have a ___________________.

  single row of elements

  two-dimensional table with only one element

  a single row and at least two columns of elements

  row which contains a single row of elements

 2. A two-dimensional (2D) array has rows and columns. A 2D array in Java is actually ______________________.

  a linked list

  an array containing a binary tree

  a single array looping through ordered elements

  an array of arrays.

 3. Arrays in Java can store many items of ____________.

  the same type

  several different data types

  linked variables that are of varying data types

  data but only if the data is not object orientated.

 4. The following 2D array (image) could be stored in a one dimensional array. Some languages store data in row-major order or column-major order. What example is shown below?
0  1   2   3  4  5
===================
2  8   4  10  6  12

Note:
Typically, on the AP exam assume that any 
2 dimensional (2D) array is in row-major order.
2darrays_java_resized1.png

  The example shown is not valid as the rows and columns are random

  row-major order

  column-major order

  The example shows the data stored in 3D format

 5. In Java, each element of the outer array has a reference to each inner array. In the following example the array indices start at _______________________.
advanced_java_2darrays_pic2.png

  0 and end at the length + 1

  1 and end at the length + 1

  1 and end at the length

  0 and end at the length - 1

 6. In the following example, list all the values in the column at index 1.
8  -2   3  -1

4   5   0  -7

2  -3  -4  -5

  Answer: 4,5,0,-7

  Answer: -2 only

  Answer: -2,5,-3

  Answer: 8,4,2

 7. In the following example, what value is held at row index 2 and column index 1?
4  -2   -1   0

9   5    0   -7

7  -3   -4   -5

  Answer: 0

  Answer: 9

  Answer: -3

  Answer: 5

 8. In the above example, the value at row index 1 and column index 1 is 5.

  FALSE

  TRUE

 9. The code below creates a 2D array with 2 rows and 3 columns named trackerInfo. How many elements are in trackerInfo?
trackerInfo = new int [2][3];
orderChart = new String [3][2];

  2+3 = 5

  2x3 = 6

  (2+1)+(3+1) = 7

  (2x3)x2 = 12

 10. Analyse the following code carefully. What is the output?
public class Tracker
{
   public static void main(String[] args)
   {
      // here we are declaring the arrays
      int[][] trackerInfo = new int[5][3];
      System.out.println(trackerInfo.length-1 + " rows");
      System.out.println(trackerInfo[0].length + " columns");
   }
}

  4 rows, 3 columns

  4 rows, 2 columns

  5 rows, 3 columns

  5 rows, 2 columns

 11. When arrays are created their contents are automatically initialized to 0 for numeric types, null for object references, and _______ for type boolean.

  FALSE

  TRUE

 12. nums[2][1] = 5; would set the value for the 3rd row and 2nd column of a 2D array called nums.

  TRUE

  FALSE

 13. What is the value at creationInfo[2][1] after the code below executes?
int[][] trackerInfo = { {25,20,25}, {25,20,25} };
String[][] creationInfo = 
{ {"Adam", "Eve"}, {"Cain", "Abel"}, {"Seth", "Enoch"} };

  Cain

  Seth

  Adam

  Enoch

 14. What is the value of name after the code below executes?
int[][] trackerInfo = { {25,20,25}, {25,20,25} };
String[][] creationInfo = 
{ {"Adam", "Eve"}, {"Cain", "Abel"}, {"Seth", "Enoch"} };
int value = trackerInfo[1][0];
String name = creationInfo[0][1];

 15. Given the following, what is the value of things[2].length ?
double[][] things =
  { {1.2, 9.0},
    {9.2, 0.5, 0.0},
    {7.3, 7.9, 1.2, 3.9} } ;

  4

  3

  9

  2

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

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

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

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

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

 17. Given the following, which of the following statements constructs an array with 5 rows of 7 columns each and assign its reference to stuff ?
long[][] stuff ;

  stuff = new stuff[5][7] ;

   stuff = long[7][5] ;

  stuff = new long[5][7] ;

  stuff = long[5][7] ;

 18. Examine the following. What is in values[3][0] ?
double[][] values =
  { {1.2, 9.0, 3.2},
    {9.2, 0.5, 1.5, -1.2},
    {7.3, 7.9, 4.8} } ;

  There is no such array element

  7.9

  9.2

  7.3

 19. The square brackets [row][col] are used to access and modify an element in a 2D array.

  FALSE

  TRUE

 20. 2D array objects that are not rectangular are called ragged arrays. (Note these are generally outside the scope of the AP exam)

  TRUE

  FALSE