Quiz 3 (Ch 7,8,9)

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Show the output of the following program when invoked using 1. java Test I have a dream 2. java Test "1 2 3" 3. java Test public class Test { public static void main(String[] args) { System.out.println("Number of strings is " + args.length); for (int i = 0; i < args.length; i++) System.out.println(args[i]); } }

(1) Number of strings is 4 I have a dream (2) Number of strings is 1 1 2 3 (3) Number of strings is 0

Show the output of the following two programs: (a) public class Test { public static void main(String[] args) { int number = 0; int[] numbers = new int[1]; m(number, numbers); System.out.println("number is " + number + " and numbers[0] is " + numbers[0]); } public static void m(int x, int[] y) { x = 3; y[0] = 3; } } (b) 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; } }

(a) number is 0 and numbers[0] is 3 (b) 1 2 3 4 5

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

-(insertion point + 1)

For the linear search method in the text, what is linearSearch(new int[]{1, 2, 14, 7, 3}, 4)?

-1

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

-2

What is java.util.Arrays.binarySearch(new int[]{1, 2, 3, 7, 14}, 5)?

-4

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

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

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

What is the output of the following program? public class Test { public static void main(String[] args) { int[][] values = {{3, 4, 5, 1}, {33, 6, 1, 2}}; int v = values[0][0]; for (int[] list : values) for (int element : list) if (v > element) v = element; System.out.print(v); } }

1

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

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

1 2 3

what is the output of the following code? public class Test { public static void main(String[] args) { int[] x = {1, 2}; int i = 1; m(i, x); System.out.print(i + "," + x[0]); } public static void m(int i, int[] list) { i = 9; list[0] = 9; } }

1,9

What is the output of the following code? public class Test { public static void main(String[] args) { int[] list1 = {1, 2, 3}; int[] list2 = list1; list2[1] = 10; System.out.println(list1[1]); } }

10

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

120 200 14

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

2

Suppose int[][] m = {{4, 5, 6}, {2, 1, 0}}, what is m.length?

2

Show the output of the following code: public class Test { public static void main(String[] args) { int[][] array = {{1, 2, 3, 4}, {5, 6, 7, 8}}; System.out.println(m1(array)[0]); System.out.println(m1(array)[1]); } public static int[] m1(int[][] m) { int[] result = new int[2]; result[0] = m.length; result[1] = m[0].length; return result; } }

2 4

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++; } }

2 1

Assume int[][] x = {{1, 2}, {3, 4, 5}, {5, 6, 5, 9}}, what are x[0].length, x[1].length, and x[2].length?

2, 3, and 4

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

2.0

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?

2.1, 3.1, 2.5, 6.4, 3.1

How many elements are array matrix (int[][] matrix = new int[5][5])?

25

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

3

Suppose int[][] m = {{4, 5, 6}, {2, 1, 0}}, what is m[0].length?

3

What is java.util.Arrays.binarySearch(new int[]{1, 2, 3, 7, 14}, 7)?

3

Assume int[][] x = {{1, 2}, {3, 4}, {5, 6}}, what are x.length are x[0].length?

3 and 2

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

4

For the linear search method in the text, what is linearSearch(new int[]{1, 2, 14, 7, 3}, 3)?

4

What is the output of the following code? public class Test5 { public static void main(String[] args) { int[][] matrix = {{1, 2, 3, 4}, {4, 5, 6, 7}, {8, 9, 10, 11}, {12, 13, 14, 15}}; for (int i = 0; i < 4; i++) System.out.print(matrix[1][i] + " "); } }

4 5 6 7

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

5

Suppose int[][] m = {{4, 5, 6}, {2, 1, 0}}, what is m[0][1]?

5

What is the output of the following code? public class Test { public static void main(String[] args) { int[][] x = {{1, 1}, {1, 1, 1}}; System.out.println(m(x)); } public static int m(int[][] m) { int result = 0; for (int i = 0; i < m.length; i++) for (int j = 0; j < m[i].length; j++) result += m[i][j]; return result; } }

5

Show the output of the following code: int[][] array = {{1, 2}, {3, 4}, {5, 6}}; for (int i = array.length - 1; i >= 0; i--) { for (int j = array[i].length - 1; j >= 0; j--) System.out.print(array[i][j] + " "); System.out.println(); }

6 5 4 3 2 1

What is the output of the following code? public class Test { public static void main(String[] args) { int[][] x = {{2, 1}, {1, 7, 1}}; System.out.println(m(x)); } public static int m(int[][] m) { int result = m[0][1]; for (int i = 0; i < m.length; i++) for (int j = 0; j < m[i].length; j++) if (result < m[i][j]) result = m[i][j]; return result; } }

7

Show the output of the following code: int[][] array = {{1, 2}, {3, 4}, {5, 6}}; int sum = 0; for (int i = 0; i < array.length; i++) sum += array[i][0]; System.out.println(sum);

9

What is the output of the following code? public class Test { public static void main(String[] args) { int[][] x = {{2, 1}, {1, 7, 1}}; System.out.println(m(x[1])); } public static int m(int[] m) { int result = 0; for (int i = 0; i < m.length; i++) result += m[i]; return result; } }

9

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

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

Which of the following statements is valid?

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

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'};

Which of the following is incorrect?

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

Which of the following declarations are correct

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

True or false? When an array is passed to a method, a new array is created and passed to the method.

False. When an array is passed to a method, the reference value of the array is passed. No new array is created. Both argument and parameter point to the same array.

Identify the problems in the following code. public class Test { public static void main(String argv[]) { System.out.println("argv.length is " + argv.length); } }

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

How do you modify the code so that it also displays the highest count and the student with the highest count?

Introduce variabls highestCount and studentNumberOfHighestCount with initial values -1 and -1. Once a new count is obtained for a student, update highestCount and studentNumberOfHighestCount if the new count is larger than highestCount.

Suppose the following code is written to reverse the contents in an array, explain why it is wrong. How do you fix it? int[] list = {1, 2, 3, 5, 4}; for (int i = 0, j = list.length - 1; i < list.length; i++, j--) { // Swap list[i] with list[j] int temp = list[i]; list[i] = list[j]; list[j] = temp; }

It swaps the elements twice. For example, the first element is swapped with the last element once and then the last element is swapped with the first element another time. In effect, the elements are not swapped. Here is the fix: int[] list = {1, 2, 3, 5, 4}; for (int i = 0, j = list.length - 1; i < list.length / 2; i++, j--) { // Swap list[i] with list[j] int temp = list[i]; list[i] = list[j]; list[j] = temp; }

How do you modify the selectionSort method in Listing 7.8 to sort numbers in decreasing order?

Omitted

Use Figure 7.11 as an example to show how to apply the selection-sort approach to sort {3.4, 5, 3, 3.5, 2.2, 1.9, 2}.

Omitted

Use Figure 7.9 as an example to show how to apply the binary search approach to a search for key 10 and key 12 in list {2, 4, 7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79}.

Omitted

What is wrong with each of the following method headers? public static void print(String... strings, double... numbers) public static void print(double... numbers, String name) public static double... print(double d1, double d2)

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.

If high is a very large integer such as the maximum int value 2147483647, (low + high) / 2 may cause overflow. How do you fix it to avoid overflow?

Replace (low + high) / 2 with (-low + high) / 2 + low

Which correctly creates an array of five empty Strings?

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

Use the arraycopy method to copy the following array to a target array t: int[] source = {3, 4, 5};

System.arraycopy(source, 0, t, 0, source.length);

Analyze the following code. int[] list = new int[5]; list = new int[6];

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

If the binary search method returns -4, is the key in the list? Where should the key be inserted if you wish to insert the key into the list?

The key is not in the list. The key should be inserted at -(-4 + 1) = 3. Hide Answer

Can you invoke the printMax method in Listing 7.5 using the following statements? printMax(1, 2, 2, 1, 4); printMax(new double[]{1, 2, 3}); printMax(new int[]{1, 2, 3});

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

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

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

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

The program displays 0 0

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

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

The program displays 1 2 3 4 5.

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

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.

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

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

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

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

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

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

What is the output of the following program? public class Test { public static void main(String[] args) { int[][] values = {{3, 4, 5, 1 }, {33, 6, 1, 2}}; for (int row = 0; row < values.length; row++) { java.util.Arrays.sort(values[row]); for (int column = 0; column < values[row].length; column++) System.out.print(values[row][column] + " "); System.out.println(); } } }

The program prints two rows 1 3 4 5 followed by 1 2 6 33

Analyze the following code: public class Test { public static void main(String[] args) { boolean[][] x = new boolean[3][]; x[0] = new boolean[1]; x[1] = new boolean[2]; x[2] = new boolean[3]; System.out.println("x[2][2] is " + x[2][2]); } }

The program runs and displays x[2][2] is false.

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

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

Once an array is created, its size cannot be changed. Does the following code resize the array? int[] myList; myList = new int[10]; // Sometime later you want to assign a new array to myList myList = new int[20];

The second assignment statement myList = new int[20] creates a new array and assigns its reference to myList.

To apply java.util.Arrays.binarySearch(array, key), should the array be sorted in increasing order, in decreasing order, or neither?

To apply java.util.Arrays.binarySearch(array, key), the array must be sorted in increasing order.

When you create an array using the following statement, the element values are automatically initialized to 0. int[][] matrix = new int[5][5];

True

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

Yes

Can the rows in a two-dimensional array have different lengths?

Yes. They are ragged array.

What types of array can be sorted using the java.util.Arrays.sort method? Does this sort method create a new array?

You can sort an array of any primitive types except boolean. The sort method is void, so it does not return a new array.

What is java.util.Arrays.toString(new int[]{1, 2, 3, 7, 14})?

[1, 2, 3, 7, 14]

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]

Show the output of the following code: int[] list1 = {2, 4, 7, 10}; java.util.Arrays.fill(list1, 7); System.out.println(java.util.Arrays.toString(list1)); int[] list2 = {2, 4, 7, 10}; System.out.println(java.util.Arrays.toString(list2)); System.out.print(java.util.Arrays.equals(list1, list2));

[7, 7, 7, 7] [2, 4, 7, 10] False

What is the index variable for the element at the first row and first column in array a?

a[0][0]

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

a[2]

How can you get the word "abc" in the main method from the following call? java Test "+" 3 "abc" 2

args[2]

What is the output of the following code? int[][] array = new int[5][6]; int[] x = {1, 2}; array[0] = x; System.out.println("array[0][1] is " + array[0][1]);

array[0][1] is 2.

Write a statement to declare a variable named x for a two dimensional array of double values.

double[][]x; or double x[][];

What is java.util.Arrays.equals(new int[]{1, 2, 3, 7, 14}, new int[]{1, 2, 3, 14, 7})?

false

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

What is the correct term for numbers[99]?

indexed variable

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?

int count = args.length;

Declare an array reference variable for a two-dimensional array of int values, create a 4 × 5 int matrix, and assign it to the variable.

int[][] m = new int[4][5];

Which of the following statements are valid? int[][] r = new int[2]; int[] x = new int[]; int[][] y = new int[3][]; int[][] z = {{1, 2}}; int[][] m = {{1, 2}, {2, 3}}; int[][] n = {{1, 2}, {2, 3}, };

int[][] r = new int[2]; Answer: Invalid int[] x = new int[]; Answer: Invalid int[][] y = new int[3][]; Answer: Valid int[][] z = {{1, 2}}; Answer: Valid int[][] m = {{1, 2}, {2, 3}}; Answer: Valid int[][] n = {{1, 2}, {2, 3}, }; Answer: invalid

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

java.util.Arrays.sort(scores)

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

list1 is 1 2 3 4 5 6

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

list1 is 2.5 3.1, 3.1, 6.4

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

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

low is 5 and high is 4

Write an expression for creating a two-dimensional array of the int type with 40 rows and 20 columns.

new int[40][20]

This book text declares the main method as public static void main(String[] args) Can it be replaced by one of the following lines? public static void main(String args[]) public static void main(String[] x) public static void main(String x[]) static void main(String x[])

public static void main(String[] args) can be replaced by public static void main(String args[]) public static void main(String[] x) public static void main(String x[]) but not static void main(String x[]) because it is not public.

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}, {2, 4, 5}};

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

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

the reference of the array

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

the reference of the array


Ensembles d'études connexes

American Revolution Set 2 Marquis De Lafayette — James Armistead

View Set

MICRO ECONOMICS, Chapter 2 (unit 1- obj. 10), Econ 1000 Chapter 3, 2.1-2.4, ECON Chapter 2 Homework, ECN101 Chapter 5 Key Terms, Econ 102 Exam, ECON102 CH. 4, 2.4 Gains from Trade, Microeconomics ch3, ECON E 201 Assignment 3, 1.1 Def. of Econ, ECO 20...

View Set

Ch. 37- Learning: Diabetes Mellitus and Its Complications

View Set

Final Exam Aslp 3025( Practice Quizzes )

View Set