2D Array Traversals - AP Computer Science A
- Identify the method to get the number of columns in a 2D array.
- array[0].length. Accesses the length of the first row to get column count.
- What is the default value of elements in a newly created boolean 2D array?
- false. Boolean arrays initialize all elements to false by default.
- How do you initialize a 2D array with values 1 to 4 in Java?
- int[][] array = { {1, 2}, {3, 4} };. Direct initialization with literal values 1 through 4.
- State the keyword used to initialize a 2D array in Java.
- new. Required keyword to allocate memory for array objects.
- What is the type of elements in 'int[][] array = new int[2][3];'?
- int. Declaration specifies integer as the element type.
- What is the purpose of nested loops in 2D array traversal?
- To iterate over all elements. Enables systematic access to every array element.
- What is the typical structure of an inner loop for 2D arrays?
- for (int j = 0; j < array[i].length; j++). Iterates through columns in current row i.
- What is the result of 'array[2].length' if array has 3 rows, unequal columns?
- Length of third row. Returns column count specifically for row at index 2.
- What is the default value of elements in a newly created int 2D array?
- 0. Integer arrays initialize all elements to zero by default.
- What does 'array.length * array[0].length' compute for a 2D array?
- Total number of elements. Multiplies rows by columns for rectangular arrays.
- What does 'array[0][array[0].length - 1]' access in a 2D array?
- Last element in the first row. Uses length-1 to get the rightmost column index.
- What is initialized first in a nested loop for 2D array traversal?
- Row index. Outer loop variable controls row position first.
- Find and correct the error: 'int[][] arr; arr[1][2] = 3;'
- Correct: arr must be initialized before access. Array must be instantiated with 'new' before access.
- How do you assign a value to element at row 2, column 3 in array?
- array[1][2] = value;. Arrays use zero-based indexing, so row 2 is index 1.
- What is the initial value of 'array[i][j]' after 'int[][] array = new int[2][2];'?
- 0. New integer arrays initialize all elements to zero.
- Which keyword is used to create a new 2D array object in Java?
- new. Essential keyword for creating array objects in memory.
- Identify the method to iterate over a 2D array using enhanced for-loops.
- for (int[] row : array). Enhanced for-loop iterates over each row as an array.
- What is the term for accessing each element in a 2D array?
- Traversal. The systematic process of visiting each element in order.
- What is the result of accessing an out-of-bounds index in a 2D array?
- ArrayIndexOutOfBoundsException. Runtime exception thrown when accessing invalid indices.
- What is the typical structure of an outer loop for 2D arrays?
- for (int i = 0; i < array.length; i++). Iterates through each row using array.length.
- Which loop structure is commonly used for 2D array traversal?
- Nested for-loops. Outer loop for rows, inner loop for columns.
- Identify the expression to access the element at row i, column j.
- array[i][j]. Uses row index i and column index j.
- Identify the method to get the number of rows in a 2D array.
- array.length. Returns the number of rows in the outer array dimension.
- Identify the syntax error: 'int[][] array = new int()[3][3];'
- Correct: 'new int[3][3]'. Missing type specification between 'new' and brackets.
- Find and correct the error: 'int[][] arr = new int[2][2]; arr[2][0] = 5;'
- Correct: arr[1][0] = 5;. Index 2 is out of bounds for 2-row array.
- What is the syntax to declare a 2D array in Java?
- int[][] arrayName;. Standard declaration syntax using double brackets for 2D arrays.
- What does 'int[][] array = new int[3][];' initialize?
- 3 rows, undefined column sizes. Creates row array without specifying column dimensions.
- Identify the action: 'for (int[] row : array) { for (int elem : row) { sum += elem; } }'
- Sum all elements in 2D array. Enhanced for-loops accumulate all array element values.
- What operation is performed by 'array[i][j] += 1;' in a 2D array?
- Increment element at array[i][j] by 1. Adds 1 to the current value at that position.
- What is the syntax to declare a 2D array with specific values?
- int[][] array = { {1, 2}, {3, 4} };. Initialization syntax using nested braces for values.
- What is the result of 'array.length' in a 2D array?
- Number of rows. Returns the count of rows in the array.
- What is the function of 'array[0].length' in a 2D array?
- Number of columns in the first row. Returns column count for the first row only.
- Identify the error: 'int[][] array = { {1, 2}, {3, 4, 5} };'
- Array is jagged, not rectangular. Rows have different lengths, creating irregular structure.
- What exception is thrown by accessing an invalid 2D array index?
- ArrayIndexOutOfBoundsException. Standard runtime exception for bounds violations.