Common Data Structures

Help Questions

AP Computer Science A › Common Data Structures

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) {

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.

4

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.

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

Which of the following blocks of code converts an array of characters into a string?

private static void string() {

char\[\] vals = {'A','t', ' ', '6',' ','a','m','!'};

String s = "";

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

s += vals\[i\];

}

}

private static void string() {

char\[\] vals = {'A','t', ' ', '6',' ','a','m','!'};

String s;

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

s += vals\[i\];

}

}

private static void string() {

char\[\] vals = {'A','t', ' ', '6',' ','a','m','!'};

String s = "";

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

s = vals\[i\];

}

}

private static void string() {

char\[\] vals = {'A','t', ' ', '6',' ','a','m','!'};

String s = "";

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

vals\[i\] = s;

}

}

private static void string() {

String s = "At 6 am!";

char\[\] vals = new char\[s.length()\];

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

vals\[i\] = s.charAt(i);

}

}

Explanation

The easiest way to consider this is by commenting on the correct answer. You must begin by defining the character array:

char\[\] vals = {'A','t', ' ', '6',' ','a','m','!'};

Next, you must initialize the string value s to be an empty string. This is critical. Otherwise, you can't build your string!

String s = "";

Next, you have the loop. This goes through the characters and concatenates the values to the variable s. The operation to concatenate the characters is the "+=". This will give you the string value of the array of characters.

7

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.

8

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

9

Which of the following blocks of code converts an array of characters into a string?

private static void string() {

char\[\] vals = {'A','t', ' ', '6',' ','a','m','!'};

String s = "";

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

s += vals\[i\];

}

}

private static void string() {

char\[\] vals = {'A','t', ' ', '6',' ','a','m','!'};

String s;

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

s += vals\[i\];

}

}

private static void string() {

char\[\] vals = {'A','t', ' ', '6',' ','a','m','!'};

String s = "";

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

s = vals\[i\];

}

}

private static void string() {

char\[\] vals = {'A','t', ' ', '6',' ','a','m','!'};

String s = "";

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

vals\[i\] = s;

}

}

private static void string() {

String s = "At 6 am!";

char\[\] vals = new char\[s.length()\];

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

vals\[i\] = s.charAt(i);

}

}

Explanation

The easiest way to consider this is by commenting on the correct answer. You must begin by defining the character array:

char\[\] vals = {'A','t', ' ', '6',' ','a','m','!'};

Next, you must initialize the string value s to be an empty string. This is critical. Otherwise, you can't build your string!

String s = "";

Next, you have the loop. This goes through the characters and concatenates the values to the variable s. The operation to concatenate the characters is the "+=". This will give you the string value of the array of characters.

10

Which of the following blocks of code converts an array of characters into a string?

private static void string() {

char\[\] vals = {'A','t', ' ', '6',' ','a','m','!'};

String s = "";

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

s += vals\[i\];

}

}

private static void string() {

char\[\] vals = {'A','t', ' ', '6',' ','a','m','!'};

String s;

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

s += vals\[i\];

}

}

private static void string() {

char\[\] vals = {'A','t', ' ', '6',' ','a','m','!'};

String s = "";

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

s = vals\[i\];

}

}

private static void string() {

char\[\] vals = {'A','t', ' ', '6',' ','a','m','!'};

String s = "";

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

vals\[i\] = s;

}

}

private static void string() {

String s = "At 6 am!";

char\[\] vals = new char\[s.length()\];

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

vals\[i\] = s.charAt(i);

}

}

Explanation

The easiest way to consider this is by commenting on the correct answer. You must begin by defining the character array:

char\[\] vals = {'A','t', ' ', '6',' ','a','m','!'};

Next, you must initialize the string value s to be an empty string. This is critical. Otherwise, you can't build your string!

String s = "";

Next, you have the loop. This goes through the characters and concatenates the values to the variable s. The operation to concatenate the characters is the "+=". This will give you the string value of the array of characters.

Page 1 of 47