Implementing 2D Array Algorithms - AP Computer Science A

  1. 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.
  2. How do you pass a 2D array to a method in Java?
    Use methodName(int[][] array). Parameter type specifies two-dimensional integer array reference.
  3. 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.
  4. 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.
  5. 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.
  6. What is the result of accessing array[-1][0]?
    ArrayIndexOutOfBoundsException. Negative indices are invalid and throw runtime exception.
  7. 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.
  8. 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.
  9. What is the result of array[0][5] if array has 4 columns?
    ArrayIndexOutOfBoundsException. Index 5 exceeds valid column range, causing runtime error.
  10. Which option describes a jagged array?
    Rows can have different lengths. Unlike rectangular arrays, each row can have different column count.
  11. 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.
  12. What is the syntax to find the length of the second dimension?
    array[0].length. Column count accessed through any row's length property.
  13. 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.
  14. 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.
  15. What is the consequence of not initializing a 2D array?
    NullPointerException on access. Uninitialized array references are null, causing access errors.
  16. 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.
  17. What is the length property of a 2D array in Java?
    Number of rows (array.length). Returns the number of rows, not total elements.
  18. 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.
  19. 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.
  20. 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.
  21. 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.
  22. 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.
  23. 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.
  24. How do you transpose a 2D array?
    Swap rows with columns. Transform rows into columns and columns into rows.
  25. What is the default value of int elements in a 2D array?
    0. Numeric arrays initialize all elements to zero by default.
  26. How do you declare a 2D array of integers in Java?
    int[][] array;. Double brackets indicate two dimensions for row and column indices.
  27. What happens when you try to access array[0][10] in a 2D array?
    ArrayIndexOutOfBoundsException. Index 10 exceeds array bounds, throwing runtime exception.
  28. What does array[1] refer to in a 2D array?
    The entire second row. Returns reference to the one-dimensional array at index 1.
  29. 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.
  30. 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.
  31. What is the time complexity to access an element in a 2D array?
    O(1)O(1). Direct access by indices provides constant time complexity.
  32. What is the index of the first row in a 2D array?
    0. Java arrays use zero-based indexing for both dimensions.
  33. 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.
  34. 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.
  35. 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.

Study these flashcards