CompSci.rocks

2D Arrays

Free AP Computer Science A multiple-choice practice for topics 4.11–4.13, Unit 4 (Data Collections). Answer five random questions, check your work, and review the essential knowledge behind each one.

Unit 4: Data Collections. Topics 4.11–4.13. This set has 58 questions. Each time the page loads you get five of them, chosen at random. Pick an answer, press Check answer, and read the explanation for every choice. Reload the page or use New questions for a fresh five.

Topics assessed

  • 4.11 2D Array Creation and Access
  • 4.12 2D Array Traversals
  • 4.13 Implementing 2D Array Algorithms

Practice questions

Loading questions…

Essential knowledge for this set

These are the essential knowledge (EKS) statements from the AP Computer Science A Course and Exam Description that the 58 questions in this set assess, grouped by topic and learning objective. The table under each question links here.

Topic 4.11: 2D Array Creation and Access

4.11.A — Develop code used to represent collections of related data using two- dimensional (2D) array objects.

  • 4.11.A.1 A 2D array is stored as an array of arrays. Therefore, the way 2D arrays are created and indexed is similar to 1D array objects. The size of a 2D array is established at the time of creation and cannot be changed. 2D arrays can store either primitive data or object reference data.
  • 4.11.A.2 When a 2D array is created using the keyword new, all of its elements are initialized to the default values for the element data type. The default value for int is 0, for double is 0.0, for boolean is false, and for a reference type is null.
  • 4.11.A.3 The initializer list used to create and initialize a 2D array consists of initializer lists that represent 1D arrays; for example, int[][] arr2D = { {1, 2, 3}, {4, 5, 6} };.
  • 4.11.A.4 The square brackets [row][col] are used to access and modify an element in a 2D array. For the purposes of the exam, when accessing the element at arr[first][second], the first index is used for rows, the second index is used for columns.
  • 4.11.A.5 A single array that is a row of a 2D array can be accessed using the 2D array name and a single set of square brackets containing the row index.
  • 4.11.A.6 The number of rows contained in a 2D array can be accessed through the length attribute. The valid row index values for a 2D array are 0 through one less than the number of rows or the length of the array, inclusive. The number of columns contained in a 2D array can be accessed through the length attribute of one of the rows. The valid column index values for a 2D array are 0 through one less than the number of columns or the length of any given row of the array, inclusive. For example, given a 2D array named values, the number of rows is values.length and the number of columns is values[0].length. Using an index value outside of these ranges will result in an ArrayIndexOutOfBoundsException.

Topic 4.12: 2D Array Traversals

4.12.A — Develop code used to traverse the elements in a 2D array and determine the result of these traversals.

  • 4.12.A.1 Nested iteration statements are used to traverse and access all or an ordered sequence of elements in a 2D array. Since 2D arrays are stored as arrays of arrays, the way 2D arrays are traversed using for loops and enhanced for loops is similar to 1D array objects. Nested iteration statements can be written to traverse the 2D array in row-major order, column-major order, or a uniquely defined order. Row-major order refers to an ordering of 2D array elements where traversal occurs across each row, whereas column-major order traversal occurs down each column.
  • 4.12.A.2 The outer loop of a nested enhanced for loop used to traverse a 2D array traverses the rows. Therefore, the enhanced for loop variable must be the type of each row, which is a 1D array. The inner loop traverses a single row. Therefore, the inner enhanced for loop variable must be the same type as the elements stored in the 1D array. Assigning a new value to the enhanced for loop variable does not change the value stored in the array.

Topic 4.13: Implementing 2D Array Algorithms

4.13.A — Develop code for standard and original algorithms for a particular context or specification that involves 2D arrays and determine the result of these algorithms.

  • 4.13.A.1 There are standard algorithms that utilize 2D array traversals to:
    • determine a minimum or maximum value of all the elements or for a designated row, column, or other subsection
    • compute a sum or average of all the elements or for a designated row, column, or other subsection
    • determine if at least one element has a particular property in the entire 2D array or for a designated row, column, or other subsection
    • determine if all elements of the 2D array or a designated row, column, or other subsection have a particular property
    • determine the number of elements in the 2D array or in a designated row, column, or other subsection having a particular property
    • access all consecutive pairs of elements
    • determine the presence or absence of duplicate elements in the 2D array or in a designated row, column, or other subsection
    • shift or rotate elements in a row left or right or in a column up or down
    • reverse the order of the elements in a row or column