A data structure that organizes data into a grid of rows and columns.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Flip
Revise later
SpaceTo flip
If confident
All Flashcards
What is a 2D Array?
A data structure that organizes data into a grid of rows and columns.
What is double-index notation?
The notation used to access elements in a 2D array, specifying the row and column index (e.g., array[row][col]).
What is an ArrayIndexOutOfBoundsException?
An exception thrown when trying to access an array element with an index that is outside the allowed range.
What is the outer array in a 2D array?
The array that contains other arrays as its elements, forming the rows of the 2D array.
What is the inner array in a 2D array?
The arrays contained within the outer array, representing the columns of each row in the 2D array.
What is a rectangular 2D array?
A 2D array where all inner arrays (rows) have the same length (number of columns).
What is the meaning of '0-indexed language'?
A programming language where array indices start at 0, not 1.
What does arrayName.length represent for a 2D array?
The number of rows (the length of the outer array) in the 2D array.
What does arrayName[0].length represent for a rectangular 2D array?
The number of columns (the length of the first inner array) in the 2D array.
What is meant by traversing a 2D array?
Iterating through all the elements of the 2D array, typically using nested loops to access each row and column.
What does the following code output?
int[][] arr = {{1, 2}, {3, 4}};
System.out.println(arr[0][1]);
2
What does the following code output?
int[][] arr = new int[2][3];
System.out.println(arr[1][2]);
0
Identify the error in the following code:
int[][] arr = {{1, 2}, {3, 4}};
System.out.println(arr[2][0]);
ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2 (attempting to access a row that doesn't exist).
What does the following code output?
int[][] arr = {{1, 2, 3}, {4, 5, 6}};
System.out.println(arr.length);
2
What does the following code output?
int[][] arr = {{1, 2, 3}, {4, 5, 6}};
System.out.println(arr[0].length);
3
What does the following code output?
int[][] arr = new int[3][2];
int count = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
arr[i][j] = count++;
}
}
System.out.println(arr[2][1]);
5
Identify the error in the following code:
int[][] arr = new int[2][2];
for (int i = 0; i <= arr.length; i++) {
for (int j = 0; j <= arr[i].length; j++) {
arr[i][j] = 0;
}
}
ArrayIndexOutOfBoundsException: The loops iterate one element too far in both dimensions (i and j should be less than arr.length and arr[i].length, respectively).
What does the following code output?
int[][] arr = {{10, 20}, {30, 40}};
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i][0];
}
System.out.println(sum);
40
What does the following code output?
int[][] arr = {{1, 2}, {3, 4}};
int product = 1;
for (int j = 0; j < arr[0].length; j++) {
product *= arr[0][j];
}
System.out.println(product);
2
What is the output of the following code?
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
System.out.println(matrix[1][1]);
5
How are 2D arrays stored in memory?
As a nested array, where the outer array contains references to inner arrays (rows).
What is the significance of nested loops when working with 2D arrays?
They are essential for iterating through each element in the 2D array, with the outer loop handling rows and the inner loop handling columns.
How do you declare and initialize an empty 2D array of integers with 3 rows and 4 columns in Java?
int[][] arrayA = new int[3][4];
How do you initialize a 2D array with pre-existing values?
Using curly braces to define the rows and their elements, e.g., int[][] arrayB = {{1, 2}, {3, 4}};
Explain the relationship between rows, columns, and indices in a 2D array.
Rows are the horizontal lines, columns are the vertical lines, and indices are used to access specific elements at the intersection of a row and column (starting from 0).
How do you prevent ArrayIndexOutOfBoundsException when accessing 2D array elements?
Ensure that the row and column indices are within the valid range (0 to array.length - 1 for rows, 0 to array[0].length - 1 for columns).
How do you access the element in the second row and third column of a 2D array named 'matrix'?
matrix[1][2]
What is the difference between arrayName.length and arrayName[0].length?
arrayName.length gives the number of rows, while arrayName[0].length gives the number of columns (assuming it's a rectangular array).
Why are 2D arrays useful?
They are useful for organizing data in a grid-like structure, which is suitable for representing matrices, game boards, and image data.
How does the graphical representation of a 2D array help in understanding?
It provides a visual representation of the array as a table or grid, making it easier to understand the arrangement of elements and their indices.