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
The reverse method is defined in the textbook. What is list1 after executing the following statements?
int[] list1 = {1, 2, 3, 4, 5, 6};
list1 = reverse(list1);
list1 is 6 5 4 3 2 1
Assume int[] t = {1, 2, 3, 4}. What is t.length?
4
public class Test { public static void main(String[] args) { int[] x = {120, 200, 016}; for (int i = 0; i < x.length; i++) System.out.print(x[i] + " "); } }
120 200 14
The reverse method is defined in this section. What is list1 after executing the following statements?
int[] list1 = {1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
1 2 3 4 5 6
What is the output of the following code? double[] myList = {1, 5, 5, 5, 5, 1}; double max = myList[0]; int indexOfMax = 0; for (int i = 1; i < myList.length; i++) { if (myList[i] > max) { max = myList[i]; indexOfMax = i; } } System.out.println(indexOfMax);
1
Analyze the following code: public class Test { public static void main(String[] args) { int[] a = new int[4]; a[1] = 1; a = new int[2]; System.out.println("a[1] is " + a[1]); } }
The program displays a[1] is 0.
If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, the highest index in array list is __________.
3
What is the correct term for numbers[99]?
indexed variable
What is the representation of the third element in an array called a?
a[2]
Analyze the following code: public class Test { public static void main(String[] args) { double[] x = {2.5, 3, 4}; for (double value: x) System.out.print(value + " "); } }
The program displays 2.5 3.0 4.0