Learn AP Comp Sci

Problem of the Day

Thursday, February 19, 2026


Problem:

Consider the following definition.

int[][] arr = {{ 0,  1,  2,  3},
{ 4, 5, 6, 7},
{ 8, 9, 10, 11},
{12, 13, 14, 15}};

What does the following code segment print?

int s = 0;
for (int i = 0; i < arr.length; i++)
{
s = s + arr[i][i];
s = s + arr[i][arr.length - 1 - i];
}
System.out.println(s);
  1. The sum of the values in the first and last columns of the array
  2. The sum of the values in the first and last rows of the array
  3. The sum of the values in the diagonals of the array
  4. The sum of the four corner values in the array

Show solution:

The correct answer is c. The first summation statement, s = s + arr[i][i]; adds the values from the "top-left to bottom-right" diagonal, while the second statement sums the "top-right to bottom-left" diagonal.