CPSC 1110 Arrays

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

Select the statement that reveals the logic error in the following method. public static double minimum(double[] data) { double smallest = 0.0; for (int i = 0; i < data.length; i++) { if (data[i] < smallest) { smallest = data[i]; } } return smallest; } double m = minimum(new double[] { 1.2, -3.5, 6.6, 2.4 }); double m = minimum(new double[] { -10.2, 3.5, 62.6, 21.4 }); double m = minimum(new double[] { 1.2, 23.5, 16.6, -23.4 }); double m = minimum(new double[] { 12.2, 31.5, 6.6, 2.4 });

double m = minimum(new double[] { 12.2, 31.5, 6.6, 2.4 });

Complete the following code snippet with the correct enhanced for loop so it iterates over the array without using an index variable. String[] arr = { "abc", "def", "ghi", "jkl" }; ___________________ { System.out.print(str); } for (str : String arr) for (str[] : arr) for (arr[] : str) for (String str : arr)

for (String str : arr)

Which one of the following snippets finds the value of the bottom neighbor of a valid position j, k in the two-dimensional integer array data2D with dimensions ROWS and COLUMNS, yielding a value of -1 if it does not exist? int bottomNeighbor = -1; // choose snippet to find bottom neighbor bottomNeighbor = data2D[j, k+1]; if (j < ROWS - 1) { bottomNeighbor = data2D[j+1, k]; } if (k < ROWS - 1) { bottomNeighbor = data2D[j, k+1]; } bottomNeighbor = data2D[j+1, k];

if (j < ROWS - 1) { bottomNeighbor = data2D[j+1, k]; }

Which one of the following is the correct code snippet for calculating the largest value in an integer array list arrList of size 10? int maximum = arrList.get(0); for (int counter = 1; counter > arrList.size(); counter++) { if (arrList.get(counter) < maximum) { maximum = arrList.get(counter); } } int maximum = 0; for (int counter = 1; counter < arrList.size(); counter++) { if (arrList.get(counter) > maximum) { maximum = arrList.get(counter); } } int maximum = arrList.get(1); for (int counter = 1; counter < arrList.size(); counter++) { if (arrList.get(counter) > maximum) { maximum = arrList.get(counter); } } int maximum = arrList.get(0); for (int counter = 1; counter < arrList.size(); counter++) { if (arrList.get(counter) > maximum) { maximum = arrList.get(counter); } }

int maximum = arrList.get(0); for (int counter = 1; counter < arrList.size(); counter++) { if (arrList.get(counter) > maximum) { maximum = arrList.get(counter); } }

Identify the correct statement for defining an integer array named numarray of ten elements. int numarray[10]; int[] numarray = new int[10]; int[10] numarray; int[] numarray = new int[9];

int[] numarray = new int[10];

Suppose you wish to write a method that returns the sum of the elements in the partially filled array myArray. Which is a reasonable method header? public static int sum() public static int sum(int currSize) public static int sum(int[] values, int currSize) public static int sum(int[] values)

public static int sum(int[] values, int currSize)

Which one of the following is a valid signature of a method with an integer two-dimensional array parameter of size 10 x 10? public static void func(int[10][10] arr) public static void func(int[][10] arr) public static void func(int[][] arr) public static void func(int[10][] arr)

public static void func(int[][] arr)

Which one of the following is a correct declaration for a method named passAList with the array list num of size 5 as a parameter? public static void passAList(ArrayList<Integer> num) public static void passAList(num) public static void passAList(ArrayList<Integer> num[5]) public static void passAList(ArrayList<Integer> num, 5)

public static void passAList(ArrayList<Integer> num)

Suppose you wish to process an array of values and eliminate any potential duplicate values stored in the array. Which array algorithms might be adapted for this? remove an element find the minimum add an element calculate the sum of the elements

remove an element

What is the valid range of index values for an array of size 10? 0 to 10 1 to 10 1 to 9 0 to 9

0 to 9

Consider the following code snippet. What does the array contain at the end of the program? public static fillWithRandomNumbers(double[] values) { double[] numbers = new double[values.length]; for (int i = 0; i < numbers.length; i++) { numbers[i] = Math.random(); } values = numbers; } public static void main(String[] args) { double[] num = new double[20]; fillWithRandomNumbers(num); } Undefined data due to compilation error 20 zeros because array num is not changed by method Array index bound error 20 random numbers

20 zeros because array num is not changed by method

What is the output of the following code snippet? int[] myarray = { 10, 20, 30, 40, 50 }; System.out.print(myarray[2]); System.out.print(myarray[3]); 1050 2030 3040 4050

3040

What is the output of the code snippet below? int[][] arr = { { 1, 2, 3, 0 }, { 4, 5, 6, 0 }, { 0, 0, 0, 0 } }; int val = arr[1][2] + arr[1][3]; System.out.println(val); 6 9 7 5

6

Which one of the following code snippets accepts the integer input in an array named num1 and stores the odd integers of num1 in another array named oddnum? ArrayList<Integer> num1 = new ArrayList<Integer>(); ArrayList<Integer> oddnum = new ArrayList<Integer>(); int data; Scanner in = new Scanner(System.in); for (int i = 0; i < 10; i++) { data = in.nextInt(); num1.add(data); if (num1.get(i) % 2 != 0) { oddnum.add(num1.get(i)); } } ArrayList<Integer> num1 = new ArrayList<Integer>(); ArrayList<Integer> oddnum = new ArrayList<Integer>(); int data; Scanner in = new Scanner(System.in); for (int i = 0; i < 10; i++) { data = in.nextInt(); num1.add(data); if (num1.get(i) % 2 == 0) { oddnum.add(num1.get(i)); } } ArrayList<Integer> num1; ArrayList<Integer> oddnum = new ArrayList<Integer>(); int data; Scanner in = new Scanner(System.in); for (int i = 0; i < 10; i++) { data = in.nextInt(); num1.add(data); if (num1[i] % 2 != 0) { oddnum.add(num1[i]); } } ArrayList<Integer> num1 = new ArrayList<Integer>(); ArrayList<Integer> oddnum = new ArrayList<Integer>(); int data; Scanner in = new Scanner(System.in); for (int i = 0; i < 10; i++) { data = in.nextInt(); num1.add(data); if (num1.get(i) % 2 == 0) { oddnum.add(num1[i]); } }

ArrayList<Integer> num1 = new ArrayList<Integer>(); ArrayList<Integer> oddnum = new ArrayList<Integer>(); int data; Scanner in = new Scanner(System.in); for (int i = 0; i < 10; i++) { data = in.nextInt(); num1.add(data); if (num1.get(i) % 2 != 0) { oddnum.add(num1.get(i)); } }

Which statements are true regarding the differences between arrays and array lists? I. Arrays are better if the size of a collection will not change. II. Array lists are more efficient than arrays. III. Array lists are easier to use than arrays. I, II, and III II and III only I and II only I and III only

I and III only

When a Java program terminates and reports an exception, the error message contains which pieces of useful information? I. The compile and revision control history of the source code changes that caused the error II. The name of the exception that occurred III. The stack trace I, II, and III II and III only I and II only I and III only

II and III only

Which statements about the enhanced for loop are true? I. It is suitable for all array algorithms. II. It does not allow the contents of the array to be modified. III. It does not require the use of an index variable. I and III only II and III only I and II only I, II, and III

II and III only

Which statements are true about array references? I. Assigning an array reference to another creates a second copy of the array. II. An array reference specifies the location of an array. III. Two array references can reference the same array. I only II and III only III only II only

II and III only

Why is the following method header invalid? public static int[5] meth(int[] arr, int[] num) Methods cannot have an array as a parameter variable. Methods cannot have two array parameter variables. Methods cannot have an array as a return type. Methods that return an array cannot specify its size.

Methods that return an array cannot specify its size.

What is the purpose of this algorithm? for (int i = 0; i < names.length; i++) { if (i > 0) { System.out.print("; "); } System.out.print(names[i]); } Appends a comma to the end of each name in an array and prints it. Appends a semicolon to the end of each name in an array and prints it. Prints out the names in an array separated by commas. Prints out the names in an array separated by semicolons.

Prints out the names in an array separated by semicolons.

Consider the following code snippet. Which statement should be used to fill in the empty line so that the output will be [32, 54, 67.5, 29, 35]? public static void main(String[] args) { double data[] = {32, 54, 67.5, 29, 35}; ______________ System.out.println(str); } String str = Arrays.format(data); String str = Arrays.toString(data); String str = str + ", " + data[i]; String str = data.toString();

String str = Arrays.toString(data);

What is the result of the following code? for (double element : values) { element = 0; } The elements of the array element are initialized to zero. The elements of the array values are initialized to zero. The first element of the array values is initialized to zero. The array values is not modified.

The array values is not modified.

Which statement is true about the code snippet below? public static void main(String[] args) { Scanner in = new Scanner(System.in); ArrayList<Double> inputs = new ArrayList<Double>(); int ind = 0; while (in.hasNextDouble()) { inputs.set(ind, in.nextDouble()); ind++; } } The code has compile-time error. The code has a run-time error. The array list is full after 100 numbers are entered. The code adds all input numbers to the array list.

The code has a run-time error.

Consider the following code snippet, where the array lists contain elements that are stored in ascending order: ArrayList<Integer> list1 = new ArrayList<Integer>(); ArrayList<Integer> list2 = new ArrayList<Integer>(); . . . int count = 0; for (int i = 0; i < list1.size() && i < list2.size(); i++) { if (list1.get(i) == list2.get(i)) { count++; } } Which one of the following descriptions is correct for the given code snippet? The code snippet compares the values of two array lists and stores the count of total matches found. The code snippet finds the highest value out of the two array lists. The code snippet finds the lowest value out of the two array lists. The code snippet adds the values of the two array lists.

The code snippet compares the values of two array lists and stores the count of total matches found.

Which statement is true about the code snippet below? ArrayList<String> names = new ArrayList<String>(); names.add("John"); names.add("Jerry"); ArrayList<String> friends = names; friends.add("Harry"); The final size of names is 2; the final size of friends is 3 Compilation error The final size of names is 3; the final size of friends is 2 The final size of names is 3; the final size of friends is 3

The final size of names is 3; the final size of friends is 3

If a programmer confuses the method required for checking the length of a string and uses size() instead of length(), what will happen? The program will run but will produce an uncertain result. The program will not compile. The program will crash. The compiler will automatically correct the error.

The program will not compile.

What is the result of the following definition of an array list? ArrayList<Double> points; The statement creates an array list of size 0. The statement defines an array list reference but leaves its value as null. The statement causes a compile time error as the size of the array list is not defined. The statement creates an array list with unlimited size.

The statement defines an array list reference but leaves its value as null.

What statement is true about the following code? int[] values = {2, 3, 5, 8, 11}; int[] data = values; data[4] = 13; The variable data references an uninitialized array. The code has a compile-time error. The last value in the values array is 11. The variable data references an initialized array.

The variable data references an initialized array.

What is the output of the following code snippet? public static void main(String[] args) { String[] arr = { "aaa", "bbb", "ccc" }; mystery(arr); System.out.println(arr[0] + " " + arr.length); } public static void mystery(String[] arr) { arr = new String[5]; arr[0] = "ddd"; } aaa 5 ddd 3 aaa 3 ddd 5

aaa 3


Ensembles d'études connexes

¡Ay Chihuahua! ¡Hay tantas culturas en México!

View Set

Stats Exam 1 Online Assessments Quiz

View Set

Programmatic Adv Certificate Quizes TradeDesk

View Set

Module 62: What Is Aggregate Demand? What Is Aggregate Supply?

View Set

NRN171 Quiz 2 Clinical Decision making and judgement

View Set

Psych Test study guide 11 - 31 - 17

View Set