Java Reference
In-Depth Information
public static void mystery(int x, int[] a) {
x = x + 1;
a[x] = a[x] + 1;
System.out.println(x + " " + Arrays.toString(a));
}
}
16. What is the output of the following program?
public class ReferenceMystery2 {
public static void main(String[] args) {
int x = 1;
int[] a = new int[2];
mystery(x, a);
System.out.println(x + " " + Arrays.toString(a));
x--;
a[1] = a.length;
mystery(x, a);
System.out.println(x + " " + Arrays.toString(a));
}
public static void mystery(int x, int[] list) {
list[x]++;
x++;
System.out.println(x + " " + Arrays.toString(list));
}
}
17. Write a method called swapPairs that accepts an array of integers and swaps the elements at adjacent indexes. That
is, elements 0 and 1 are swapped, elements 2 and 3 are swapped, and so on. If the array has an odd length, the final
element should be left unmodified. For example, the list {10, 20, 30, 40, 50} should become {20, 10, 40,
30, 50} after a call to your method.
Section 7.4: Advanced Array Techniques
18. What are the values of the elements in the array numbers after the following code is executed?
int[] numbers = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
for (int i = 0; i < 9; i++) {
numbers[i] = numbers[i + 1];
}
19. What are the values of the elements in the array numbers after the following code is executed?
int[] numbers = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
for (int i = 1; i < 10; i++) {
numbers[i] = numbers[i - 1];
}
Search WWH ::




Custom Search