Arrays

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

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

decompose the problem into steps

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); } 20 random numbers Undefined data due to compilation error 20 zeros because array num is not changed by method Array index bound error

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

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

C) By default, arrays are passed by reference to a method

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

C) II, III

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

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

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

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

What is the result of the following code? for (double element : values) { element = 0; } The elements of the array values are initialized to zero. The elements of the array element 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.

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 (String str : arr) for (str : String arr) for (str[] : arr) for (arr[] : str)

for (String str : arr)

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

the number of inputs may not be known in advance.

Identify the correct statement for defining an integer array named numarray of ten elements. int[] numarray = new int[9]; int[] numarray = new int[10]; int[10] numarray; 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? int[] somearray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int somearray[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int[10] somearray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 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 - is convenient for traversing all elements in an array - is convenient for traversing elements in a partially filled array - is only used for arrays of integers - 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? - access myArray.length() - maintain a companion variable that stores the current number of elements - access myArray.currentElements() - 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? public static int sum(int[] values) public static int sum() public static int sum(int[] values, int currSize) 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 public static void arrMeth(int[] ar, int n) public static void arrMeth(r[], n) public static void arrMeth(int n , int[] ar) public static void arrMeth(int[] ar)

public static void arrMeth(int[] ar)

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

B) I, III

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.

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

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

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

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

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

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.

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

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? System.out.println(somearray[28]); System.out.println(somearray(28)); System.out.println(somearray(27)); System.out.println(somearray[27]);

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

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); } The code has a run-time error The code has a compilation error The code has a logic error 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); - The code snippet displays the total marks of all ten subjects. - The for loop causes a run-time time error on the first iteration. - The code snippet causes a bounds error. - The code snippet displays zero.

The code snippet causes a bounds error.


संबंधित स्टडी सेट्स

Earth's Interior and Convection Currents

View Set

Golf Management I Business Planning and Career Enhancement

View Set

Chapter 9: Segmentation, Targeting, and Positioning

View Set