2D Array Traversals - AP Computer Science A

  1. 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.
  2. What is the default value of elements in a newly created boolean 2D array?
    false. Boolean arrays initialize all elements to false by default.
  3. 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.
  4. State the keyword used to initialize a 2D array in Java.
    new. Required keyword to allocate memory for array objects.
  5. What is the type of elements in 'int[][] array = new int[2][3];'?
    int. Declaration specifies integer as the element type.
  6. What is the purpose of nested loops in 2D array traversal?
    To iterate over all elements. Enables systematic access to every array element.
  7. 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.
  8. 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.
  9. What is the default value of elements in a newly created int 2D array?
    0. Integer arrays initialize all elements to zero by default.
  10. What does 'array.length * array[0].length' compute for a 2D array?
    Total number of elements. Multiplies rows by columns for rectangular arrays.
  11. 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.
  12. What is initialized first in a nested loop for 2D array traversal?
    Row index. Outer loop variable controls row position first.
  13. 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.
  14. 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.
  15. 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.
  16. Which keyword is used to create a new 2D array object in Java?
    new. Essential keyword for creating array objects in memory.
  17. 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.
  18. What is the term for accessing each element in a 2D array?
    Traversal. The systematic process of visiting each element in order.
  19. What is the result of accessing an out-of-bounds index in a 2D array?
    ArrayIndexOutOfBoundsException. Runtime exception thrown when accessing invalid indices.
  20. 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.
  21. Which loop structure is commonly used for 2D array traversal?
    Nested for-loops. Outer loop for rows, inner loop for columns.
  22. Identify the expression to access the element at row i, column j.
    array[i][j]. Uses row index i and column index j.
  23. 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.
  24. Identify the syntax error: 'int[][] array = new int()[3][3];'
    Correct: 'new int[3][3]'. Missing type specification between 'new' and brackets.
  25. 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.
  26. What is the syntax to declare a 2D array in Java?
    int[][] arrayName;. Standard declaration syntax using double brackets for 2D arrays.
  27. What does 'int[][] array = new int[3][];' initialize?
    3 rows, undefined column sizes. Creates row array without specifying column dimensions.
  28. 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.
  29. 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.
  30. 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.
  31. What is the result of 'array.length' in a 2D array?
    Number of rows. Returns the count of rows in the array.
  32. 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.
  33. Identify the error: 'int[][] array = { {1, 2}, {3, 4, 5} };'
    Array is jagged, not rectangular. Rows have different lengths, creating irregular structure.
  34. What exception is thrown by accessing an invalid 2D array index?
    ArrayIndexOutOfBoundsException. Standard runtime exception for bounds violations.

Study these flashcards