AP Computer Science A : Arrays

Study concepts, example questions & explanations for AP Computer Science A

varsity tutors app store varsity tutors android store

Example Questions

Example Question #1 :Implementation Techniques

Consider the following code:

int[] vals = {6,1,41,5,1};

int[][] newVals = new int[vals.length][];

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

newVals[i] = new int[vals[i]];

for(int j = 0; j < vals[i];j++) {

newVals[i][j] = vals[i] * (j+1);

}

}

What does the code above do?

Possible Answers:

It creates a 2D matrix with thevalsarray for every row.

It sums the values invals, placing the outcome innewVals.

It performs a matrix multiplication onvalsandnewVals, storing the final result innewVals.

代码创建一个二维数组,其中包含相乘iples for the members ofvals,using each of those values to determine the number of multiples to be computed.

The code creates a 2D array that contains all multiples from 1 to 5 for the members ofvals.

Correct answer:

代码创建一个二维数组,其中包含相乘iples for the members ofvals,using each of those values to determine the number of multiples to be computed.

Explanation:

Let's look at the main loop in this program:

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

newVals[i] = new int[vals[i]];

for(int j = 0; j < vals[i];j++) {

newVals[i][j] = vals[i] * (j+1);

}

}

The first loop clearly runs through thevalsarray for the number of items in that array. After this, it creates in thenewVals2D array a second dimension that is as long as the value in the input arrayvals. Thus, for 41, it creates a 3rd row innewValsthat is 41 columns wide.

Then, we go to the second loop. This one iterates from 0 to the value stored in the current location invals. It places in the given 2D array location the multiple of the value. Think ofj + 1. This will go from 1 to 41 for the case of 41 (and likewise for all the values). Thus, you create a 2D array with all the multiples of the initialvalsarray.

Example Question #31 :Common Data Structures

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?

Possible Answers:

64

-4

16

25

-64

64

4

16

25

64

37

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

Correct answer:

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

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 forytimes, each time outputting the value of the given member of the original array—but nowwithits particular sign value. Thus, even numbers likewill be output multiple times (i.e. 8). This is done line by line for each member of the parameter array.

Example Question #1 :Arrays

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 forgraphicsin the code below:

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

graphics(matrix);

Possible Answers:

Correct answer:

Explanation:

Thegraphicsmethod takes the 2D arraymatrixand then runs through each element. This is the point of the doubleforloop in the method itself. Notice that for each element it does several things. First of all, it is clearly accumulating a value into the variabler. 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

Example Question #1 :Arrays

For the following question, consider the following code:

publicstaticvoidmain(String[]args)

{

double[]vec= {4,8,10,18};

System.out.println(fun(vec));

}

privatestaticdoublefun(double[]x)

{

doublep= 0;

for(inti=x.length-1;i> -1;i--)

{

p+=x[i];

}

returnp/x.length;

}

Which of the following is a possible output for this program?

Possible Answers:

Correct answer:

Explanation:

First of all, you can eliminate the following two options very quickly, as thefunmethod returns a double value:

[D@4b71bbc9]

[4,9,11,19]

These are trying to trick you into thinking that it returns an array. (The first value is really what would be the output, since there is no toString defined for the double array by default.)

Now, infun, the loop simply runs through the values in the array, summing those up. Notice, it runs "backwards" through the array, from the end to the beginning. It then divides by the length of the array, giving you the average of the values in the array.

Example Question #1 :Arrays

Consider the following code:

String s = "I read logic for fun!";

boolean[] b = {true,false,true,false,false,true,true,false,true,false,true,false,false,true,false,true,true,false,true,false,false};

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

char c = s.charAt(i);

if(!b[i]) {

c = Character.toUpperCase(c);

} else {

c = Character.toLowerCase(c);

}

System.out.print(c);

}

What is the output for this function code?

Possible Answers:

I ReAd LoGiC FoR FuN!

i rEAd LoGiC fOr FuN!

I read LOGIC for FUN!

I ReAd lOgIc fOr FuN!

The code will throw an exception.

Correct answer:

i rEAd LoGiC fOr FuN!

Explanation:

This code is using what are called parallel arrays. The boolean array has one value for each character in the Strings. Notice the logic in the loop. There is a condition:

if(!b[i]) {

c = Character.toUpperCase(c);

} else {

c = Character.toLowerCase(c);

}

This means that if the booleanisfalse, then you will make the given letter upper case. (This is because of the!in the condition. Be careful!) Otherwise, you make it lower case.

Carefully walking through the code, you will get:

i rEAd LoGiC fOr FuN!

Example Question #1 :Arrays

Suppose your friend has the following lines of code that intend to find the first index of the first positive integer in array[0] ... array[N-1], where array is an array of N integers

int i = 0;

while (array[i] >=0)

{

i++;

}

location = i;

Will your friend's code work as intended?

Possible Answers:

When array contains at least one negative integer

It works when there are no negative integers.

It will work, but unexpectedly fail on occasion.

It never works.

Yes, it works as intended.

Correct answer:

When array contains at least one negative integer

Explanation:

The code segment will work only when the array has at least one integer. If the array has no negative integers, then the while loop will continue running, incrementing i past the length of the array, in which case an Out of Bounds Exception will occur.

Learning Tools by Varsity Tutors