2D Array Creation and Access - AP Computer Science A

  1. Which method accesses the element at row 2, column 1?
    array[2][1];. First index specifies row, second index specifies column.
  2. 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.
  3. What is an example of a rectangular 2D array?
    int[][] array = new int[3][3];. All rows have same length, forming regular matrix shape.
  4. 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.
  5. 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.
  6. How do you define a 2D array with specific values?
    int[][] array = {{1, 2}, {3, 4}};. Initializer list syntax creates array with literal values.
  7. What is the purpose of nested loops in 2D arrays?
    To iterate through rows and columns. Outer loop handles rows, inner loop handles columns.
  8. How do you declare a 2D array to store Strings?
    String[][] array;. Declaration syntax for String object references in 2D array.
  9. 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.
  10. 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.
  11. 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.
  12. What is the time complexity for accessing an element in a 2D array?
    O(1). Direct indexing provides constant time element access.
  13. 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.
  14. What will array.length return for a 4x6 2D array?
    4. Length property returns number of rows in the array.
  15. What is the default value of elements in a new int 2D array?
    0. Java initializes numeric array elements to zero by default.
  16. How many elements are in a 2D array of size 4x5?
    20 elements. Total elements equals rows multiplied by columns: 4×5=204 \times 5 = 20.
  17. How do you check if a 2D array element is zero?
    if (array[i][j] == 0). Conditional statement compares element value to zero.
  18. How do you declare a 2D array reference of any type?
    Type[][] array;. Generic declaration syntax for any reference type.
  19. What is an example of initializing a jagged 2D array?
    int[][] array = { {1, 2}, {3, 4, 5} };. Different row lengths create irregular array structure.
  20. 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.
  21. 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.
  22. 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.
  23. 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.
  24. What is the default value of a boolean 2D array?
    false. Java initializes boolean array elements to false by default.
  25. What is the result of accessing out-of-bounds index in a 2D array?
    ArrayIndexOutOfBoundsException. Runtime exception thrown when index exceeds array bounds.
  26. What is the length of a 2D array row array[0]?
    array[0].length. Returns number of columns in the specified row.
  27. How do you declare a 2D array in Java?
    int[][] arrayName;. Declaration syntax uses double brackets for 2D array type.
  28. How do you declare a char 2D array?
    char[][] array;. Declaration syntax for character data type in 2D format.
  29. 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.
  30. What is a jagged array?
    A 2D array where rows have different lengths. Each row can have different number of columns.
  31. 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.
  32. Which keyword is used to create a new 2D array in Java?
    new. Required keyword for dynamic memory allocation in Java.
  33. 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.

Study these flashcards