본문 바로가기
자바(Java)/자바 AP

[자바AP연습문제]08.2D Arrays

by 긱펀 2024. 3. 29.
반응형

[자바AP연습문제] 08. 2D Arrays

 

 

 

*08.2D Arryas 단원의 복습내용은 아래 "더보기" 클릭

더보기

In this chapter you learned about two dimensional arrays. A two dimensional array stores objects in a 2d table. You can think of it as storing objects in rows and columns, but it actually uses an array of arrays to store the objects as shown below. In this chapter you learned how to declare 2d arrays, create them, and access array elements. Array elements are accessed using a row and column index. The first element in a 2d array is at row 0 and column 0.

 

  • 2d Array - An array that holds items in a two dimensional grid. You can think of it as storing items in rows and columns (like a bingo card or battleship game). You can access an item (element) at a given row and column index.

  • 2d Array Declaration - To declare an array, specify the type of elements that will be stored in the array, then([][]) to show that it is a 2d array of that type, then at least one space, and then a name for the array. 
    Examples: int[][] seats; String[][] seatingChart;

  • 2d Array Creation - To create a 2d array, type the name and an equals sign then use the new keyword, followed by a space, then the type, and then [numRows][numCols].
    Example: seatingChart = new String[5][4];. This will have 5 rows and 4 columns.

  • 2d Array Index - You can access and set values in a 2d array using the row and column index. The first element in an array called arr is at row 0 and column 0 arr[0][0].

  • 2d Array Initialization - You can also initialize (set) the values in the array when you first create it. In this case you don’t need to specify the size of the array, it will be determined from the number of values that you specify. Example: String[][] seatingInfo = { {"Jamal", "Maria"}, {"Jake", "Suzy"}, {"Emma", "Luke"}}; This will create a 2d array with 3 rows and 2 columns.

  • 2d Array Number of Rows - The number of rows (or height) is the length of the outer array. For an array arr use arr.length to get the number of rows in the array.

  • 2d Array Number of Columns - The number of columns (or width) is the length of the inner array. For an array arr use arr[0].length to get the number of columns.

  • nested for loop - A for loop inside of another for loop. These are used to loop through all the elements in a 2d array. One loop can work through the rows and the other the columns.

  • out of bounds error - This happens when a loop goes beyond the last valid index in an array. Remember that the last valid row index is arr.length - 1. The last valid column index is arr[0].length - 1.

 


 

[문제1]

Consider the following code segment. What is the last row of numbers printed when this code segment is executed?

1
2
3
4
5
6
7
8
9
10
11
12
int[][] points = { {1112131415},
                    {2122232425},
                    {3132333435},
                    {4142434445}};
 for (int row = 0; row < points.length; row++)
 {
     for (int col = points[0].length - 1; col >= row; col--)
     {
          System.out.print(points[row][col] + " ");
     }
     System.out.println();
}
cs

 

A. 45 44 43 42 41
B. 45
C. 41 42
D. 45 44
E. 44 45

 

[문제2]

Consider the following code segment. What is the value of sum as a result of executing the code segment?

1
2
3
4
5
6
7
8
9
10
11
int[][] arr = { {1234},
                {5678},
                {9101112} };
int sum = 0;
for (int[] x : arr)
{
    for (int y = 0; y < x.length - 1; y++)
    {
         sum += x[y];
    }
}
cs

 

A. 36
B. 54
C. 63
D. 68
E. 78

 

[문제3]

How many columns does a have if it is created as follows int[][] a = { {2, 4, 6, 8}, {1, 2, 3, 4}};?

 

A. 2
B. 4
C. 8

 

[문제4]

Which of the following statements assigns the letter S to the third row and first column of a two-dimensional array named strGrid (assuming row-major order).

 

A. strGrid[0][2] = "S";
B. strGrid[1][3] = "S";
C. strGrid[3][1] = "S";
D. strGrid[2][0] = "S";
E. strGrid[0][0] = "S";

 

[문제5]

How would you get the value 6 out of the following array int[][] a = { {2, 4, 6, 8}, {1, 2, 3, 4}};?

 

A. a[0][3]
B. a[1][3]
C. a[0][2]
D. a[2][0]
E. a[3][1]

 

[문제6]

Given the following code segment, what is the value of sum after this code executes?

1
2
3
4
5
6
7
8
int[][] matrix = { {1,1,2,2},{1,2,2,4},{1,2,3,4},{1,4,1,2}};
 
int sum = 0;
int col = matrix[0].length - 2;
for (int row = 0; row < 4; row++)
{
   sum = sum + matrix[row][col];
}
cs

 

A. 4
B. 8
C. 9
D. 12
E. 10

 

[문제7]

What are the contents of mat after the following code segment has been executed?

1
2
3
4
5
6
7
8
9
10
11
12
13
int [][] mat = new int [4][3];
for (int row = 0; row < mat.length; row++)
{
   for (int col = 0; col < mat[0].length; col++)
   {
      if (row < col)
         mat[row][col] = 1;
      else if (row == col)
         mat[row][col] = 2;
      else
         mat[row][col] = 3;
   }
}
cs

 

A. { {2 3 3}, {1 2 3}, {1 1 2}, {1 1 1}}
B. { {2 1 1}, {3 2 1}, {3 3 2}, {3 3 3}}
C. { {2 1 1 1}, {3 2 1 1}, {3 3 2 1}}
D. { {2 3 3 3}, {1 2 3 3}, {1 1 2 3}}
E. { {1 1 1 1}, {2 2 2 2}, {3 3 3 3}}

 

[문제8]

Given the following code segment, what is the value of sum after this code executes?

1
2
3
4
5
6
7
int[][] m = { {1,1,1,1},{1,2,3,4},{2,2,2,2},{2,4,6,8}};
 
int sum = 0;
for (int k = 0; k < m.length; k++)
{
    sum = sum + m[m.length-1-k][1];
}
cs

 

A. 4
B. 6
C. 9
D. 10
E. 20

 

[문제9]

A two-dimensional array, imagePixels, holds the brightness values for the pixels in an image. The brightness can range from 0 to 255. What does the following method compute?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public int findMax(int[][] imagePixels)
{
   int r, c;
   int i, iMax = 0;
 
   for (r = 0; r < imagePixels.length; r++)
   {
      for (c = 0; c < imagePixels[0].length; c++)
      {
         i = imagePixels[r][c];
         if (i > iMax)
            iMax = i;
       }
    }
    return iMax;
 }
cs

 

A. The maximum brightness value for all pixels in imagePixel
B. The column with the greatest brightness sum
C. The most frequent brightness value in imagePixels
D. The row with the greatest brightness sum
E. The sum of the total brightness of imagePixels

 

 

[문제10]

What are the contents of arr after the following code has been executed?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int[][] arr = { {3,2,1},{1,2,3}};
int value = 0;
for (int row = 1; row < arr.length; row++)
{
   for (int col = 1; col < arr[0].length; col++)
   {
      if (arr[row][col] % 2 == 1)
      {
          arr[row][col] = arr[row][col] + 1;
      }
      if (arr[row][col] % 2 == 0)
      {
          arr[row][col] = arr[row][col] * 2;
      }
   }
}
cs

 

A. { {6, 4, 2}, {2, 4, 6}}
B. { {3, 2, 1}, {1, 4, 6}}
C. { {3, 2, 1}, {1, 4, 8}}
D. { {4, 4, 2}, {2, 4, 4}}
E. { {3, 2, 1}, {2, 4, 4}}

 

 


 

[문제 정답은 아래 "더보기" 클릭]

더보기

[문제1 정답]

D

 

[문제2 정답]

B

(Notice that the inner loop goes up to but not including x.length - 1. / 1~12까지의 합 중, 마지막 칼럼 4,8,12는 안 더하니까 빼주면 54가 됨.)

 

[문제3 정답]

B

(The size of the inner array is the number of columns.)

 

[문제4 정답]

D

(In row-major order the row is specified first followed by the column. Row and column indicies start with 0. So letterGrid[2][0] is the 3rd row and 1st column.)

 

[문제5 정답]

C

 

[문제6 정답]

B

(Since col is matrix[0].length - 2 it is 4 - 2 which is 2. This code will loop through all the rows and add all the numbers in the third column (index is 2) which is 2 + 2 + 3 + 1 which is 8.)

 

[문제7 정답]

B

(This code will put a 1 in the array when the row index is less than the column index and a 2 in the array when the row and column index are the same, and a 3 in the array when the row index is greater than the column index.)

 

[문제8 정답]

C

(This adds all the values in column 1 starting with the one in the last row (row 3).)

 

[문제9 정답]

A

(The method works by scanning all the pixels in imagePixels and comparing them to the current iMax value. If the current is greater, it replaces iMax and becomes the new maximum brightness. This is the value that is returned.)

 

[문제10 정답]

C

(The first if will change an odd number to an even. The second if will also execute after an odd number has been made even. Both loops start at index 1 so this only changes the items in the second row and second and third column.)

 

 

 


 

728x90
반응형

댓글