How are 2D arrays stored in memory?
As a nested array structure, where the outer array contains references to inner arrays.
How do you access an element in a 2D array?
Using double-index notation: arrayName[row_index][column_index].
What are the two ways to initialize 2D arrays?
1. Initialize an empty 2D array. 2. Initialize a pre-existing 2D array.
Why is graphical representation only suitable for rectangular arrays?
Because it relies on the grid/table/matrix analogy, which requires consistent row lengths.
Explain the concept of rows and columns in a 2D array.
Rows are the horizontal arrays, and columns are the vertical arrays. The first dimension determines the number of rows, and the second determines the number of columns.
What happens if you try to access an element outside the bounds of a 2D array?
An ArrayIndexOutOfBoundsException is thrown.
What is the significance of the first dimension in a 2D array?
It defines the number of rows in the array.
What is the significance of the second dimension in a 2D array?
It defines the number of columns in the array (for rectangular arrays).
How does the concept of 'nested arrays' relate to 2D arrays?
A 2D array is essentially an array where each element is another array, hence 'nested'.
Why is it important to understand the memory representation of 2D arrays?
It helps in understanding how data is organized and accessed, especially when dealing with complex algorithms.
What does the following code output?
int[][] arr = {{1, 2}, {3, 4}};
System.out.println(arr[0][1]);
2
Identify the error in the following code:
int[][] arr = new int[2][3];
System.out.println(arr[2][1]);
ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2 (first dimension).
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
Identify the error in the following code:
int[][] arr = new int[2][];
arr[0] = {1, 2};
arr[1] = {3, 4, 5};
Missing 'new int[]' when assigning values to arr[0] and arr[1]. Should be: arr[0] = new int[] {1, 2}; arr[1] = new int[] {3, 4, 5};
What does the following code output?
int[][] arr = {{1}, {2, 3}, {4, 5, 6}};
System.out.println(arr[2][1]);
5
What does the following code output?
int[][] arr = new int[3][4];
System.out.println(arr[1][2]);
0
Identify the error in the following code:
int[][] arr = new int[3][4];
arr[1,2] = 5;
Incorrect syntax for accessing 2D array elements. Should be arr[1][2] = 5;
What does the following code output?
int[][] arr = {{1, 2}, {3, 4}};
System.out.println(arr[1][0]);
3
What does the following code output?
int[][] arr = {{1, 2}, {3, 4}};
System.out.println(arr[0][0]);
1
What is a 2D array?
An array of arrays, where each element is itself an array.
What is double-index notation?
A way to access elements in a 2D array using two indices: one for the row and one for the column.
What is an outer array in the context of 2D arrays?
The main array that contains other arrays as its elements.
What is an inner array in the context of 2D arrays?
One of the arrays contained within the outer array of a 2D array.
What is a rectangular 2D array?
A 2D array where all inner arrays have the same length.
What is an ArrayIndexOutOfBoundsException?
An exception thrown when trying to access an array element with an index that is outside the valid range.
What does '0-indexed language' mean?
It means that the first element in an array has an index of 0, not 1.
What is the meaning of arrayName.length for a 2D array?
It represents the number of rows in the 2D array.
What is the meaning of arrayName[0].length for a rectangular 2D array?
It represents the number of columns in the 2D array.
Define initialization of a 2D array.
Assigning an initial value to all the elements of the array when it is declared, or shortly after.