ch. 6 terms

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

Fill in the blank for this algorithm for finding a minimum value in an accounts array. double b = ____________________; for (int i = 1; i < accounts.length; i++) { BankAccount a = accounts[i]; if (a.getBalance() < b) b = a.getBalance(); } return b;

accounts[0].getBalance()

When an integer literal is added to an array list declared as ArrayList<Integer>, Java performs ___________.

auto-boxing

Consider an array list values declared as ArrayList<Double> that has been constructed and populated. What concept does the given code represent? double d = values.get(0);

auto-unboxing

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

Fill in the blank to find the total for the specific column of a two-dimensional integer array data2D with dimensions ROWS and COLUMNS? int total = 0; for (int i = 0; i < ROWS; i++) { total = total + ______________; }

data2D[i][column]

Fill in the blank to find the total for the specific row of a two-dimensional integer array data2D with dimensions ROWS and COLUMNS? int total = 0; for (int i = 0; i < COLUMNS; i++) { total = total + ______________; }

data2D[row][i]

Fill in the blank for this algorithm for removing an element at a position in an ordered partially filled array, assuming currentSize is the companion variable indicating the actual number of elements in the data array? for (int i = position + 1; i < currentSize; i++) { ____________________ } currentSize--;

data[i - 1] = data[i];

Fill in the blank for this algorithm for inserting an element at a position in an ordered partially filled array, assuming currentSize is the companion variable indicating the actual number of elements in the data array? if (currentSize < data.length) { for (int i = currentSize; i > position; i--) { ____________________ } data[pos] = newElement; currentSize++; }

data[i] = data[i - 1];

Java 7 introduced enhanced syntax for declaring array lists, which is termed _________________.

diamond syntax

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

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?

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

Fill in the blank for this algorithm for displaying elements separated by semicolons. for (int i = 0; i < names.length; i++) { System.out.print(names[i]); if (__________) { System.out.print("; "); } }

i != names.length - 1

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

i < data.length

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

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

Which one of the following snippets finds the value of the left 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 leftNeighbor = -1; // choose snippet to find left neighbor

if (k > 0) { leftNeighbor = data2D[j, k-1]; }

Consider the following 2-dimensional array. Select the statement that gives the number of columns in the third row. int[][] counts = { { 0, 0, 1 }, { 0, 1, 1, 2 }, { 0, 0, 1, 4, 5 }, { 0, 2 } };

int cols = counts[2].length;

Choose the correct algorithm for removing values in the data array list that are beyond a threshold.

int j = 0; while (j < data.size()) { if (data.get(j) > threshold) { data.remove(j); } else { j++; } }

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

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

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

The following code is an example of what type of search? public class Bank { public BankAccount find(int accountNumber) { for (BankAccount a : accounts) { if (a.getAccountNumber() == accountNumber) return a; } return null; } . . . }

linear

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

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

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[][] arr)

Consider the following line of code for calling a method named func1: func1(listData, varData); Which one of the following method headers is valid for func1, where listData is an integer array list and varData is an integer variable?

public static void func1(ArrayList<Integer> ldata, int vdata)

Which one of the following is a correct declaration for a method named passAList that has as arguments an array list myList of size 10 and an integer array intArr of size 20, and that modifies the contents of myList and intArr?

public static void passAList(ArrayList<Integer> myList, int[] intArr)

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)

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

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

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

The binary search is faster than the linear search, providing ____________________.

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 _______________.

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 ___________________.

the number of inputs may not be known in advance

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

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?

"ghi"

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

0 to 9

Consider the following code snippet: ArrayList<Double> somedata = new ArrayList<Double>(); somedata.add(10.5); What is the size of the array list somedata after the given code snippet is executed?

1

What is the value of the cnt variable after the execution of the code snippet below? ArrayList<Integer> somenum = new ArrayList<Integer>(); somenum.add(1); somenum.add(2); somenum.add(1); int cnt = 0; for (int index = 0; index < somenum.size(); index++) { if (somenum.get(index) % 2 == 0) { cnt++; } }

1

What is the output of the following code? int[][] counts = { { 0, 0, 1 }, { 0, 1, 1, 2 }, { 0, 0, 1, 4, 5 }, { 0, 2 } }; System.out.println(counts[3].length);

2

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 zeros because array num is not changed by method

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

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

6

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

What is the output of the following statements? ArrayList<String> cityList = new ArrayList<String>(); cityList.add("London"); cityList.add("New York"); cityList.add("Paris"); cityList.add("Toronto"); cityList.add("Hong Kong"); cityList.add("Singapore"); System.out.print(cityList.size()); System.out.print(" " + cityList.contains("Toronto")); System.out.print(" " + cityList.indexOf("New York")); System.out.println(" " + cityList.isEmpty());

6 true 1 false

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

Your program needs to store a sequence of integers of unknown length. Which of the following is most suitable to use?

A array list declared as ArrayList<Integer> marks = new ArrayList<Integer>();

Which one of the following statements about declaring an array list as a method parameter is true?

An array list value can be modified inside the method.

What is the output of the following statements? ArrayList<String> names = new ArrayList<String>(); names.add("Bob"); names.add(0, "Ann"); names.remove(1); names.add("Cal"); names.set(1, "Tony"); for (String s : names) { System.out.print(s + ", "); }

Ann, Tony

What is the output of the following statements? ArrayList<String> names = new ArrayList<String>(); names.add("Bob"); names.add(0, "Ann"); names.remove(1); names.add("Cal"); names.set(2, "Tony"); for (String s : names) { System.out.print(s + ", "); }

Array list bound error

Which statement represents the shortcut diamond syntax for declaring and constructing array lists?

ArrayList<Double> values = new ArrayList<>();

Which one of the following code snippets accepts the integer input in an array list named num1 and stores the even integers of num1 in another array list named evennum?

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

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

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

Arrays.sort(data);

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.

What is the purpose of this algorithm that uses the accounts array list of BankAccount objects, which have a getBalance method? int m = 0; for (BankAccount a: accounts) { if (a.getBalance() >= minBalance) m++; }

Counting matches.

What is the output of the following code snippet? ArrayList<Integer> num; num.add(4); System.out.println(num.size());

Error because num is not initialized

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.

I and III only

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

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.

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

Which statement(s) about the size of a Java array, array list, and string are true? I. The syntax for determining the size of an array, an array list, and a string in Java is consistent among the three. II. The string uses s.size(), while the array list uses a.length(). III. The array uses a.length, which is not a method call.

III only

What is the purpose of this algorithm, assuming currentSize is the companion variable indicating the actual number of elements in the data array? if (currentSize < data.length) { data[currentSize] = e; currentSize++; }

Inserting an element e in an unordered partially filled array.

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

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

Why is the use of physical objects helpful in algorithm design?

Many people feel it is less intimidating than drawing diagrams

Why is the following method header invalid? public static int[5] meth(int[] arr, int[] num)

Methods that return an array cannot specify its size.

Consider the following code snippet: ArrayList<Integer> arrList = new ArrayList<Integer>(); for (int i = 0; i < arrList.size(); i++) { arrList.add(i + 3); } What value is stored in the element of the array list at index 0?

None

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

Prints out the names in an array separated by semicolons.

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

Which one of the following statements is correct for displaying the value in the third row and the fourth column of a two-dimensional 5 by 6 array?

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

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

What should you check for when calculating the largest value in an array list?

The array list contains at least one element.

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 the result of the following code? for (double element : values) { element = 0; }

The array values is not modified.

Consider the following code snippet: public static void check(ArrayList<Integer> chknum1) { int cnt = 0; for(int i = 0; i < chknum1.size(); i++) { if(chknum1.get(i) == 0) { cnt++; } } System.out.println("The total 0 values in the list are: " + cnt); } Which one of the following is true about the check method in the given code snippet?

The check method counts all the elements with value 0 in an array list passed as a parameter to the method.

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 a run-time error.

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.

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.

Which statement is true about the code snippet below? ArrayList<String> names = new ArrayList<String>(); names.add("John"); names.add("Jerry"); ArrayList<String> friends = new ArrayList<String>(names); friends.add("Harry");

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

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 3; the final size of friends is 3

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 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?

The method return type should be changed to int[].

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 not compile.

What is the result of the following definition of an array list? ArrayList<Double> points;

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

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 does this loop compute, assuming accounts is not empty? double b = 0.0; for (BankAccount a: accounts) { if (a.getBalance() > b) b = a.getBalance(); } return b;

The value of the largest balance.

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.

What is 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"); }

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

aaa 3


Conjuntos de estudio relacionados

BUS140 In-Class Quizzes ch9, 11, 12, 16

View Set

Chapter 4 (South America) Regions Study Set

View Set

free biology exit exam practice test

View Set

Module 19/20 - Introduction to Ecology

View Set

Principles of Management - Final Exam Cate Loes

View Set

Texas Principles of Real Estate 1 - Chapter 12

View Set

Accounting Smart Books, Assignments, etc.

View Set

Ch 3. License Application and Administration

View Set