CHP 7

0.0(0)
studied byStudied by 0 people
GameKnowt Play
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/70

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

71 Terms

1
New cards

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

A. a[2]

2
New cards

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

B. 2.0

3
New cards

* Which of the following is incorrect?

C. int[] a = new int(2);

D. int a = new int[2];

E. int a() = new int[2];

4
New cards

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

D. 3

5
New cards

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

B. 5

6
New cards

* What is the correct term for numbers[99]?

C. indexed variable

7
New cards

Suppose int i = 5, which of the following can be used as an index for array double[] t = new double[100]?

A. i

B. (int)(Math.random() * 100))

C. i + 10

8
New cards

Analyze the following code.

public class Test {

public static void main(String[] args) {

int[] x = new int[3];

System.out.println("x[0] is " + x[0]);

}

}

C. The program runs fine and displays x[0] is 0.

9
New cards

Which of the following statements is valid?

B. double d[] = new double[30];

C. int[] i = {3, 4, 3, 2};

10
New cards

How can you initialize an array of two characters to 'a' and 'b'?

C. char[] charArray = {'a', 'b'};

D. char[] charArray = new char[]{'a', 'b'};

11
New cards

What would be the result of attempting to compile and run the following code?

public class Test {

public static void main(String[] args) {

double[] x = new double[]{1, 2, 3};

System.out.println("Value is " + x[1]);

}

}

E. The program compiles and runs fine and the output "Value is 2.0" is printed.

12
New cards

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

C. 4

13
New cards

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

B. 1

14
New cards

Analyze the following code:

public class Test {

public static void main(String[] args) {

int[] x = new int[5];

int i;

for (i = 0; i < x.length; i++)

x[i] = i;

System.out.println(x[i]);

}

}

C. The program has a runtime error because the last statement in the main method causes ArrayIndexOutOfBoundsException.

15
New cards

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

}

}

C. The program displays 2.5 3.0 4.0

16
New cards

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

D. 1 1 2 3 4 5

17
New cards

What is output of the following code:

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] + " ");

}

}

B. 120 200 14

18
New cards

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] + " ");

}

}

D. 1 1 1 1 1 1

19
New cards

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] + " ");

}

}

C. 0 1 2

20
New cards

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] + " ");

}

}

C. 0 1 2

21
New cards

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

22
New cards

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 < x.length; i++)

System.out.print(x[i] + " ");

}

}

B. The program displays 0 0

23
New cards

Analyze the following code:

public class Test {

public static void main(String[] args) {

final 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] + " ");

}

}

C. The program has a compile error on the statement x = new int[2], because x is final and cannot be changed.

24
New cards

Analyze the following code.

int[] list = new int[5];

list = new int[6];

C. The code can compile and run fine. The second line assigns a new array to list.

25
New cards

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

}

}

C. The program displays a[1] is 0.

26
New cards

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

C. the reference of the array

27
New cards

Show the output of the following code:

public class Test {

public static void main(String[] args) {

int[] x = {1, 2, 3, 4, 5};

increase(x);

int[] y = {1, 2, 3, 4, 5};

increase(y[0]);

System.out.println(x[0] + " " + y[0]);

}

public static void increase(int[] x) {

for (int i = 0; i < x.length; i++)

x[i]++;

}

public static void increase(int y) {

y++;

}

}

D. 2 1

28
New cards

Do the following two programs produce the same result?

Program I:

public class Test {

public static void main(String[] args) {

int[] list = {1, 2, 3, 4, 5};

reverse(list);

for (int i = 0; i < list.length; i++)

System.out.print(list[i] + " ");

}

public static void reverse(int[] list) {

int[] newList = new int[list.length];

for (int i = 0; i < list.length; i++)

newList[i] = list[list.length - 1 - i];

list = newList;

}

}

Program II:

public class Test {

public static void main(String[] args) {

int[] oldList = {1, 2, 3, 4, 5};

reverse(oldList);

for (int i = 0; i < oldList.length; i++)

System.out.print(oldList[i] + " ");

}

public static void reverse(int[] list) {

int[] newList = new int[list.length];

for (int i = 0; i < list.length; i++)

newList[i] = list[list.length - 1 - i];

list = newList;

}

}

A. Yes

29
New cards

Analyze the following code:

public class Test {

public static void main(String[] args) {

int[] oldList = {1, 2, 3, 4, 5};

reverse(oldList);

for (int i = 0; i < oldList.length; i++)

System.out.print(oldList[i] + " ");

}

public static void reverse(int[] list) {

int[] newList = new int[list.length];

for (int i = 0; i < list.length; i++)

newList[i] = list[list.length - 1 - i];

list = newList;

}

}

A. The program displays 1 2 3 4 5.

30
New cards

Analyze the following code:

public class Test1 {

public static void main(String[] args) {

xMethod(new double[]{3, 3});

xMethod(new double[5]);

xMethod(new double[3]{1, 2, 3});

}

public static void xMethod(double[] a) {

System.out.println(a.length);

}

}

C. The program has a compile error because xMethod(new double[3]{1, 2, 3}) is incorrect.

31
New cards

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.

B. heap

32
New cards

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

C. the reference of the array

33
New cards

Suppose a method p has the following heading:

public static int[] p()

What return statement may be used in p()?

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

34
New cards

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

B. list1 is 6 5 4 3 2 1

35
New cards

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. list1 is 1 2 3 4 5 6

36
New cards

Which of the following declarations are correct?

D. public static void print(double... numbers)

E. public static void print(int n, double... numbers)

37
New cards

Which of the following statements are correct to invoke the printMax method in Listing 7.5 in the textbook?

A. printMax(1, 2, 2, 1, 4);

B. printMax(new double[]{1, 2, 3});

C. printMax(1.0, 2.0, 2.0, 1.0, 4.0);

38
New cards

For the binarySearch method in Section 7.10.2, what is low and high after the first iteration of the while loop when invoking binarySearch(new int[]{1, 4, 6, 8, 10, 15, 20}, 11)?

D. low is 5 and high is 4

39
New cards

If a key is not in the list, the binarySearch method returns _________.

C. -(insertion point + 1)

40
New cards

Use the selectionSort method presented in this section to answer this question. Assume list is {3.1, 3.1, 2.5, 6.4, 2.1}, what is the content of list after the first iteration of the outer loop in the method?

E. 2.1, 3.1, 2.5, 6.4, 3.1

41
New cards

Use the selectionSort method presented in this section to answer this question. What is list1 after executing the following statements?

double[] list1 = {3.1, 3.1, 2.5, 6.4};

selectionSort(list1);

B. list1 is 2.5 3.1, 3.1, 6.4

42
New cards

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

C. java.util.Arrays.sort(scores)

43
New cards

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

D. 2

44
New cards

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

E. -2

45
New cards

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

B. [1, 20, 30, 40, 50]

46
New cards

How can you get the word "abc" in the main method from the following call?

java Test "+" 3 "abc" 2

C. args[2]

47
New cards

Suppose int i = 5, which of the following can be used as an index for array double[] t = new double[100]?

A. i

B. (int)(Math.random() * 100))

C. i + 10

48
New cards

Given the following program:

public class Test {

public static void main(String[] args) {

for (int i = 0; i < args.length; i++) {

System.out.print(args[i] + " ");

}

}

}

What is the output, if you run the program using

java Test 1 2 3

C. 1 2 3

49
New cards

Which code fragment would correctly identify the number of arguments passed via the command line to a Java application, excluding the name of the class that is being invoked?

A. int count = args.length;

50
New cards

Which correctly creates an array of five empty Strings?

B. String[] a = {"", "", "", "", ""};

51
New cards

Identify the problems in the following code.

public class Test {

public static void main(String argv[]) {

System.out.println("argv.length is " + argv.length);

}

}

D. If you run this program without passing any arguments, the program would display argv.length is 0.

52
New cards

Which of the following are valid array declarations?

char[] charArray = new char[26];

53
New cards

The name length is a method in an array.

false

54
New cards

The ________ method copies the sourceArray to the targetArray.

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

55
New cards

The element in the array must be of a primitive data type.

false

56
New cards

Suppose array a is int[] a = {1, 2, 3}, what is a[0] - a[2]?

-2

57
New cards

The array index of the first element in an array is 0.

true

58
New cards

The array index is not limited to int type.

false

59
New cards

4. Consider the following code fragment:

int[] list = new int[10];

for (int i = 0; i <= list.length; i++) {

� list[i] = (int)(Math.random() * 10);

}

Which of the following statements is true?

a. list.length must be replaced by 10

b. The loop body will execute 10 times, filling up the array with random numbers.

c. The loop body will execute 10 times, filling up the array with zeros.

d. The code has a runtime error indicating that the array is out of bound.

D.The code has a runtime error indicating that the array is out of bound.

60
New cards

7. Given the following statement

���� int[ ] list = new int[10];

The array variable list contains a memory address that refers to an array of 10 int values.

61
New cards

Assume the signature of the method xMethod is as follows.

public static void xMethod(double[] a)

Which of the following could be used to invoke xMethod?

xMethod(new double[2]);

62
New cards

Analyze the following code:

public class Test {

� public static void main(String[] args) {

��� int[] x = {0, 1, 2, 3, 4, 5};

��� xMethod(x, 5);

� }

� public static void xMethod(int[] x, int length) {

��� for (int i = 0; i < length; i++)

����� System.out.print(" " + x[i]);

� }

}

The program displays 0 1 2 3 4.

63
New cards

Analyze the following code:

public class Test {

� public static void main(String[] args) {

��� int[] x = new int[5];

��� for (int i = 0; i < x.length; i++)

����� x[i] = i;

��� System.out.println(x[i]);

� }

}

The program has a syntax error because i is not defined in the last statement in the main method.

64
New cards

The array size is specified when the array is declared.

false

65
New cards

Binary search can be applied on an unsorted list.

false

66
New cards

You can use the operator == to check whether two variables refer to the same array.

true

67
New cards

Assume double[] scores = {1, 2, 3, 4, 5}, what value does java.util.Arrays.binarySearch(scores, 3.5) return?

-4

68
New cards

The arraycopy method does not allocate memory space for the target array. The target array must already be created with memory space allocated.

true

69
New cards

x.length does not exist if x is null.

true

70
New cards

________ declares an array of char.

A) char chars[]

D) char[] chars

71
New cards

Which of the following is correct to create an array?

int[] m = {1, 2, 3};