Arrays

Help Questions

AP Computer Science A › Arrays

Questions 1 - 10
1

Consider the following code:

public static void main(String\[\] args) {

double\[\]\[\] matrix = {{1,6,7},{1,4,5}};

graphics(matrix);

}

private static double graphics(double\[\]\[\] x) {

double r = 0;

for(int i = 0; i < x.length; i++) {

for(int j = 0; j < x\[i\].length; j++) {

r += x\[i\]\[j\] * (i + 1);

}

}

return r;

}

What is the return value for graphics in the code below:

double\[\]\[\] matrix = {{1,6,7},{1,4,5}};

graphics(matrix);

Explanation

The graphics method takes the 2D array matrix and then runs through each element. This is the point of the double for loop in the method itself. Notice that for each element it does several things. First of all, it is clearly accumulating a value into the variable r. Now, for each element, you are adding:

x\[i\]\[j\] (the current value in the 2D array iteration)

TIMES

(i + 1), or, the current row number. Thus, for the data given you are doing the following:

1* 1 + 1 * 6 + 1 * 7 + 2 * 1 + 2 * 4 + 2 * 5 = 34

2

Consider the following code:

public static void main(String\[\] args) {

int\[\] vec = {8,-2,4,5,-8};

foo(vec);

}

private static void foo(int\[\] x) {

for(int i = 0; i < x.length; i++) {

int y = Math.abs(x\[i\]);

for(int j = 0; j < y; j++) {

System.out.print(x\[i\] + " ");

}

System.out.println();

}

}

Which of the following represents a possible output for the program above?

8 8 8 8 8 8 8 8

-2 -2

4 4 4 4

5 5 5 5 5

-8 -8 -8 -8 -8 -8 -8 -8

8 8 8 8 8 8 8 8

4 4 4 4

5 5 5 5 5

64

4

16

25

64

64

-4

16

25

-64

37

Explanation

In this code's loop, notice that it takes the absolute value of each element. This is done on the line:

int y = Math.abs(x\[i\]);

This value is then used for the second loop, which goes for y times, each time outputting the value of the given member of the original array—but now with its particular sign value. Thus, even numbers like will be output multiple times (i.e. 8). This is done line by line for each member of the parameter array.

3

Consider the following code:

public static void main(String\[\] args) {

double\[\]\[\] matrix = {{1,6,7},{1,4,5}};

graphics(matrix);

}

private static double graphics(double\[\]\[\] x) {

double r = 0;

for(int i = 0; i < x.length; i++) {

for(int j = 0; j < x\[i\].length; j++) {

r += x\[i\]\[j\] * (i + 1);

}

}

return r;

}

What is the return value for graphics in the code below:

double\[\]\[\] matrix = {{1,6,7},{1,4,5}};

graphics(matrix);

Explanation

The graphics method takes the 2D array matrix and then runs through each element. This is the point of the double for loop in the method itself. Notice that for each element it does several things. First of all, it is clearly accumulating a value into the variable r. Now, for each element, you are adding:

x\[i\]\[j\] (the current value in the 2D array iteration)

TIMES

(i + 1), or, the current row number. Thus, for the data given you are doing the following:

1* 1 + 1 * 6 + 1 * 7 + 2 * 1 + 2 * 4 + 2 * 5 = 34

4

Consider the following code:

public static void main(String\[\] args) {

double\[\]\[\] matrix = {{1,6,7},{1,4,5}};

graphics(matrix);

}

private static double graphics(double\[\]\[\] x) {

double r = 0;

for(int i = 0; i < x.length; i++) {

for(int j = 0; j < x\[i\].length; j++) {

r += x\[i\]\[j\] * (i + 1);

}

}

return r;

}

What is the return value for graphics in the code below:

double\[\]\[\] matrix = {{1,6,7},{1,4,5}};

graphics(matrix);

Explanation

The graphics method takes the 2D array matrix and then runs through each element. This is the point of the double for loop in the method itself. Notice that for each element it does several things. First of all, it is clearly accumulating a value into the variable r. Now, for each element, you are adding:

x\[i\]\[j\] (the current value in the 2D array iteration)

TIMES

(i + 1), or, the current row number. Thus, for the data given you are doing the following:

1* 1 + 1 * 6 + 1 * 7 + 2 * 1 + 2 * 4 + 2 * 5 = 34

5

Consider the following code:

public static void main(String\[\] args) {

int\[\] vec = {8,-2,4,5,-8};

foo(vec);

}

private static void foo(int\[\] x) {

for(int i = 0; i < x.length; i++) {

int y = Math.abs(x\[i\]);

for(int j = 0; j < y; j++) {

System.out.print(x\[i\] + " ");

}

System.out.println();

}

}

Which of the following represents a possible output for the program above?

8 8 8 8 8 8 8 8

-2 -2

4 4 4 4

5 5 5 5 5

-8 -8 -8 -8 -8 -8 -8 -8

8 8 8 8 8 8 8 8

4 4 4 4

5 5 5 5 5

64

4

16

25

64

64

-4

16

25

-64

37

Explanation

In this code's loop, notice that it takes the absolute value of each element. This is done on the line:

int y = Math.abs(x\[i\]);

This value is then used for the second loop, which goes for y times, each time outputting the value of the given member of the original array—but now with its particular sign value. Thus, even numbers like will be output multiple times (i.e. 8). This is done line by line for each member of the parameter array.

6

Consider the following code:

public static void main(String\[\] args) {

int\[\] vec = {8,-2,4,5,-8};

foo(vec);

}

private static void foo(int\[\] x) {

for(int i = 0; i < x.length; i++) {

int y = Math.abs(x\[i\]);

for(int j = 0; j < y; j++) {

System.out.print(x\[i\] + " ");

}

System.out.println();

}

}

Which of the following represents a possible output for the program above?

8 8 8 8 8 8 8 8

-2 -2

4 4 4 4

5 5 5 5 5

-8 -8 -8 -8 -8 -8 -8 -8

8 8 8 8 8 8 8 8

4 4 4 4

5 5 5 5 5

64

4

16

25

64

64

-4

16

25

-64

37

Explanation

In this code's loop, notice that it takes the absolute value of each element. This is done on the line:

int y = Math.abs(x\[i\]);

This value is then used for the second loop, which goes for y times, each time outputting the value of the given member of the original array—but now with its particular sign value. Thus, even numbers like will be output multiple times (i.e. 8). This is done line by line for each member of the parameter array.

7

Which of these instantiate a matrix called matrx with 5 columns and 4 rows that takes in integers?

int \[\] \[\] matrx = new int \[\] \[4\];

int \[\] \[\] matrx = new int \[5\] \[4\];

int \[\] \[\] matrx = new int \[5\] \[\];

int \[\] \[\] matrx = new int \[\] \[5\];

int \[\] \[\] matrx = new int \[4\] \[5\];

Explanation

You create a matrix also known as a 2 dimensional array the same way you'd instantiate a normal array except the first array space remains blank and you'd insert the number for the amount of rows. Due to the fact that you want 5 columns and 4 rows, you'd only input the 4 into the second array.

8

Which of these instantiate a matrix called matrx with 5 columns and 4 rows that takes in integers?

int \[\] \[\] matrx = new int \[\] \[4\];

int \[\] \[\] matrx = new int \[5\] \[4\];

int \[\] \[\] matrx = new int \[5\] \[\];

int \[\] \[\] matrx = new int \[\] \[5\];

int \[\] \[\] matrx = new int \[4\] \[5\];

Explanation

You create a matrix also known as a 2 dimensional array the same way you'd instantiate a normal array except the first array space remains blank and you'd insert the number for the amount of rows. Due to the fact that you want 5 columns and 4 rows, you'd only input the 4 into the second array.

9

Which of these instantiate a matrix called matrx with 5 columns and 4 rows that takes in integers?

int \[\] \[\] matrx = new int \[\] \[4\];

int \[\] \[\] matrx = new int \[5\] \[4\];

int \[\] \[\] matrx = new int \[5\] \[\];

int \[\] \[\] matrx = new int \[\] \[5\];

int \[\] \[\] matrx = new int \[4\] \[5\];

Explanation

You create a matrix also known as a 2 dimensional array the same way you'd instantiate a normal array except the first array space remains blank and you'd insert the number for the amount of rows. Due to the fact that you want 5 columns and 4 rows, you'd only input the 4 into the second array.

10

Given the following in C++:

int * data = new int\[12\];

Pick an expression that is equivalent to : data\[5\];

*(data +5);

(data+5)*

&(data+5)

data + 5

(data + 5)&

Explanation

Let's look at this line of code:

int * data = new int\[12\]

An int pointer is created and an array of size 12 is assigned to it.

When data\[5\] is called, the "data" is dereferenced and the value of the 6th position is returned.

The only one of the choices that does this is

*(data + 5)

In line above, the pointer is incremented ot the 6th position and is then dereferenced to get the value.

If the code was

*data

The first item at the first position will be returned.

*(data+1)

This will return the item and the second position and so on.

Page 1 of 4
Return to subject