Java Reference
In-Depth Information
20. Consider the following method, mystery :
public static void mystery(int[] a, int[] b) {
for (int i = 0; i < a.length; i++) {
a[i] += b[b.length - 1 - i];
}
}
What are the values of the elements in array a1 after the following code executes?
int[] a1 = {1, 3, 5, 7, 9};
int[] a2 = {1, 4, 9, 16, 25};
mystery(a1, a2);
21. Consider the following method, mystery2 :
public static void mystery2(int[] a, int[] b) {
for (int i = 0; i < a.length; i++) {
a[i] = a[2 * i % a.length] - b[3 * i % b.length];
}
}
What are the values of the elements in array a1 after the following code executes?
int[] a1 = {2, 4, 6, 8, 10, 12, 14, 16};
int[] a2 = {1, 1, 2, 3, 5, 8, 13, 21};
mystery2(a1, a2);
22. Consider the following method, mystery3 :
public static void mystery3(int[] data, int x, int y) {
data[data[x]] = data[y];
data[y] = x;
}
What are the values of the elements in the array numbers after the following code executes?
int[] numbers = {3, 7, 1, 0, 25, 4, 18, -1, 5};
mystery3(numbers, 3, 1);
mystery3(numbers, 5, 6);
mystery3(numbers, 8, 4);
23. Consider the following method:
public static int mystery4(int[] list) {
int x = 0;
for (int i = 1; i < list.length; i++) {
int y = list[i] - list[0];
if (y > x) {
x = y;
}
}
return x;
}
Search WWH ::




Custom Search