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.
Subtopic: 2D Arrays Clear filter
Question 192
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?
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 193
Question 194
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.

