πŸ“š

Β >Β 

πŸ’»Β 

Β >Β 

πŸ’»

8.4 MC Answers and Review

5 min readβ€’january 2, 2023


AP Computer Science AΒ πŸ’»

130Β resources
See Units

Answers and Review for 2D Array

https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-lCtShz9VMeSl.png?alt=media&token=36428516-ae4d-4ce1-92b0-d16f6cb3fdfe

Image From Wikibooks.

Β 
β›”STOP!β›” Before you look at the answers make sure you gave this practice quiz a try so you can assess your understanding of the concepts covered in unit 8. Click here for the practice questions: AP CS8 Unit # Multiple Choice Questions.
Facts about the test: The AP Computer Science A exam has 40 multiple choice questions and you will be given 90 minutes to complete the section. That means it should take you around 34 minutes to complete 15 questions.

*The following questions were not written by CollegeBoard and although they cover information outlined in the AP Computer Science A Course and Exam Description the formatting on the exam may be different.


1. Which of these is a correct way to declare and initialize a 2D Array?

A. int[][] arrayRectangle = new int[][];
B. int[][] arrayRectangle = new String[5][4];
C. int[][] arrayRectangle = new int[5][4];
D. int arrayRectangle[][] = new int[5][4];
Answer:Β proper syntax
πŸ“„ Study AP CSA, Unit 8.1: 2D Arrays

2. What will print when the following code executes?

https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-2P46EBx65SF4.png?alt=media&token=86829835-e2de-44ff-97e5-d76cf2e9fc67
A. 8
B. 9
C. 10
D. 46
Answer:Β The printed line is row 4, column 6, which was set to a value of row+col-1 in the nested for loop. In this case it is 4+6-1, equaling 9.Β 
πŸ“„Β Study AP CSA, Unit 8.2:Β Traversing 2D Arrays

3. What is located at index[3][2]?

https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-Djw5NIjdTKCR.png?alt=media&token=d14eb08d-8ea8-41ee-9546-c4ea990f7ab7
A. "Daniel"
B. "Taylor"
C. "Fletcher"
D. "Amir"
Answer:Β The elements start at index 0, and the first value [3] represents the row and the second value [2] represents the column. Therefore row 3+1=4 down and column 2+1=3 across, which is "Daniel".
πŸ“„Β Study AP CSA, Unit 8.1:Β 2D Arrays

4. What will print when the following code executes?

https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-9brhMM6Lahk5.png?alt=media&token=21f86a4b-6d21-4cf3-b840-e385d4283c56
A. 3
B. 4
C. 5
D. 6
Answer:Β int i represents rows and int j represents columns. Since j is set to 2, and the index starts from 0, all values will be in the last column that has values of 1, 3, and 1. However, in the for loop, i is only incremented to where it is less than arrayTwo.length - 1, which would be 2. In the first iteration, arrayTwo[0][2] is 1. In the second iteration, arrayTwo[1][2] is 3. There are no more iterations after that, meaning that total = 1+3=4.Β 
πŸ“„Β Study AP CSA, Unit 8.2:Β Traversing 2D Arrays

5. What are the index values for "apple", "grapefruit", and "cherries" in the following 2D array?

https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-0668nO4WMIft.png?alt=media&token=d88872d4-ba84-451f-8d00-d6d7c703a3e8
A. [1][3], [0][1], [2][2]
B. [2][4], [1][2], [3][3]
C. [3][1], [1][0], [2][2]
D. [4][2], [2][1], [3][3]
Answer:Β Since Java is a zero-indexed language, all indexes start from 0. The first value is the row, and the second value is the column. So apple has a row index of 1 and a column index of 3: [1][3]. Grapefruit has a row index of 0 and a column index of 1: [0][1]. Cherries has a row index of 2 and a column index of 2: [2][2].Β 
πŸ“„Β Study AP CSA, Unit 8.1:Β 2D Arrays

6. For any 2D array, what does array.length return?

A. The number of rows and columns averaged
B. The number of columns
C. The number of rows and columns
D. The number of rows
Answer:Β knowledge about 2D arrays
πŸ“„Β Study AP CSA, Unit 8.1:Β 2D Arrays

7. What error will show up when trying to run this program?

https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-CJ0rGcQ9tDBb.png?alt=media&token=490aeade-0035-4721-b761-8d2c64bd7b24
A. NullPointerException
B. InterruptedException
C. ArrayIndexOutOfBoundsException
D. Runtime Exception
Answer:Β definition and using <= in the for loop for length when index starts at 0
πŸ“„Β Study AP CSA, Unit 8.1:Β 2D Arrays

8. Where is the error in the program? What should it be fixed to?

https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-mx0cqTCCtNM5.png?alt=media&token=d6a8fe70-441a-44f5-bdd5-2ee3a9e462fe
A. row < arrayFour.length()
B. arrayFour[0].length
C. row <= arrayFour.length()
D. arrayFour[].length
Answer:Β definition of 2D array; length is not called as a method but as a variable
πŸ“„Β Study AP CSA, Unit 8.1:Β 2D Arrays

9. For each method, determine if it is a row-wise traversal or column-wise traversal. Form: nestedLoopOne(array), nestedLoopTwo(array)

https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-L9cBBDmnQQ9L.png?alt=media&token=745db621-4732-4141-8edf-5667c2411eb8
A. row-wise traversal, row-wise traversal
B. row-wise traversal, column-wise traversal
C. column-wise traversal, row-wise traversal
D. column-wise traversal, column-wise traversal
Answer: definition
πŸ“„Β Study AP CSA, Unit 8.2:Β Traversing 2D Arrays

10. What does arrayFive look like after the code below executes?

https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-IhuGDnEglfey.png?alt=media&token=12d31759-10bc-4b9e-9fb6-eab3208a0c90
A. {{"#", "#', "#", "$", "#"}, {"#", "#", "#", "S", "#"}}
B. {{"#", "#', "#", "$"}, {"#", "#", "S", "#"}}
C. {{"$", "#', "#", "$", "#"}, {"$", "#", "#", "S", "#"}}
D. {{"$", "#', "#", "$"}, {"$", "#", "#", "S"}}
Answer: The 2D array is 2x4, so row indexes go from 0 to 1 and column indexes go from 0 to 3. 0 % 3 is still 0, so the first values of both rows are "$". 3%3 is also 0, so the value at index 3 is also "$".Β 
πŸ“„Β Study AP CSA, Unit 8.1:Β 2D Arrays

11. What do the two bracketed numbers in double-index notation represent?

A. [row][column]
B. [column][row]
C. [size][column]
D. [column][length]
Answer: definition
πŸ“„Β Study AP CSA, Unit 8.1:Β 2D Arrays

12. For all 2D arrays, can different rows have a different number of columns?

A. No
B. Yes
C. Only int arrays
D. Only String arrays
Answer: definition
πŸ“„Β Study AP CSA, Unit 8.1:Β 2D Arrays

13. What is the default value for 2D arrays?

A. All values are defaulted to 0.0
B. All values are defaulted to null
C. It depends on the type of value and isn't the same for all
D. All values are defaulted to false
Answer: definition; given that 2D arrays can be comprised of any types including objects
πŸ“„Β Study AP CSA, Unit 8.1:Β 2D Arrays

14. Given an array as shown below, which enhanced for loops and/or nested loops are able to successfully perform a row-wise traversal with no errors?

https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-BrbNvmNywut9.png?alt=media&token=f68e225b-6ed8-458d-9362-b2582c362288
A. I
B. III
C. I, III
D. I, II, III
Answer: I has no errors as it is a typical nested loop traversing through a 2D array. III also has no errors being a typical enhanced nested loop traversing through a 2D array. II has errors in that it has a type mismatch: cannot convert from element type int[] to int[][].Β 
πŸ“„Β Study AP CSA, Unit 8.2:Β Traversing 2D Arrays

15. How do you find the last element of the last row?

A.Β 
https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-MYaXxx6VWvNt.png?alt=media&token=aa4ac8e3-053b-461e-97d7-1e2c3b498274
B.Β 
https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-7kqOeZQJ7AbJ.png?alt=media&token=e951e735-31bf-450f-94fe-5a18c052cc55
C.Β 
https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-DfJmFOTR8DWB.png?alt=media&token=f8d5087f-afbe-4acd-9a61-441a2f6e43a3
D.Β 
https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-LYrAEgP7PXJC.png?alt=media&token=d69e1d99-0de0-4a3f-8011-41a4ee353bc8
Answer: B. Definition, as index starts from 0, while length starts from 1, therefore to find the last element of the last row, that would be the last index, so the length-1 for both rows and columns.
πŸ“„Β Study AP CSA, Unit 8.1:Β 2D Arrays

What can we help you do now?

πŸ” Study for Unit 9
🦘 Jump to AP CSA Unit 9 Multiple Choice Questions
🀝 Connect with other students studying AP CSA with Hours
Browse Study Guides By Unit
βž•Unit 1 – Primitive Types
πŸ“±Unit 2 – Using Objects
πŸ–₯Unit 3 – Boolean Expressions & if Statements
πŸ•ΉUnit 4 – Iteration
βš™οΈUnit 5 – Writing Classes
⌚️Unit 6 – Array
πŸ’ΎUnit 7 – ArrayList
πŸ–²Unit 9 – Inheritance
πŸ–±Unit 10 – Recursion
πŸ™Exam Reviews