Unit 8: 2D Array Flashcards

1
Q

What will int[][] grid = new int[3][4] create?

A

It will make a 3x4 grid of ints

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

How to get a row from your 2D int array?

A

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

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

How do you get an element within your 2D array?

A

int elem = grid[1][2];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
Consider the following code segment, which is intended to create and initialize the two-dimensional (2D) integer array num so that columns with an even index will contain only even integers and columns with an odd index will contain only odd integers.
int[][] num = / missing code /;
Which of the following initializer lists could replace / missing code / so that the code segment will work as intended?
A. {{0, 1, 2}, {4, 5, 6}, {8, 3, 6}}
B. {{1, 2, 3}, {3, 4, 5}, {5, 6, 7}}
C. {{1, 3, 5}, {2, 4, 6}, {3, 5, 7}}
D. {{2, 1, 4}, {5, 2, 3}, {2, 7, 6}}
E. {{2, 4, 6}, {1, 3, 5}, {6, 4, 2}}
A

A. {{0, 1, 2}, {4, 5, 6}, {8, 3, 6}}

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

Consider the following code segment, which is intended to display “cat”.
String[][] keyboard = {{“q”, “w”, “e”, “r”, “t”},
{“a”, “s”, “d”, “f”, “g”},
{“z”, “x”, “c”, “v”, “b”}};

System.out.println(/ missing expression /);
Which of the following can replace / missing expression / so that the code segment works as intended?

A) keyboard[12] + keyboard[5] + keyboard[4]
B) keyboard[13] + keyboard[6] + keyboard[5]
C) keyboard[2][2] + keyboard[1][0] + keyboard[0][4]
D) keyboard[2][2] + keyboard[0][1] + keyboard[4][0]
E) keyboard[3][3] + keyboard[2][1] + keyboard[1][5]

A

C) keyboard[2][2] + keyboard[1][0] + keyboard[0][4]

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

How do you declare a 2D array?

A

datatype[][] variableName = new datatype[numberRows][numberCols];

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

How can you traverse through arrays though a tested for loop?

A

A for loop inside of another for loop. These are used to loop through all
the elements in a 2d array. One loop can work through the rows and the other the
columns.

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

How can you get an out of bounds error when traversing through an array?

A

This happens when a loop goes beyond the last valid index in an array. Remember that the last valid row index is arr.length - 1. The last valid column
index is arr[0].length - 1.

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