slug
type
status
category
summary
date
tags
password
icon

Key Points for 2D Arrays (AP Computer Science A)

  1. Definition and Structure:
      • A 2D array is an array of arrays. It is used to represent a table, matrix, or grid of values.
      • Syntax: dataType[][] arrayName = new dataType[rows][columns];
  1. Accessing Elements:
      • Access elements in a 2D array using indices: arrayName[row][column].
      • Indexing starts at 0, so arrayName[0][0] refers to the first element.
  1. Traversing 2D Arrays:
      • A common technique is using nested loops (one for rows and another for columns).
      • Example:
    1. Manipulating Elements:
        • You can modify values of a 2D array using the same index syntax: array[row][column] = value;
    1. Common Operations:
        • Finding the sum of all elements: Traverse through the array and sum the elements.
        • Searching for a specific value: Traverse through the array to check for the target value.
    1. Array Initialization:
        • Arrays can be initialized with values when declared:
      1. Jagged Arrays:
          • A jagged array is an array of arrays where each row can have a different number of columns:

        Sample Code (for traversing and manipulating 2D arrays)

        20 AP Style Multiple-Choice Questions (MCQs)

        1. What is the correct way to declare a 2D array in Java with 3 rows and 4 columns? a) int[][] array = new int[3][4];
          1. b) int[] array = new int[3][4];
            c) int[][] array = new int[4][3];
            d) int[] array = new int[4][3];
        1. Given the 2D array int[][] arr = {{1, 2}, {3, 4}};, what is the value of arr[1][0]?
          1. a) 4
            b) 1
            c) 3
            d) 2
        1. Which of the following is the correct way to initialize a jagged array in Java?
          1. a) int[][] jaggedArray = new int[3][2];
            b) int[][] jaggedArray = new int[3][];
            c) int jaggedArray = new int[3][2];
            d) int jaggedArray = new int[2][3];
        1. How do you traverse through each element in a 2D array in Java?
          1. a) Using one for loop
            b) Using two nested for loops
            c) Using a while loop
            d) Using a foreach loop
        1. What does the following code print?
          1. a) 1
            b) 2
            c) 4
            d) 6
        1. What is the output of this code?
          1. a) 1
            b) 3
            c) 4
            d) 2
        1. What is the purpose of the length attribute when working with 2D arrays in Java?
          1. a) It gives the total number of elements in the 2D array
            b) It gives the number of rows in the 2D array
            c) It gives the number of columns in the 2D array
            d) It gives the size of each element
        1. Which of the following will result in an ArrayIndexOutOfBoundsException?
          1. a) arr[0][0]
            b) arr[2][3] when arr has only 2 rows
            c) arr[1][1]
            d) None of the above
        1. Which of the following methods can be used to change an element of a 2D array?
          1. a) array.set(row, column, value);
            b) array[row][column] = value;
            c) array.add(row, column, value);
            d) array.update(row, column, value);
        1. Given a 2D array int[][] arr = {{5, 7, 9}, {1, 3, 5}};, what is arr[1][2]?
          1. a) 7
            b) 3
            c) 5
            d) 9
        1. How many times will the following code print "Hello"?
          1. a) 6
            b) 3
            c) 2
            d) 4
        1. How would you initialize a jagged array of 3 rows where the first row has 2 columns, the second row has 3 columns, and the third row has 4 columns?
          1. a) int[][] arr = new int[3][]; arr[0] = new int[2]; arr[1] = new int[3]; arr[2] = new int[4];
            b) int[][] arr = new int[3][2]; arr[1] = new int[3]; arr[2] = new int[4];
            c) int[][] arr = new int[2][3]; arr[0] = new int[2]; arr[1] = new int[3]; arr[2] = new int[4];
            d) int[][] arr = {{1, 2}, {3, 4}, {5, 6}};
        1. In a 2D array, if the first dimension has 4 rows and the second dimension has 3 columns, how many elements can the array hold?
          1. a) 12
            b) 7
            c) 4
            d) 3
        1. How can a value be added to a specific row and column of a 2D array?
          1. a) array[row][column] = value;
            b) array.add(row, column, value);
            c) array.insert(row, column, value);
            d) array.push(row, column, value);
        1. What is the result of this code?
          1. a) 0
            b) 1
            c) 100
            d) Null
        1. What is the output of the following code?
          1. a) 1 2 3
            b) 1 3 5
            c) 2 4 6
            d) 5 4 3
        1. What is the index of the last element in the first row of a 2D array with 4 columns?
          1. a) 3
            b) 4
            c) 1
            d) 0
        1. How would you initialize a 2D array with 2 rows and 3 columns, all elements set to 0?
          1. a) int[][] arr = new int[2][3];
            b) int[][] arr = {{0, 0, 0}, {0, 0, 0}};
            c) int arr[][] = new int[2][3];
            d) All of the above
        1. What happens if you try to access an index outside the bounds of a 2D array?
          1. a) The program will continue without any errors
            b) An ArrayIndexOutOfBoundsException will be thrown
            c) The program will crash without any message
            d) The program will loop indefinitely
        1. How do you modify the value of an element in a 2D array?
          1. a) array[row].set(column, value);
            b) array[row][column] = value;
            c) array[row].update(column, value);
            d) array.set(row, column, value);
         
         
        Answers
        1. a
        1. c
        1. b
        1. b
        1. d
        1. c
        1. b
        1. b
        1. b
        1. c
        1. a
        1. a
        1. a
        1. a
        1. c
        1. b
        1. a
        1. d
        1. b
        1. b
         
         
         
         
         
         
         
         
        CSA UNIT 7: ArrayListCSA UNIT 9: Inheritance
        Loading...
        现代数学启蒙
        现代数学启蒙
        推广现代数学🍚
        最新发布
        Statistics Key  Concepts and Selected Questions (*_*)
        2025-2-20
        CSA UNIT 7: ArrayList
        2025-2-20
        CSA UNIT 6:  ARRAY
        2025-2-20
        CSA UNIT 10: Recursion
        2025-2-20
        CSA UNIT 9: Inheritance
        2025-2-19
        CSA UNIT 8: 2D Array
        2025-2-19
        公告
        🎉现代数学启蒙(MME:Modern Mathematics Enlightenment)欢迎您🎉
        -- 感谢您的支持 ---