Module 08: 2D Array Flashcards

1
Q

2D Arrays

How can you store arrays in arrays?

A

The additional set of brackets note that arrays will be stored in this array

int[][] gradeBook = {exam1, exam2…};

int[][] gradeBook = new int[2][5];

gradeBook[0] = exam1;

gradeBook[1] = exam2;

  • 2: number of arrays in the array
  • 5: number of values in each array
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

2D Arrays

What are 2D Arrays?

A

Arrays that store arrays

int[][] gradeBook = {{90, 87, 30},

{49, 99, 22},

{90, 90, 23}};

When accessing elements in a 2D array, we are accessing the row and then column:

gradebook[row][column];

  • Row - find array
  • Column - find value in array
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

2D Arrays

What is row-major order?

A

The process of traversing a 2D array by accessing all elements in a row before moving on to the next row

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

2D Arrays

What is column-major order?

A

The process of traversing a 2D array by accessing all values at the first column in every row, before moving to the next column

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

2D Arrays

What is the proper syntax to initialize an empty 6x6 2D String array?

  1. String[] array = new String[6][6];
  2. int[][] array = new int[6][6];
  3. String[][] array = new String[6];
  4. String[][] array = new String[6][6];
A
  1. String[][] array = new String[6][6];
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

2D Arrays

int[][] grid = new int[5][3]

What is the value of grid[0].length?

0

5

3

15

A

3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

2D Arrays

A 2D array is initialized:

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

What is the result of array[0][1] + array[2][0]?

10

15

9

5

A

9

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

8.2 Traversing 2D Arrays

What are the two methods to traverse a 2D array?

A

“Row-Major Order” = Traversing an array across each row

“Column-Major Order” = Traversing an array down each row

Choice depends on the task

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

8.2 Traversing 2D Arrays

How would you use a row-major order to “print all the test scores for exam1?”

A

for(int index = 0; index < gradeBook[0].length; index++)

{

System.out.println(gradeBook[0][index]);

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

8.2 Traversing 2D Arrays

How would you use a column-major traversal to “print all the test scores for student 2?”

A

for(int index = 0; index < gradeBook.length; index++)

{

System.out.println(gradeBook[index][1]);

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

8.2 Traversing 2D Arrays

How do you use a nested loop to traverse a 2D array?

A

for(int row = 0; row < gradeBook.length; row++)

{

for(int column = 0; column < gradeBook[row].length; column++)

{

System.out.print(gradeBook[row][column]);

}

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

8.2 Traversing 2D Arrays

How do you use column-major to traverse an array (using a nested loop)?

A

for(int row = 0; row < gradeBook.length; row++)

{

for(int column = 0; column < gradeBook[row].length; column++)

{

System.out.print(gradeBook[column][row]);

}

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

8.2 Traversing 2D Arrays

How do you use an enhanced for loops to traverse a 2D array?

A

for(int[] row: gradeBook)

{

for(int col: row)

{

System.out.printl(col);

}

}

  • Outer loop needs to be an array data type
  • Inner loop needs to be the data type of the array
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

8.2 Traversing 2D Arrays

How do you use Linear Search in 2D arrays?

A

When applying Linear Search on 2D Arrays, each row must be accessed, then Linear Search can be applied to each row:

public String search(int[][], array2D, int key)

{

for(int rowIndex = 0; rowIndex < array2D.length; rowIndex++)

{

for(int colIndex = 0; colIndex < array2D[rowIndex].length; colIndex++)

{

if(array2D[rowIndex][colIndex] == key)

{

return key + “ is located at: “ + rowIndex + “,” + colIndex;

}

}

}

return key + “is not in this 2D array”;

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

8.2 Traversing 2D Arrays

How do you use common 2D algorithms to determine the min/max or compute the sum, average, or mode?

A

int[][] scores = {{2,3,4,5,6},{7,6,5,4,3}}

//Initialize value here: for(int[] row: scores)

{

for(int i = 0; i < row.lenght; i++)

//Preform calculation here

}

//Report results

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

8.2 Traversing 2D Arrays

How do you use common 2D array algorithms for:

  • Determine if at least one element has a particular property
  • Determine if all elements have a particular property
  • Determine the number of elements meeting specific criteria
A

int[][] scores = {{2,3,4,5,6},{7,6,5,4,3}}

//Initialize tracher variable

for(int[] row: scores)

{

for(int i = 0; i < row.length; i++)

{

//Conditionally check properties

}

}

//Report results

17
Q

8.2 Traversing 2D Arrays

How do you use common 2D array algorithms for:

  • Shift or rotate elements left or right
  • Reverse the order of the elements
A

int[][] scores = {{2,3,4,5,6},{7,6,5,4,3}}

int[][] alteredScores;

for(int[] row : scores)

{

for(int i = 0; i < row.length; i++)

//add values from grade to alteredArray

}

//Report results

18
Q

8.2 Traversing 2D Arrays

A 2D double array is declared and initialized to track the terrain of a city park. Each value in the 2D array represents the height of a particular latitude and longitude above sea level. Longitude is represented by the columns in the 2D array and latitude is represented by each row in the 2D array.

The creator of this 2D array would like to look at the height of the city park along the longitude 100.0 - which traversal method would make the most sense in order to do so?

  1. Row-Major Order
  2. Enhanced for loop
  3. Column-Major Order
  4. This isn’t possible given the 2D array.
A
  1. Column-Major Order
19
Q

8.2 Traversing 2D Arrays

A 2D double array terrainMap is declared and initialized to track the terrain of a city park. Each value in the 2D array represents the height of a particular latitude and longitude above sea level. Longitude is represented by the columns in the 2D array and latitude is represented by each row in the 2D array.

Which of the following would be a correct way to write the traversal for this 2D array?

(1)

for(double[] row: terrainMap)

{

for(double[] num: row)

{

System.out.print(num + “ “);

}

System.out.println();

}

(2)

for(double[] row: terrainMap)

{

for(double num: row)

{

System.out.print(num + “ “);

}

System.out.println();

}

A

(2)

for(double[] row: terrainMap)

{

for(double num: row)

{

System.out.print(num + “ “);

}

System.out.println();

}

20
Q

2D Array Quiz

Question: 1

What would be a correct way to instantiate this 2D array?

1 0 10 0

3 8 38 0

A

int [][] table = {

{1, 0, 10, 0},

{3, 8, 38, 0}

};

21
Q

2D Array Quiz

Question: 2

Which is the correct way to construct and assign a 2D array, with 8 rows and 10 columns, to the variable popcorn?

  1. int[8][10] popcorn;
  2. int[8][10] popcorn = new int[8][10];
  3. int[][] popcorn = new int[8][10];
  4. int[8][10] popcorn = new int[][];
  5. int[][] popcorn = new int[];
A

3. int[][] popcorn = new int[8][10];

22
Q

2D Array Quiz

Question: 3

We want to create a 2D double array with 6 rows and 7 columns and assign it to connectFour. Which of these is correct?

  1. double[][] connectFour = new double[6][7];
  2. double[][] connectFour = new connectFour[6][7];
  3. double[][] connectFour = double[6][7];
  4. new double[6][7] connectFour;
  5. double[6][7] connectFour = new double[][];
A

1. double[][] connectFour = new double[6][7];

23
Q

2D Array Quiz

Question: 4

Given the following:

double[][] something =

{ {2.5, 6.8, 8.3, 2.3, 0.0},

{6.1, 10.2, 1.3, -2.5, -9.9},

{1.1, 2.3, 5.8, 13.21, 34.55} };

What is the value of something[1][2]?

  1. 6.8
  2. 6.1
  3. 2.3
  4. 1.3
  5. The array does not have an element at that location.
A

4. 1.3

24
Q

2D Array Quiz

Question: 5

Given the following:

double[][] something =

{ {2.5, 6.8, 8.3, 2.3, 0.0},

{6.1, 10.2, 1.3, -2.5, -9.9},

{1.1, 2.3, 5.8, 13.21, 34.55} };

What is the value of something[2][1]?

  1. 6.8
  2. 6.1
  3. 2.3
  4. 1.3
  5. The array does not have an element at that location.
A

2. 2.3

25
Q

2D Array Quiz

Question: 6

double[][] something =

{ {2.5, 6.8, 8.3, 2.3, 0.0},

{6.1, 10.2, 1.3, -2.5, -9.9},

{1.1, 2.3, 5.8, 13.21, 34.55} };

What is the value of something.length?

5

4

3

12

8

A

3

26
Q

2D Array Quiz

Question: 7

double[][] something =

{ {2.5, 6.8, 8.3, 2.3, 0.0},

{6.1, 10.2, 1.3, -2.5, -9.9},

{1.1, 2.3, 5.8, 13.21, 34.55} };

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

5

4

3

12

8

A

5

27
Q

2D Array Quiz

Question: 8

double[][] something =

{ {2.5, 6.8, 8.3, 2.3, 0.0},

{6.1, 10.2, 1.3, -2.5, -9.9},

{1.1, 2.3, 5.8, 13.21, 34.55} };

How do you replace the value 13.21 with 8.8?

  1. something[2][3] = 8.8;
  2. something[2][4] = 8.8;
  3. something[13.21] = 8.8;
  4. something[3][4] = 8.8;
  5. something[3][3] = 8.8;
A
  1. something[2][3] = 8.8;
28
Q

2D Array Quiz

Question: 9

double[][] something =

{ {2.5, 6.8, 8.3, 2.3, 0.0},

{6.1, 10.2, 1.3, -2.5, -9.9},

{1.1, 2.3, 5.8, 13.21, 34.55} };

We want to replace the row {6.1, 10.2, 1.3, -2.5, -9.9} with a complete new array that looks like {3.1, 4.1, 5.9, 2.6, 8.4}.

  1. double[] temp = {3.1, 4.1, 5.9, 2.6, 8.4};
    something[2] = temp;
  2. something[1] = {3.1, 4.1, 5.9, 2.6, 8.4};
  3. something[2] = new {3.1, 4.1, 5.9, 2.6, 8.4};
  4. double[] temp = {3.1, 4.1, 5.9, 2.6, 8.4};
    something[1] = temp;
  5. something[1][0] = 3.1;
    something[1][1] = 4.1;
    something[1][2] = 5.9;
    something[1][3] = 2.6;
    something[1][4] = 8.4;
A

4. double[] temp = {3.1, 4.1, 5.9, 2.6, 8.4};
something[1] = temp;

Not 5: This will produce the correct results but is not replacing the row with a new array, it just replaces the values.

29
Q

2D Array Quiz

Question: 11

Consider the following code segment, which is intended to create and initialize the 2D array words where the length of each word corresponds to the product of the indices of the row and the column it resides in.

String[][] words = /*missing code */;

Which of the following initializer lists could replace /*missing code*/ so that the code segment works as intended?

  1. {{“”, “a”, “as”}, {“”, “b”, “be”}, {“”, “d”, “don”}}
  2. {{“a”, “as”, “ask”}, {“b”, “be”, “bet”}, {“d”, “do”, “don”}}
  3. {{“”, “”, “”}, {“”, “b”, “be”}, {“”, “do”, “dont”}}
  4. {{“a”, “a”, “a”}, {“”, “b”, “be”}, {“d”, “do”, “dont”}}
  5. {{“”, “”, “”}, {“”, “b”, “be”}, {“”, “d”, “do”}}
A

3. {{“”, “”, “”}, {“”, “b”, “be”}, {“”, “do”, “dont”}}

30
Q

2D Array Quiz

Question: 12

Consider the following code segment, which is intended to display the word boards.

String[][] twoLetters = {{“ab”, “ac”, “ad”,”ar”,”af”},

{“bo”, “be”, “bi”, “ba”, “bu”},

{“ca”, “ce”, “ck”, “co”, “cs”},

{“da”, “ds”, “do”, “de”, “di”}};

System.out.println(/*Missing Code*/);

Which of the following can replace /*Missing Code*/ so the code segment works as intended?

  1. twoLetters[1][0] + twoLetters[0][3]+ twoLetters[3][1]
  2. twoLetters[0] + twoLetters[3]+ twoLetters[1]
  3. twoLetters[1][1] + twoLetters[0][4]+ twoLetters[3][2]
  4. twoLetters[0][1] + twoLetters[3][0]+ twoLetters[1][3]
  5. twoLetters[1][0] + twoLetters[0][3]+ twoLetters[twoLetters.length][1]
A

1. twoLetters[1][0] + twoLetters[0][3]+ twoLetters[3][1]

31
Q

2D Array Quiz

Question: 13

Consider the following code segment:

How many instances of the number 5 will be stored in this 2D array?

4

5

3

1

0

A

4

32
Q

2D Array Quiz

Question: 14

Consider the following code segment:

What will the result of the print statement be when this code segment is executed?

1

3

4

2

This will result in an IndexOutofBoundsException.

A

4

33
Q

2D Array Quiz

Question: 15

How many times will the boolean expression printed from this code segment be true?

A

3

34
Q

2D Array Quiz

Question: 16

Consider the following method, sumTo10, that traverses a 2D array checking to see if the sum of all integers in each array are equal to 10. The method will return true if all arrays within the 2D array sum to 10, and false otherwise.

For example, the 2D array {{2,8}, {4, 6}} would return true because the content of each array sums to 10. The method, however, doesn’t always work as intended. Which of the following 2D array input values does sumTo10 not work for?

  1. {{5, 5}, {3,7}}
  2. {{5, 2, 3}, {3, 1, 6}, {1, 1, 8}}
  3. {{-100, 0, 110}, {3, 2, 5}, {2, 1, 7}}
  4. {{9, 1, 0}, {4, 1, 5}}
  5. {{10}}
A
  1. {{9, 1, 0}, {4, 1, 5}}
35
Q

2D Array Quiz

Question: 17

Which of the following traversals would print all of the elements in the 2D array of integers nums in column-major order?

A

for(int row = 0; row < nums[0].length; row++)

{

for(int col = 0; col < nums.length; col++)

{

System.out.println(nums[col][row]);

}

}

36
Q

2D Array Quiz

Question: 19

What will be displayed as a result of executing this code segment?

A

101

37
Q

2D Array Quiz

Question: 20

What will the value of each element in mystery be after the execution of the code segment?

A

{{1, 0, 0}

{2, 2, 2}

{1, 0, 3}}