Arrays Practice MCQ

Ace your homework & exams now with Quizwiz!

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

D

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

D

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

D

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] + " "); } } A. 1 2 3 4 B. 1 3 8 12 C. 2 5 9 13 D. 4 5 6 7 E. 3 6 10 14

D

Assume int[][] x = {{1, 2}, {3, 4}, {5, 6}}, what are x.length are x[0].length? A. 2 and 1 B. 2 and 2 C. 2 and 3 D. 3 and 3 E. 3 and 2

E

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'; E. Both c and d.

E

Use the increasing selectionSort method presented in this module 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.1, 2.5, 3.1, 3.1, 6.4 C. 3.1, 3.1, 2.5, 2.1, 6.4 D. 2.1, 3.1, 2.5, 6.4, 3.1 E. 2.5, 3.1, 3.1, 6.4, 2.1

E

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

E

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 row = 0; row < values.length; row++) for (int column = 0; column < values[row].length; column++) if (v < values[row][column]) v = values[row][column]; System.out.print(v); } A. 1 B. 3 C. 5 D. 6 E. 33

E

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. B. The program runs fine and displays x[0] is 0. C. The program has a runtime error because the array elements are not initialized. D. The program has a runtime error because the array element x[0] is not defined.

B

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

B

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

B

Assume double[][] x = new double[4][5], what are x.length and x[2].length? A. 4 and 4 B. 4 and 5 C. 5 and 4 D. 5 and 5

B

How many elements are in array matrix (int[][] matrix = new int[5][5])? A. 30 B. 25 C. 20 D. 14

B

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

B

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++) { System.out.print(m(values[row]) + " "); } } public static int m(int[] list) { int v = list[0]; for (int i = 1; i < list.length; i++) if (v < list[i]) v = list[i]; return v; } } A. 3 33 B. 5 33 C. 1 1 D. 33 5 E. 5 6

B

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

B

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

B

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

D

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

D

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

D

When you pass an array to a method, the method receives _________. A. a copy of the first element B. the length of the array C. a copy of the array D. the reference of the array

D

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

A

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}; for (int i = 0; i < list.length; i++) System.out.print(list[i] + " "); } } 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

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

A

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

A

What is the index variable for the element at the first row and first column in array a? A. a[0][0] B. a[1][0] C. a[0][1] D. a[1][1]

A

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 compiles and runs fine and the output "Value is 2.0" is printed. 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[]{1.0, 2.0, 3.0}; 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[3]{1, 2, 3}; D. 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}. E. The program compiles and runs fine and the output "Value is 1.0" is printed.

A

Which of the following statements are correct? A. char[][] charArray = {'a', 'b'}; B. char[][] charArray = {{'a', 'b'}, {'c', 'd'}}; C. char[2][] charArray = {{'a', 'b'}, {'c', 'd'}}; D. char[2][2] charArray = {{'a', 'b'}, {'c', 'd'}};

B

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

B. 2, 3, and 4

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

C

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

C

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

C

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

C

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

C

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

C

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

C

What is the output of the following code? public class Test { 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[i][1] + " "); } } A. 1 2 3 4 B. 4 5 6 7 C. 2 5 9 13 D. 3 6 10 14 E. 1 3 8 12

C

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

C. -(insertion point + 1)


Related study sets

CH 37 Drugs for Peptic Ulcer Disease and Hyperacidity (E2)

View Set

ASVAB - Arithmetic Reasoning/Mathematics Knowledge

View Set

Chapter 14-15 Worksheet: True & False

View Set

Nutrition in Adolescence, the Adult Years and Aging

View Set

PFLAG National Glossary of Terms

View Set

Microeconomics Chap 19 (self-test)

View Set