Implementing 2D Array Algorithms - AP Computer Science A
- What is the syntax to initialize a 2D array with specific values in Java?
- int[][] array = {{1, 2, 3}, {4, 5, 6}};. Nested braces define rows and columns with literal values.
- How do you pass a 2D array to a method in Java?
- Use methodName(int[][] array). Parameter type specifies two-dimensional integer array reference.
- What is the syntax to copy a row of a 2D array?
- Use System.arraycopy or a loop. Built-in method or manual iteration copies row elements.
- What is the key difference between 2D arrays and arrays of arrays?
- Arrays of arrays can be jagged; 2D arrays are rectangular. Array of arrays allows variable row sizes, 2D arrays are fixed.
- Find and correct the error: int[][] array = new int[][3];
- Specify row size: new int[3][]. Row dimension must be specified first in array creation.
- What is the result of accessing array[-1][0]?
- ArrayIndexOutOfBoundsException. Negative indices are invalid and throw runtime exception.
- How do you set all elements in the first row to 0?
- Use a loop: for (int i = 0; i < array[0].length; i++) array[0][i] = 0;. Loop through first row indices and assign zero values.
- What is the best way to find the sum of a specific column?
- Iterate over rows, sum the specific column. Access same column index across all rows and accumulate.
- What is the result of array[0][5] if array has 4 columns?
- ArrayIndexOutOfBoundsException. Index 5 exceeds valid column range, causing runtime error.
- Which option describes a jagged array?
- Rows can have different lengths. Unlike rectangular arrays, each row can have different column count.
- Identify the method to access the element at row 2, column 3.
- array[2][3]. Uses bracket notation with row index first, then column index.
- What is the syntax to find the length of the second dimension?
- array[0].length. Column count accessed through any row's length property.
- How do you print all elements of a 2D array?
- Use nested loops to iterate and print elements. Outer loop handles rows, inner loop handles columns for display.
- What is the output of array.length for int[][] array = new int[4][5]?
- 4. Length property returns number of rows in the 2D array.
- What is the consequence of not initializing a 2D array?
- NullPointerException on access. Uninitialized array references are null, causing access errors.
- How do you initialize a 2D array with 3 rows and 4 columns?
- int[][] array = new int[3][4];. Creates array with specified row and column dimensions.
- What is the length property of a 2D array in Java?
- Number of rows (array.length). Returns the number of rows, not total elements.
- What is the syntax to declare a 2D array with predefined values?
- int[][] array = {{1, 2}, {3, 4}};. Initializer lists create and populate array in one statement.
- Identify the method to sum all elements in a 2D array.
- Use nested loops to iterate and sum elements. Accumulate values using nested iteration through all elements.
- Identify the method to fill a 2D array with random values.
- Use nested loops with Math.random(). Generate random numbers and assign to each array position.
- Identify the method to find the maximum element in a 2D array.
- Iterate and compare each element. Track maximum value while traversing all array elements.
- How can you set the element at row 1, column 2 to 5?
- array[1][2] = 5;. Assignment operator sets value at specific row-column position.
- What is a 2D array in Java?
- A grid of elements stored in rows and columns. Arrays organized in row-column structure for storing tabular data.
- How do you transpose a 2D array?
- Swap rows with columns. Transform rows into columns and columns into rows.
- What is the default value of int elements in a 2D array?
- 0. Numeric arrays initialize all elements to zero by default.
- How do you declare a 2D array of integers in Java?
- int[][] array;. Double brackets indicate two dimensions for row and column indices.
- What happens when you try to access array[0][10] in a 2D array?
- ArrayIndexOutOfBoundsException. Index 10 exceeds array bounds, throwing runtime exception.
- What does array[1] refer to in a 2D array?
- The entire second row. Returns reference to the one-dimensional array at index 1.
- How do you iterate over all elements in a 2D array using nested loops?
- Use two nested for loops. Outer loop for rows, inner loop for columns traverses all elements.
- What is the index of the last column in a 2D array with 5 columns?
- 4. Last index is always length minus one in zero-based indexing.
- What is the time complexity to access an element in a 2D array?
- . Direct access by indices provides constant time complexity.
- What is the index of the first row in a 2D array?
- 0. Java arrays use zero-based indexing for both dimensions.
- What is the purpose of array[0].length in a 2D array?
- To get the number of columns in the first row. Determines column count for the first row dimension.
- What is the correct way to compare two 2D arrays for equality?
- Check length and compare each element. Verify dimensions match, then compare corresponding elements.
- How do you find the number of columns in the first row?
- array[0].length. Access length property of any row to get column count.