CSE110- Chap 6 Quiz (Arrays) (quiz #1)

¡Supera tus tareas y exámenes ahora con Quizwiz!

Consider the following code snippet in Java 6 or later: String[] data = { "abc", "def", "ghi", "jkl" }; String[] data2 = Arrays.copyOf(data, data.length - 1); What does the last element of data2 contain? a) "abc" b) "def" c) "ghi" d) "jkl"

"ghi"

What is the valid range of index values for an array of size 10? a) 0 to 10 b) 1 to 9 c) 1 to 10 d) 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); } a) 20 random numbers b) Undefined data due to compilation error c) 20 zeros because array num is not changed by method d) Array index bound error

20 zeros because array num is not changed by method

What is the output of the given code snippet? int[] mynum = new int[5]; for (int i = 1; i < 5; i++) { mynum[i] = i + 1; System.out.print(mynum[i]); } a) 2345 b) 1234 c) 1345 d) 1111

2345

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]); a) 1050 b) 2030 c) 3040 d) 4050

3040

) Consider the following code snippet: String[] data = { "123", "ghi", "jkl", "def", "%&*" }; Which statement sorts the data array in ascending order? a) data = Arrays.sort(); b) Arrays.sort(data); c) data = Arrays.sort(data.length); d) data = Arrays.sort(data);

Arrays.sort(data);

Which one of the following statements is true about passing arrays to a method? a) By default, arrays are passed by value to a method b) Arrays, when updated in a called method, are not reflected to the calling method c) By default, arrays are passed by reference to a method d) Arrays are passed only if size is specified as another argument

By default, arrays are passed by reference to a method

Which statements are true about the buffer overrun attack launched over the Internet in 1988? I. The buffer overrun exploited a program that was written in C running on the Unix operating system II. The Java programming language generates a run-time exception when buffer overrun occurs III. In recent years computer attacks have lessened a) I, II b) I, III c) II, III d) I, II, III

I, II

Which statements about array algorithms are true? I. The array algorithms are building blocks for many programs that process arrays II. Java contains ready-made array algorithms for every problem situation III. It is inefficient to make multiple passes through an array if you can do everything in one pass a) I, II b) I, III c) II, III d) I, II, III

I, III

) 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 a) I, II b) I, III c) II, III d) I, II, III

II, III

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 a) I, II b) I, III c) II, III d) I, II, III

II, III

Which one of the following statements is correct for the given code snippet? int[] number = new int[3]; // Line 1 number[3] = 5; // Line 2 a) Line 2 declares and initializes an array of three elements with value 5. b) Line 2 causes a bounds error because 3 is an invalid index number. c) Line 2 initializes the third element of the array with value 5. d) Line 2 initializes all the elements of the array with value 5.

Line 2 causes a bounds error because 3 is an invalid index number

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

Methods that return an array cannot specify its size

When the order of the elements is unimportant, what is the most efficient way to remove an element from an array? a) Delete the element and move each element after that one to a lower index. b) Replace the element to be deleted with the last element in the array. c) Replace the element to be deleted with the first element in the array. d) Replace the element with the next element.

Replace the element to be deleted with the last element in the array.

Consider the following code snippet. Which statement should be used to fill in the empty line so that provides 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); } a) String str = Arrays.format(data); b) String str = data.toString(); c) String str = Arrays.toString(data); d) String str = str + ", " + data[i];

String str = Arrays.toString(data);

Consider the following line of code: int[] somearray = new int[30]; Which one of the following options is a valid line of code for displaying the twenty-eighth element of somearray? a) System.out.println(somearray[28]); b) System.out.println(somearray(28)); c) System.out.println(somearray(27)); d) System.out.println(somearray[27]);

System.out.println(somearray[27]);

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

The array values is not modified

What is true about the following code snippet? public static double[] fillWithRandomNumbers(double[] values) { double[] numbers = new double[values.length]; for (int i = 0; i < numbers.length; i++) { numbers[i] = Math.random(); } return Arrays.copyOf(numbers, numbers.length); } public static void main(String[] args) { double[] num = new double[20]; num = fillWithRandomNumbers(num); } a) The code has a run-time error b) The code has a compilation error c) The code has a logic error d) The code is inefficient

The code is inefficient

What is displayed after executing the given code snippet? int[] mymarks = new int[10]; int total = 0; Scanner in = new Scanner(System.in); for (int cnt = 1; cnt <= 10; cnt++) { System.out.print("Enter the marks: "); mymarks[cnt] = in.nextInt(); total = total + mymarks[cnt]; } System.out.println(total); a) The code snippet displays the total marks of all ten subjects. b) The for loop causes a run-time time error on the first iteration. c) The code snippet causes a bounds error. d) The code snippet displays zero.

The code snippet causes a bounds error.

What is the result of executing this code snippet? int[] marks = { 90, 45, 67 }; for (int i = 0; i <= 3; i++) { System.out.println(marks[i]); } a) The code snippet does not give any output. b) The code snippet displays all the marks stored in the array without any redundancy. c) The code snippet causes a bounds error. d) The code snippet executes an infinite loop.

The code snippet causes a bounds error.

Which one of the following statements is correct about the given code snippet? int[] somearray = new int[6]; for (int i = 1; i < 6; i++) { somearray[i] = i + 1; } a) The for loop initializes all the elements of the array. b) The for loop initializes all the elements except the first element. c) The for loop initializes all the elements except the last element. d) The for loop initializes all the elements except the first and last elements.

The for loop initializes all the elements except the first element.

Consider the following method: public static int mystery(int length, int n) { int[] result = new int[length]; for (int i = 0; i < result.length; i++) { result[i] = (int) (n * Math.random()); } return result; } Which statement is correct about the code? a) The method works perfectly b) The method returns a random number c) The method return type should be changed to int[] d) The method has a bounds error when the array size is too large

The method return type should be changed to int[]

Is there any thing wrong with the following code snippet? String[] data = { "abc", "def", "ghi", "jkl" }; String searchedValue = "ghi"; int pos = 0; boolean found = false; while (pos < data.length) { if (data[pos].equals(searchedValue)) { found = true; } else { found = false; } pos++; } if (found) { System.out.println("Found at position: " + pos); } else { System.out.println("Not found"); } a) There is nothing wrong. b) There is compile-time error. c) There is a bounds error. d) There is a logic error.

There is a logic error.

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"; } a) ddd 5 b) ddd 3 c) aaa 5 d) aaa 3

aaa 3

Consider the following code snippet: String[] data = { "abc", "def", "ghi", "jkl" }; Using Java 6 or later, which statement grows the data array to twice its size? a) data = Arrays.copyOf(data, 2 * data.length); b) data = Arrays.copyOf(data, 2); c) data = Arrays.copyOf(data, 2 * data); d) data = Arrays.copyOf(data, 2 * data.size());

data = Arrays.copyOf(data, 2 * data.length);

Consider the following code snippet: String[] data = { "abc", "def", "ghi", "jkl" }; String [] data2; In Java 6 and later, which statement copies the data array to the data2 array? a) data2 = Arrays.copyOf(data, data2.length); b) data2 = Arrays.copyOf(data, data.length); c) data2 = Arrays.copyOf(data, data.size()); d) data2 = Arrays.copyOf(data);

data2 = Arrays.copyOf(data, data.length);

Suppose you wish to use an array to solve a new problem. What is the first step to take in finding a solution? a) structure a program using methods b) adapt a built-in array algorithm c) decompose the problem into steps d) assemble a test program and run it

decompose the problem into steps

Which code snippet finds the largest value in an array that is only partially full? a) double largest = values[0]; for (int i = 1; i < values.length; i++) { if (values[i] > largest) { largest = values[i]; } } b) double largest = values[0]; for (int i = 1; i < values.length; i++) { if (values[i] < largest) { largest = values[i]; } } c) double largest = values[0]; for (int i = 1; i < currSize; i++) { if (values[i] > largest) { largest = values[i]; } } d) double largest = values[0]; for (int i = 1; i < currSize; i++) { if (values[i] < largest) { largest = values[i]; } }

double largest = values[0]; for (int i = 1; i < currSize; i++) { if (values[i] > largest) { largest = values[i]; } }

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; } a) double m = minimum(new double[] { 1.2, -3.5, 6.6, 2.4 }); b) double m = minimum(new double[] { 1.2, 23.5, 16.6, -23.4 }); c) double m = minimum(new double[] { -10.2, 3.5, 62.6, 21.4 }); d) 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); } a) for (String str : arr) b) for (str : String arr) c) for (str[] : arr) d) for (arr[] : str)

for (String str : arr)

Which code snippet prints out the elements in a partially filled array of integers? a) for (int i = 0; i < myArray.length(); i++) { System.out.print(myArray[i]); } b) for (int i = 0; i < myArray.currLength(); i++) { System.out.print(myArray[i]); } c) for (int i = 0; i < currLength; i++) { System.out.print(myArray[i]); } d) for (int i = 0; i < myArray.length(); i++) { System.out.print(myArray[currLength]); }

for (int i = 0; i < currLength; i++) { System.out.print(myArray[i]); }

Which code snippet calculates the sum of all the elements in even positions in an array? a) int sum = 0; for (int i = 1; i < values.length; i+=2) { sum++; } b) int sum = 0; for (int i = 0; i < values.length; i++) { sum++; } c) int sum = 0; for (int i = 0; i < values.length; i++) { sum += values[i]; } d) int sum = 0; for (int i = 0; i < values.length; i += 2) { sum += values[i]; }

int sum = 0; for (int i = 0; i < values.length; i += 2) { sum += values[i]; }

Which code snippet calculates the sum of all the even elements in an array values? a) int sum = 0; for (int i = 0; i < values.length; i++) { if ((values[i] % 2) == 0) { sum += values[i]; } } b) int sum = 0; for (int i = 0; i < values.length; i++) { if ((values[i] % 2) == 0) { sum++; } } c) int sum = 0; for (int i = 0; i < values.length; i++) { if ((values[i] / 2) == 0) { sum += values[i]; } } d) int sum = 0; for (int i = 0; i < values.length; i++) { if ((values[i] / 2) == 0) { sum++; } }

int sum = 0; for (int i = 0; i < values.length; i++) { if ((values[i] % 2) == 0) { sum += values[i]; } }

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

int[] numarray = new int[10];

Which one of the following statements is a valid initialization of an array named somearray of ten elements? a) int[] somearray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; b) int somearray[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; c) int[10] somearray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; d) int somearray[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

int[] somearray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

The enhanced for loop a) is convenient for traversing all elements in an array b) is convenient for traversing elements in a partially filled array c) is only used for arrays of integers d) is used to initialize all array elements to a common value

is convenient for traversing all elements in an array

When an array myArray is only partially filled, how can the programmer keep track of the current number of elements? a) access myArray.length() b) maintain a companion variable that stores the current number of elements c) access myArray.currentElements() d) access myArray.length() - 1

maintain a companion variable that stores the current number of elements

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? a) public static int sum(int[] values) b) public static int sum() c) public static int sum(int[] values, int currSize) d) public static int sum(int[] values, int size, int currSize)

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

Which one of the following is the correct header for a method named arrMeth that is called like this: arrMeth(intArray); // intArray is an integer array of size 3 a) public static void arrMeth(int[] ar, int n) b) public static void arrMeth(r[], n) c) public static void arrMeth(int n , int[] ar) d) public static void arrMeth(int[] ar)

public static void arrMeth(int[] ar)

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? a) find the minimum b) remove an element c) add an element d) calculate the sum of the elements

remove an element

When an array reading and storing input runs out of space a) the program must be recompiled with a bigger size for the array. b) the array must be "grown" using the growArray method. c) it automatically resizes to accommodate new elements. d) the array must be "grown" using the new command and the copyOf method.

the array must be "grown" using the new command and the copyOf method.

The binary search is faster than the linear search, providing a) the size of the array is a power of two. b) the elements of the array are ordered. c) the elements of the array are unordered. d) the element being searched for is actually in the array.

the elements of the array are ordered.

) In a partially filled array, the number of slots in the array that are not currently used is a) the length of the array minus the number of elements currently in the array b) the number of elements currently in the array minus the length of the array c) the length of the array plus the number of elements currently in the array d) the number of elements currently in the array

the length of the array minus the number of elements currently in the array

It may be necessary to "grow" an array when reading inputs because a) the number of inputs may not be known in advance. b) arrays in Java must be resized after every 100 elements. c) arrays are based on powers of two. d) the only alternative is a bounds exception.

the number of inputs may not be known in advance.


Conjuntos de estudio relacionados

PSYC 3083 Counseling Psych Exam 1

View Set

Ch. 10 Adaptations to Resistance Training

View Set

Chapter 4 Estates and Interests in Real Property Vocabulary

View Set

Is the sentence simple, compound, complex, or compound-complex? IXL

View Set