2D Array Creation and Access - AP Computer Science A
- Which method accesses the element at row 2, column 1?
- array[2][1];. First index specifies row, second index specifies column.
- What is the index of the first row and first column in a 2D array?
- [0][0]. Arrays use zero-based indexing for both dimensions.
- What is an example of a rectangular 2D array?
- int[][] array = new int[3][3];. All rows have same length, forming regular matrix shape.
- How do you access the second column of the first row in a 2D array?
- array[0][1]. Second index (1) selects the second column position.
- Identify the method to get the number of rows in a 2D array.
- array.length. Length property returns the number of rows in the array.
- How do you define a 2D array with specific values?
- int[][] array = {{1, 2}, {3, 4}};. Initializer list syntax creates array with literal values.
- What is the purpose of nested loops in 2D arrays?
- To iterate through rows and columns. Outer loop handles rows, inner loop handles columns.
- How do you declare a 2D array to store Strings?
- String[][] array;. Declaration syntax for String object references in 2D array.
- How do you access the last element of the last row in a 2D array?
- array[array.length - 1][array[array.length - 1].length - 1]. Uses length properties to calculate final position indices.
- How do you initialize a 3x3 int 2D array in Java?
- int[][] array = new int[3][3];. Creates a 3x3 matrix with new keyword and size specification.
- What is the syntax to iterate over all elements in a 2D array?
- for (int i = 0; i < array.length; i++) for (int j = 0; j < array[i].length; j++). Nested loops traverse rows first, then columns within each row.
- What is the time complexity for accessing an element in a 2D array?
- O(1). Direct indexing provides constant time element access.
- How do you create a 2D array with 5 rows and variable columns?
- int[][] array = new int[5][];. Creates row structure without specifying column dimensions.
- What will array.length return for a 4x6 2D array?
- 4. Length property returns number of rows in the array.
- What is the default value of elements in a new int 2D array?
- 0. Java initializes numeric array elements to zero by default.
- How many elements are in a 2D array of size 4x5?
- 20 elements. Total elements equals rows multiplied by columns: .
- How do you check if a 2D array element is zero?
- if (array[i][j] == 0). Conditional statement compares element value to zero.
- How do you declare a 2D array reference of any type?
- Type[][] array;. Generic declaration syntax for any reference type.
- What is an example of initializing a jagged 2D array?
- int[][] array = { {1, 2}, {3, 4, 5} };. Different row lengths create irregular array structure.
- How do you update the element at row 1, column 2 to 10?
- array[1][2] = 10;. Assignment syntax uses array notation with row and column indices.
- Identify the method to get the number of columns in the first row.
- array[0].length. Accesses the length property of the first row array.
- How do you initialize a 2D array with specific String values?
- String[][] array = {{"a", "b"}, {"c", "d"}};. Initializer syntax with String literals in nested structure.
- What is a 2D array?
- A data structure storing elements in a grid with rows and columns. Elements arranged in matrix format with row and column indices.
- What is the default value of a boolean 2D array?
- false. Java initializes boolean array elements to false by default.
- What is the result of accessing out-of-bounds index in a 2D array?
- ArrayIndexOutOfBoundsException. Runtime exception thrown when index exceeds array bounds.
- What is the length of a 2D array row array[0]?
- array[0].length. Returns number of columns in the specified row.
- How do you declare a 2D array in Java?
- int[][] arrayName;. Declaration syntax uses double brackets for 2D array type.
- How do you declare a char 2D array?
- char[][] array;. Declaration syntax for character data type in 2D format.
- What is the syntax to assign 'A' to the first element of a 2D char array?
- array[0][0] = 'A';. Assignment uses array indexing with character literal value.
- What is a jagged array?
- A 2D array where rows have different lengths. Each row can have different number of columns.
- How do you check if a 2D array is empty?
- Check if array.length == 0 or array[0].length == 0. Check both row count and first row column count.
- Which keyword is used to create a new 2D array in Java?
- new. Required keyword for dynamic memory allocation in Java.
- How do you initialize a 2D array with 2 rows and 3 columns?
- int[][] array = new int[2][3];. Specifies exact dimensions during array creation.