Java Ch 7

Ace your homework & exams now with Quizwiz!

List 3 ways to copy an array

1. Use a loop to copy individual elements one by one. 2. Use the static arraycopy method in the System class. 3. Use the clone method to copy arrays.

6.44 Assume int[] scores = {1, 20, 30, 40, 50}, what value does java.util.Arrays.binarySearch(scores, 30) return? A. 0 B. -1 C. 1 D. 2 E. -2

2

6.29 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 B. No

A

6.36 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 B. list1 is 6 5 4 3 2 1 C. list1 is 0 0 0 0 0 0 D. list1 is 6 6 6 6 6 6

A

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 B. The program displays 0 0 C. The program displays 0 0 3 4 D. The program displays 0 0 0 0

A

Sections 6.2 Array Basics 6.1 What is the representation of the third element in an array called a? A. a[2] B. a(2) C. a[3] D. a(3)

A

6.30 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. B. The program displays 1 2 3 4 5 and then raises an ArrayIndexOutOfBoundsException. C. The program displays 5 4 3 2 1. D. The program displays 5 4 3 2 1 and then raises an ArrayIndexOutOfBoundsException.

A - The correct answer is A Explanation: The contents of the array oldList have not been changed as result of invoking the reverse method. Initially, list refers to the same heap as oldList array through pass-by-sharing; then newList is generated and stored in a different heap; by list = newList, list now refers to the same heap as newList, and list is no longer related to oldList any more.

linear search

A method to search an element in an array. Linear search compares the key with the element in the array sequentially

Indexed variable

A variable that is represented using an array reference along with an index to refer to an individual element in the array

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; B. int count = args.length - 1; C. int count = 0; while (args[count] != null) count ++; D. int count=0; while (!(args[count].equals(""))) count ++;

A.

What is the representation of the third element in an array called a? A. a[2] B. a(2) C. a[3] D. a(3)

A. a[2]

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 D. i + 6.5 E. Math.random() * 100

A. i B. (int)(Math.random() * 100)) C. i + 10

Which of the following are correct to declare an array of int values? A. int[] a; B. int a[]; C. int a; D. double[] a;

A. int[] a; B. int a[];

6.7 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 D. i + 6.5 E. Math.random() * 100

ABC

Which of the following statements are correct to invoke the printMax method in Listing 6.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); D. printMax(new int[]{1, 2, 3});

ABC The correct answer is ABC Explanation: The last one printMax(new int[]{1, 2, 3}); is incorrect, because the array must of the double[] type.

Which of the following is the correct header of the main method? A. public static void main(String[] args) B. public static void main(String args[]) C. public static void main(String[] x) D. public static void main(String x[]) E. static void main(String[] args)

ABCD

Binary Search

An efficient method to search a key in an array. Binary search first compares the key with the element in the middle of the array and reduces the search range by half. For binary search to work, the array must be pre-sorted

Index

An integer value used to specify the position of an element in the array. The array index is an int value stating with 0 for the first element, 1 for the second, and so on

6.13 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. 0 B. 1 C. 2 D. 3 E. 4

B

6.2 If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, list[1] is ________. A. 3.4 B. 2.0 C. 3.5 D. 5.5 E. undefined

B

6.22 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] + " "); } } A. The program displays 1 2 3 4 B. The program displays 0 0 C. The program displays 0 0 3 4 D. The program displays 0 0 0 0

B

6.32 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. stack B. heap C. memory block D. dynamic memory

B

6.35 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 1 2 3 4 5 6 B. list1 is 6 5 4 3 2 1 C. list1 is 0 0 0 0 0 0 D. list1 is 6 6 6 6 6 6

B

6.46 Assume int[] list1 = {3, 4, 1, 9, 13}, int[] list2 = {3, 4, 1, 9, 13}, and int[] list3 = {4, 3, 1, 9, 13}, what is System.out.println(java.util.Arrays.equals(list1, list2) + " " + java.util.Arrays.equals(list1, list3)); A. true true B. true false C. false true D. false fasle

B

6.5 How many elements are in array double[] list = new double[5]? A. 4 B. 5 C. 6 D. 0

B

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} B. [1, 20, 30, 40, 50] C. {1 20 30 40 50} D. [1 20 30 40 50]

B

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); A. list1 is 3.1, 3.1, 2.5, 6.4 B. list1 is 2.5 3.1, 3.1, 6.4 C. list1 is 6.4, 3.1, 3.1, 2.5 D. list1 is 3.1, 2.5, 3.1, 6.4

B

What is output of the following code: public class Test { public static void main(String[] args) { int[] x = {120, 200, 16}; for (int i = x.length-1; i> 0; i--) System.out.print(x[i] + " "); } } A. 120 200 16 B. 16 200 120 C. 120 200 20 D. 200 16 120

B

Which correctly creates an array of five empty Strings? A. String[] a = new String [5]; B. String[] a = {"", "", "", "", ""}; C. String[5] a; D. String[ ] a = new String [5]; for (int i = 0; i < 5; a[i++] = null);

B

If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, list[1] is ________. A. 3.4 B. 2.0 C. 3.5 D. 5.5 E. undefined

B. 2.0

How many elements are in array double[] list = new double[5]? A. 4 B. 5 C. 6 D. 0

B. 5

Which of the following statements are valid? A. int i = new int(30); B. double d[] = new double[30]; C. int[] i = {3, 4, 3, 2}; D. char[] c = new char(); E. char[] c = new char[4]{'a', 'b', 'c', 'd'};

B. double d[] = new double[30]; C. int[] i = {3, 4, 3, 2};

Once an array is created, its size _______. A. can be changed B. is fixed C. is not determined

B. is fixed

Which of the following is correct? A. String[] list = new String{"red", "yellow", "green"}; B. String[] list = new String[]{"red", "yellow", "green"}; C. String[] list = {"red", "yellow", "green"}; D. String list = {"red", "yellow", "green"}; E. String list = new String{"red", "yellow", "green"};

BC

6.9 Which of the following statements is valid? A. int i = new int(30); B. double d[] = new double[30]; C. int[] i = {3, 4, 3, 2}; D. char[] c = new char(); E. char[] c = new char[4]{'a', 'b', 'c', 'd'};

BC The correct answer is BC Explanation: e would be corrected if it is char[] c = new char[]{'a', 'b', 'c', 'd'};

(for-each loop) 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, 4 B. The program displays 2.5 3 4 C. The program displays 2.5 3.0 4.0 D. The program displays 2.5, 3.0 4.0 E. The program has a syntax error because value is undefined.

C

6.12 Assume int[] t = {1, 2, 3, 4}. What is t.length? A. 0 B. 3 C. 4 D. 5

C

6.14 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]); } } A. The program displays 0 1 2 3 4. B. The program displays 4. C. The program has a runtime error because the last statement in the main method causes ArrayIndexOutOfBoundsException. D. The program has a compile error because i is not defined in the last statement in the main method.

C

6.47 Assume int[] scores = {3, 4, 1, 9, 13}, which of the following statement displays all the element values in the array? A. System.out.println(scores); B. System.out.println(scores.toString()); C. System.out.println(java.util.Arrays.toString(scores)); D. System.out.println(scores[0]);

C

6.6 What is the correct term for numbers[99]? A. index B. index variable C. indexed variable D. array variable E. array

C

6.8 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]); } } A. The program has a compile error because the size of the array wasn't specified when declaring the array. B. The program has a runtime error because the array elements are not initialized. C. The program runs fine and displays x[0] is 0. D. The program has a runtime error because the array element x[0] is not defined.

C

Analyze the following code. int[] list = new int[5]; list = new int[6]; A. The code has compile errors because the variable list cannot be changed once it is assigned. B. The code has runtime errors because the variable list cannot be changed once it is assigned. C. The code can compile and run fine. The second line assigns a new array to list. D. The code has compile errors because you cannot assign a different size array to list.

C

How can you get the word "abc" in the main method from the following call? java Test "+" 3 "abc" 2 A. args[0] B. args[1] C. args[2] D. args[3]

C

If a key is not in the list, the binarySearch method returns _________. A. insertion point B. insertion point - 1 C. -(insertion point + 1) D. -insertion point

C

Section 6.12 The Arrays Class 6.43 The __________ method sorts the array scores of the double[] type. A. java.util.Arrays(scores) B. java.util.Arrays.sorts(scores) C. java.util.Arrays.sort(scores) D. Njava.util.Arrays.sortArray(scores)

C

Section 6.6 Passing Arrays to Methods 6.27 When you pass an array to a method, the method receives __________. A. a copy of the array B. a copy of the first element C. the reference of the array D. the length of the array

C

Section 6.7 Returning an Array from a Method 6.33 When you return an array from a method, the method returns __________. A. a copy of the array B. a copy of the first element C. the reference of the array D. the length of the array

C

What is the output for list1? 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. 1 2 3 B. 1 1 1 C. 0 1 2 D. 0 1 3

C

What is the output for list2? 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. 1 2 3 B. 1 1 1 C. 0 1 2 D. 0 1 3

C

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 has a compile error because new int[2] is assigned to a. B. The program has a runtime error because a[1] is not initialized. C. The program displays a[1] is 0. D. The program displays a[1] is 1.

C Explanation: After executing the statement a = new int[2], a refers to int[2]. The default value for a[0] and a[1] is 0.

6.3 Which of the following is incorrect? A. int[] a = new int[2]; B. int a[] = new int[2]; C. int[] a = new int(2); D. int a = new int[2]; E. int a() = new int[2];

C,D,E

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 A. 3 B. 1 C. 1 2 3 D. 1 2

C. 123

Assume int[] t = {1, 2, 3, 4}. What is t.length? A. 0 B. 3 C. 4 D. 5

C. 4

int[] list = new int[5];list = new int[6]; A. The code has compile errors because the variable list cannot be changed once it is assigned. B. The code has runtime errors because the variable list cannot be changed once it is assigned. C. The code can compile and run fine. The second line assigns a new array to list. D. The code has compile errors because you cannot assign a different size array to list.

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

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]); } } A. The program has a compile error because the size of the array wasn't specified when declaring the array. B. The program has a runtime error because the array elements are not initialized. C. The program runs fine and displays x[0] is 0. D. The program has a runtime error because the array element x[0] is not defined.

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

How can you initialize an array of two characters to 'a' and 'b'? A. char[] charArray = new char[2]; charArray = {'a', 'b'}; B. char[2] charArray = {'a', 'b'}; C. char[] charArray = {'a', 'b'}; D. char[] charArray = new char[2]; charArray[0] = 'a'; charArray[1] = 'b';

C. char[] charArray = {'a', 'b'}; D. char[] charArray = new char[2]; charArray[0] = 'a'; charArray[1] = 'b';

What is the correct term for numbers[99]? A. index B. index variable C. indexed variable D. array variable E. array

C. indexed variable

Which of the following are incorrect? A. int[] a = new int[2]; B. int a[] = new int[2]; C. int[] a = new int(2); D. int a = new int[2]; E. int a() = new int[2];

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

6.10 How can you initialize an array of two characters to 'a' and 'b'? A. char[] charArray = new char[2]; charArray = {'a', 'b'}; B. char[2] charArray = {'a', 'b'}; C. char[] charArray = {'a', 'b'}; D. char[] charArray = new char[]{'a', 'b'};

CD

Array initializer

Combines the declaration, creation, and initialization of an array in one statement

6.16 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 2 3 4 5 6 B. 6 1 2 3 4 5 C. 6 2 3 4 5 1 D. 1 1 2 3 4 5 E. 2 3 4 5 6 1

D

6.34 Suppose a method p has the following heading: public static int[] p() What return statement may be used in p()? A. return 1; B. return {1, 2, 3}; C. return int[]{1, 2, 3}; D. return new int[]{1, 2, 3};

D

6.4 If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, the highest index in array list is __________. A. 0 B. 1 C. 2 D. 3 E. 4

D

Section 6.10 Searching Arrays 6.39 For the binarySearch method in Section 6.9.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)? A. low is 0 and high is 6 B. low is 0 and high is 3 C. low is 3 and high is 6 D. low is 4 and high is 6 E. low is 0 and high is 5

D

The __________ method copies the sourceArray to the targetArray. A. System.copyArrays(sourceArray, 0, targetArray, 0, sourceArray.length); B. System.copyarrays(sourceArray, 0, targetArray, 0, sourceArray.length); C. System.arrayCopy(sourceArray, 0, targetArray, 0, sourceArray.length); D. System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length);

D

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 2 3 4 5 6 B. 2 3 4 5 6 6 C. 2 3 4 5 6 1 D. 1 1 1 1 1 1

D

6.28 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++; } } A. 0 0 B. 1 1 C. 2 2 D. 2 1 E. 1 2

D - The correct answer is D Explanation: Invoking increase(x) passes the reference of the array to the method. Invoking increase(y[0]) passes the value 1 to the method. The value y[0] outside the method is not changed.

Analyze the following code: public class Test { public static void main(String argv[]) { System.out.println("argv.length is " + argv.length); } } A. The program has a compile error because String argv[] is wrong and it should be replaced by String[] args. B. The program has a compile error because String argv[] is wrong and it should be replaced by String args[]. C. If you run this program without passing any arguments, the program would have a runtime error because argv is null. D. If you run this program without passing any arguments, the program would display argv.length is 0.

D.

Section 6.9 Variable-Length Argument Lists 6.37 Which of the following declarations are correct? A. public static void print(String... strings, double... numbers) B. public static void print(double... numbers, String name) C. public static double... print(double d1, double d2) D. public static void print(double... numbers) E. public static void print(int n, double... numbers)

DE - The correct answer is DE Explanation: Only one variable-length parameter may be specified in a method and this parameter must be the last parameter. The method return type cannot be a variable-length parameter.

6.11 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]); } } A. The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by {1, 2, 3}. B. The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by new double[3]{1, 2, 3}; C. The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by new double[]{1.0, 2.0, 3.0}; D. The program compiles and runs fine and the output "Value is 1.0" is printed. E. The program compiles and runs fine and the output "Value is 2.0" is printed.

E

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? A. 3.1, 3.1, 2.5, 6.4, 2.1 B. 2.5, 3.1, 3.1, 6.4, 2.1 C. 2.1, 2.5, 3.1, 3.1, 6.4 D. 3.1, 3.1, 2.5, 2.1, 6.4 E. 2.1, 3.1, 2.5, 6.4, 3.1

E

If you declare an array double[] list = new double[5], the highest index in array list is __________. A. 0 B. 1 C. 2 D. 3 E. 4

E. 4

foreach loop

Enables you to traverse the array sequentially without using an index variable

Array

Stores a fixed-size sequential collection of elements of the same type

6.23 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] + " "); } } A. The program displays 1 2 3 4 B. The program displays 0 0 C. The program has a compile error on the statement x = new int[2], because x is final and cannot be changed. D. The elements in the array x cannot be changed, because x is final.

The correct answer is C Explanation: The value stored in x is final, but the values in the array are not final.

6.31 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); } } A. The program has a compile error because xMethod(new double[]{3, 3}) is incorrect. B. The program has a compile error because xMethod(new double[5]) is incorrect. C. The program has a compile error because xMethod(new double[3]{1, 2, 3}) is incorrect. D. The program has a runtime error because a is null.

The correct answer is C Explanation: new double[3]{1, 2, 3} should be replaced by new double[]{1, 2, 3}) (anonymous array).

6.45 Assume int[] scores = {1, 20, 30, 40, 50}, what value does java.util.Arrays.binarySearch(scores, 3) return? A. 0 B. -1 C. 1 D. 2 E. -2

The correct answer is E Explanation: The binarySearch method returns the index of the search key if it is contained in the list. Otherwise, it returns ?insertion point - 1. The insertion point is the point at which the key would be inserted into the list. In this case the insertion point is 1. Note that the array index starts from 0.

Off by one error

When you mistakenly reference the first element in an array with index 1, but it should be 0.

Creating an array after a variable is declared

arrayRefVar = new elementType[arraySize];

Indexed variable syntax

arrayRefVar[index]; myList[9];

What is the syntax for arraycopy?

arraycopy(sourceArray, srsPos, targetArray, tarPos, length);

Declaring, creating and assigning the reference of an array

elementType[] arrayRefVar = new elementType[arraySize]; ex- double[] myList = new double[10];

Array initializer syntax

elementType[] arrayRefVar = {value0, value1, ..., valuek}; ex- double[] myList = {1.9, 2.9, 3.4, 3.5};

Syntax for declaring an array

elementType[] arrayRefVar; ex- double[] myList;

Give an example on how to display all elements using a foreach loop

for(double e: myList){ System.out.println(e);{

What are the default values

numeric primitive data type = 0 char = \u0000 boolean = false

A return statement without any value can be used in ________. a non-void method void method

void method


Related study sets

History of Rock 'n Roll Final Study Guide

View Set

ORDER: 1-55 (TEST 1), 56-115 (TEST 2),

View Set

SpeakOut Pre-intermediate Unit 5 Travel

View Set

Chapter 8-Foreign Direct Investment

View Set