Chapter 7 Study Guide AOIT MASSY

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

The statement that is correct about declaring an array list as a method parameter is:

An array list value can be modified inside the method.

When reading inputs, why might it be necessary to "grow" an array?

The number of inputs may not be known in advance.

In a partially filled array, how would you determine the number of slots in the array that are not currently used?

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

The following statement gets an element from position 4 in an array: x = a[4]; What is the equivalent operation using an array list?

x = a.get(4);

Assume the following variable has been declared and given a value as shown: int[][] data = { {9, 17, -4, 21 }, {15, 24, 0, 9}, {6, 2, -56, 8}, }; Which is the value of data[1][2]?

0

What will be printed by the statements below? int[] values = { 1, 2, 3, 4}; values[2] = 24; values[values[0]] = 86; for (int i = 0; i < values.length; i++) System.out.print (values[i] + " ");

1 86 24 4

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

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

3040

Consider the following code snippet: int cnt = 0; int[][] numarray = new int[2][3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { numarray[j][i] = cnt; cnt++; } } What is the value of numarray[1][2] after the code snippet is executed?

5

How many elements can be stored in an array of dimension 2 by 3?

6

Consider the following code snippet: String[] data = { "123", "ghi", "jkl", "def", "%&*" }; Which statement sorts the data array in ascending order?

Arrays.sort(data);

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 the order of the elements is unimportant, what is the most efficient way to remove an element from an array?

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

Consider the following code snippet: int[][] arr = { { 13, 23, 33 }, { 14, 24, 34 } }; Write 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]);

What is the result of 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 a run-time 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.

Based on the code snippet below, what is the final size of name & friends? 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 3; the final size of friends is 3

Suppose you wish to use an array to solve a new problem. What is the first step to take in finding a solution?

decompose the problem into steps

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

for (String str : arr)

Write a code snippet for calculating the largest value in an integer array list aList

int max = aList.get(0); for (int count = 1; count < aList.size(); count++) { if (aList.get(count) > max) { max = aList.get(count); } }

Write a 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 = i + 2) { sum += values[i]; }

Write a statement for defining an integer array named numarray of ten elements.

int[] numarray = new int[10];

Consider using a deck of cards as a way to visualize a shuffle algorithm. When two cards shuffle their position, what has to happen to the size of the array holding them?

it stays the same

Assume the variable numbers has been declared to be an array that has at least one element. Write a single line of code that represents the last element in numbers.

numbers[numbers.length - 1]

Suppose you wish to process an array of values and eliminate any potential duplicate values stored in the array. Which array algorithm might be adapted for this?

remove an element

What was Babbage's machine for automatically producing printed tables called?

the difference engine

What is the enhanced for loop convenient for?

traversing all elements in an array

Assume the array of integers values has been created. Which condition must be used in the indicated area so the loop below will assign max the largest value in values? int max = values[0]; for (int val: values) { if (/* Put condition here */) max = val; }

val > max


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

The Iroquois Creation Myth: "The World on Turtle's Back"

View Set

Chapter 24: Trauma Overview Practice Questions

View Set