Chapter 7 Flashcards

1
Q

The __________ method sorts the array scores of the double[] type.

A

java.util.Arrays.sort(scores)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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 + “ “);

A

1 1 2 3 4 5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How many elements are in array double[] list = new double[5]?

A

5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
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] + " ");
     }
}
A

0 1 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, list[1] is ________.

A

2.0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Assume int[] scores = {1, 20, 30, 40, 50}, what value does java.util.Arrays.binarySearch(scores, 30) return?

A

2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

When you return an array from a method, the method returns __________.

A

the reference of the array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
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] + " ");
 } }
A

1 1 1 1 1 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

The __________ method copies the sourceArray to the targetArray.

A

System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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.

A

heap

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
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] + " ");
     }
}
A

0 1 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Suppose a method p has the following heading:
public static int[] p()
What return statement may be used in p()?

A

return new int[]{1, 2, 3};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Assume int[] scores = {1, 20, 30, 40, 50}, what is the output of System.out.println(java.util.Arrays.toString(scores))?

A

[1, 20, 30, 40, 50]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
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] + " ");
     }
}
A

The program displays 1 2 3 4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

When you pass an array to a method, the method receives __________.

A

the reference of the array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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);

A

list1 is 6 5 4 3 2 1

17
Q

Assume int[] t = {1, 2, 3, 4}. What is t.length?

A

4

18
Q
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] + " ");
     }
}
A

120 200 14

19
Q

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);

A

1 2 3 4 5 6

20
Q
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);
A

1

21
Q
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]);
     }
}
A

The program displays a[1] is 0.

22
Q

If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, the highest index in array list is __________.

A

3

23
Q

What is the correct term for numbers[99]?

A

indexed variable

24
Q

What is the representation of the third element in an array called a?

A

a[2]

25
Q
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 + " ");
     }
}
A

The program displays 2.5 3.0 4.0