AP® Computer Science A Data Collections Practice Questions
Work through AP Computer Science A practice questions on Data Collections, then check each answer against a worked solution.
Free · no sign-up Open questions need no account. Sign up or log in to open the rest.
Multiple choice
Sorting and Searching
Question 1182
Multiple choice
Sorting and Searching
Question 1183
Multiple choice
Sorting and Searching
Question 1184
Multiple choice
Using Text Files
Question 1185
Multiple choice
Using Text Files
Question 1186
Multiple choice
Arraylists
Question 1187
Free response
Arraylists
Question 1188
Multiple choice
Arrays
Question 1189
Multiple choice
Arraylists
Question 1190
Multiple choice
2D Arrays
Consider the following code segment.
int[][] grid = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int sum = 0;
for (int r = 0; r < grid.length; r++)
{
for (int c = 0; c < grid[0].length; c++)
{
if (r + c == 2)
{
sum = sum + grid[r][c];
}
else if (r == c)
{
sum = sum - grid[r][c];
}
}
}
What is the value of sum after the loops?
Pick an answer, then check it. The solution opens in a window.
CCorrect answer
Main Solution
Answer
Answer: C. The anti-diagonal values 3, 5, and 7 are added. The main diagonal cells that are not already handled by the first branch are 1 and 9, which are subtracted. The total is 15 - 10 = 5.
Answer: Correct: C
A
15
B
0
C
5
D
-5
Answer images
Question 1,181 of 1,222
Page 119 of 123
AP® and Advanced Placement® are registered trademarks of the College Board, which is not affiliated with, and does not endorse, Verityprep. Names are used for identification purposes only.

