quiz 6

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What is the valid range of index values for an array of size 10?

0 to 9

Consider the following code snippet: int[][] numarray = { { 3, 2, 3 }, { 0, 0, 0 } }; System.out.print(numarray[0][0]); System.out.print(numarray[1][0]); What is the output of the given code snippet?

30

How many elements can be stored in a two-dimensional 5 by 6 array?

30

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

3040

Consider the following code snippet: int[][] arr = { { 1, 2, 3, 0 }, { 4, 5, 6, 0 }, { 0, 0, 0, 0 } }; int[][] arr2 = arr; System.out.println(arr2[2][1] + arr2[1][2]); What is the output of the given code snippet on execution?

6

What is the output of the code snippet below? int[][] numarray = { { 8, 7, 6 }, { 0, 0, 0 } }; System.out.print(numarray[0][0]); System.out.print(numarray[1][0]);

80

Consider the following code snippet: int[][] arr = { { 1, 2, 3 }, { 4, 5, 6 } }; int val = arr[0][2] + arr[1][2]; System.out.println(val); What is the output of the given code snippet on execution?

9

Which one of the following statements is true about passing arrays to a method?

By default, arrays are passed by reference to a method.

What is the purpose of this algorithm? double total = 0; for (double element : data) { total = total + element; }

Computing the sum.

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.

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.

II and III only

Consider the telephone book as a physical object that can help understand algorithms. What kind of algorithm might be visualized using it?

Searching

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.toString(data);

What is the purpose of this algorithm, assuming data is a non-empty array and both j and k are valid positions in the array? public void mystery(int[] data, int j, int k) { int t = data[j]; data[j] = data[k]; data[k] = t; }

Swapping elements of an array.

Consider the following code snippet: int[][] arr = { { 13, 23, 33 }, { 14, 24, 34 } }; Identify the appropriate statement to display the value 24 from the given array?

System.out.println(arr[1][1]);

Which one of the following statements is correct for displaying the value in the second row and the third column of a two-dimensional, size 3 by 4 array?

System.out.println(arr[1][2]);

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

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

What is the error in this array code fragment? double[] data; data[0] = 15.25;

The array referenced by data has not been initialized.

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

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

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

Consider the following code snippet: int val = arr[0][2]; Which value of arr is stored in the val variable?

The value in the first row and the third column

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 initialized array.

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?

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?

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

Which code snippet finds the largest value in an array that is only partially full, assuming currentSize is the companion variable indicating the actual number of elements in the array??

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

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); } Question options:

for (String str : arr)

Which code snippet prints out the elements in a partially filled array of integers, assuming currentSize is the companion variable indicating the actual number of elements in the array? wrong

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

Fill in the blank for this algorithm for filling in initial values for a data array. for (int i = 0; __________; i++) { data[i] = i; } wrong

i < data.length()

Which code snippet calculates the sum of all the elements in even positions in an array?

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?

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

Which code correctly swaps elements in a non-empty data array for valid positions j and k?

int t = data[j]; data[j] = data[k]; data[k] = t;

Identify the correct statement for defining an integer array named numarray of ten elements.

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

Which one of the following is the correct definition for initializing data in a two-dimensional array of three rows and two columns?

int[][] arr = { { 1, 1 }, { 2, 2 }, { 3, 3 } };

Which one of the following statements is the correct definition for a two-dimensional array of 20 rows and 2 columns of the type integer?

int[][] num = new int[20][2];

The enhanced for loop

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?

maintain a companion variable that stores the current number of elements

When an array reading and storing input runs out of space,

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

Fill in the blank for this algorithm for computing the average of values in a data array, assuming the array is not empty. double total = 0; for (double element : data) { total = total + element; } double average = ____________________; wrong

total / data.length() - 1


Set pelajaran terkait

ECON 201- EXAM 1 WVU: Multiple Choice

View Set

Day 15 Count 1 to 10, 1 to 20 and 1 to 100

View Set

SOC 1113 EXAM 3 - CH. 13-18 Quizzes

View Set

Cumulative Exam Review 9th Grade

View Set