AP® Computer Science A Practice Questions
Work through AP Computer Science A practice questions unit by unit, 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
Unit 4: Data Collections
Question 3102
Multiple choice
Unit 4: Data Collections
Question 3103
Multiple choice
Unit 4: Data Collections
Question 3104
Multiple choice
Unit 4: Data Collections
Question 3105
Free response
Unit 4: Data Collections
Question 3106
Multiple choice
Unit 4: Data Collections
Question 3107
Multiple choice
Unit 4: Data Collections
Question 3108
Multiple choice
Unit 4: Data Collections
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 3109
Multiple choice
Unit 4: Data Collections
Question 3,101 of 3,140
Page 311 of 314
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.

