Chapter 7 Flashcards
The __________ method sorts the array scores of the double[] type.
java.util.Arrays.sort(scores)
What is the output of the following code?
int[] myList = {1, 2, 3, 4, 5, 6};
for (int i = myList.length - 2; i >= 0; i–) {
myList[i + 1] = myList[i];
}
for (int e: myList)
System.out.print(e + “ “);
1 1 2 3 4 5
How many elements are in array double[] list = new double[5]?
5
In the following code, what is the output for list1? public class Test { public static void main(String[] args) { int[] list1 = {1, 2, 3}; int[] list2 = {1, 2, 3}; list2 = list1; list1[0] = 0; list1[1] = 1; list2[2] = 2; for (int i = 0; i < list1.length; i++) System.out.print(list1[i] + " "); } }
0 1 2
If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, list[1] is ________.
2.0
Assume int[] scores = {1, 20, 30, 40, 50}, what value does java.util.Arrays.binarySearch(scores, 30) return?
2
When you return an array from a method, the method returns __________.
the reference of the array
What is output of the following code: public class Test { public static void main(String[] args) { int list[] = {1, 2, 3, 4, 5, 6}; for (int i = 1; i < list.length; i++) list[i] = list[i - 1];
for (int i = 0; i < list.length; i++) System.out.print(list[i] + " "); } }
1 1 1 1 1 1
The __________ method copies the sourceArray to the targetArray.
System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length);
The JVM stores the array in an area of memory, called _______, which is used for dynamic memory allocation where blocks of memory are allocated and freed in an arbitrary order.
heap
In the following code, what is the output for list2? public class Test { public static void main(String[] args) { int[] list1 = {1, 2, 3}; int[] list2 = {1, 2, 3}; list2 = list1; list1[0] = 0; list1[1] = 1; list2[2] = 2; for (int i = 0; i < list2.length; i++) System.out.print(list2[i] + " "); } }
0 1 2
Suppose a method p has the following heading:
public static int[] p()
What return statement may be used in p()?
return new int[]{1, 2, 3};
Assume int[] scores = {1, 20, 30, 40, 50}, what is the output of System.out.println(java.util.Arrays.toString(scores))?
[1, 20, 30, 40, 50]
Analyze the following code: public class Test { public static void main(String[] args) { int[] x = {1, 2, 3, 4}; int[] y = x; x = new int[2]; for (int i = 0; i < y.length; i++) System.out.print(y[i] + " "); } }
The program displays 1 2 3 4
When you pass an array to a method, the method receives __________.
the reference of the array