AP Computer Science A › Computer Science
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);
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
Which is true?
a = 4
b = 2
c = 5
d = 6
a) a = c - d
b) a = b - d
c) a = d - b
d) a = c * d
c)
a)
b)
d)
c) is the correct choice because a = 6 - 2 since d = 6 and b = 2. Substitute the numbers for the variables in each answer choice. You will see that all of the answer choices are not equal to 4 except for c).
Consider the following code:
int\[\] a = {8,4,1,5,1,5,6,2,4};
int b = 1;
boolean\[\] vals = new boolean\[a.length\];
for(int i = 0; i < a.length; i++) {
vals\[i\] = (a\[i\] - 1 > 4);
}
for(int i = 0; i < vals.length; i++) {
String s = "Duns Scotus";
if(!vals\[i\]) {
s = "Mithrandir";
}
System.out.println(s);
}
What is the output for the code above?
Duns Scotus
Mithrandir
Mithrandir
Mithrandir
Mithrandir
Mithrandir
Duns Scotus
Mithrandir
Mithrandir
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Mithrandir
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Mithrandir
Duns Scotus
Duns Scotus
Duns Scotus
Mithrandir
Mithrandir
Duns Scotus
Mithrandir
Duns Scotus
Mithrandir
Duns Scotus
None of the others
Begin by getting a general idea of the loop logic being executed. For each execution of the first loop, you are assigning a boolean to the vals array. Now, you could rewrite the code a little to make the math easier:
a\[i\] - 1 > 4
really is the same as:
a\[i\] > 5
Now, the only values for which this is true are:
8 and 6 (the first and the 7th values in a).
Now, looking at the second loop, the value s will be "Duns Scotus" for every value that is true in vals. This is because the if statement checks for !vals\[i\]. Thus, the first and the seventh values alone will be "Duns Scotus". Everything else will be "Mithrandir".
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);
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
Consider the following code:
int\[\] a = {8,4,1,5,1,5,6,2,4};
int b = 1;
boolean\[\] vals = new boolean\[a.length\];
for(int i = 0; i < a.length; i++) {
vals\[i\] = (a\[i\] - 1 > 4);
}
for(int i = 0; i < vals.length; i++) {
String s = "Duns Scotus";
if(!vals\[i\]) {
s = "Mithrandir";
}
System.out.println(s);
}
What is the output for the code above?
Duns Scotus
Mithrandir
Mithrandir
Mithrandir
Mithrandir
Mithrandir
Duns Scotus
Mithrandir
Mithrandir
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Mithrandir
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Duns Scotus
Mithrandir
Duns Scotus
Duns Scotus
Duns Scotus
Mithrandir
Mithrandir
Duns Scotus
Mithrandir
Duns Scotus
Mithrandir
Duns Scotus
None of the others
Begin by getting a general idea of the loop logic being executed. For each execution of the first loop, you are assigning a boolean to the vals array. Now, you could rewrite the code a little to make the math easier:
a\[i\] - 1 > 4
really is the same as:
a\[i\] > 5
Now, the only values for which this is true are:
8 and 6 (the first and the 7th values in a).
Now, looking at the second loop, the value s will be "Duns Scotus" for every value that is true in vals. This is because the if statement checks for !vals\[i\]. Thus, the first and the seventh values alone will be "Duns Scotus". Everything else will be "Mithrandir".
boolean x;
int y;
x = false;
y = 10;
x = !(y < 10 || y > 10);
System.out.println ( x );
What is printed?
True
False
5
10
x
The expression !(y < 10 || y > 10) evaluates to (y >= 10 && y <= 10).
Since y = 10, the expression evaluates to true.
boolean x;
int y;
x = false;
y = 10;
x = !(y < 10 || y > 10);
System.out.println ( x );
What is printed?
True
False
5
10
x
The expression !(y < 10 || y > 10) evaluates to (y >= 10 && y <= 10).
Since y = 10, the expression evaluates to true.
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);
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
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
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.
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);
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