CS - Chapter 7

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

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

0 to 9

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

6

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.

Which one of the following statements is true about using arrays with methods?

Arrays can be method arguments, and return values, just like any other values.

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.

I, II only

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, 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, 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, III only

Which statements about the enhanced for loop are true for arrays of primitive data? 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, 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

Which statement correctly describes the enhanced for loop?

In the enhanced for loop, the element variable is assigned. In the basic for loop, the index variable is assigned.

Assume the method doSomething has been defined as follows: public static int[] doSomething (int[] values) { int [] result = new int[values.length - 1]; for (int i = 0; i < result.length; i++) { result[i] = values[i + 1]; } return result; } What does the method do?

It copies all but the first element in the integer array values to a new array.

Assume the method doSomething has been defined as follows: public static void doSomething (int[] values) { for (int i = 1; i < values.length; i++) { values[i - 1] = values[i]; } } What does the method do?

It moves each element of the array values to a lower index position and overwrites the first element.

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 method doSomething has been defined as follows: public static void doSomething (int[] values, int p1, int p2) { int temp = values[p1]; values[p1] = values[p2]; values[p2] = temp; }

It swaps the integer values stored in positions p1 and p2 of the array values.

Assume the method doSomething has been defined as follows: public static void doSomething (int[] values, int p1, int p2) { int temp = values[p1]; values[p1] = values[p2]; values[p2] = values[p1]; } What does the method do?

It swaps the integer values stored in positions p1 and p2 of the array values.

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.

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.

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

The array list contains at least one element.

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

The array list contains at least one element.

The code snippet below is intended to perform a linear search on the array values to find the location of the value 42. What is the error in the code snippet? int searchedValue = 42; int pos = 0; boolean found = true; while (pos < values.length && !found) { if (values[pos] == searchedValue) { found = true; } else { pos++; } }

The boolean variable found should be initialized to false.

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.

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 (if anything) is wrong with the following code snippet, which searches for the searchedValue variable in the array data? 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.

The enhanced for loop is

convenient for traversing all elements in an array

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

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

diamond syntax.

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

double m = minimum(new double[] { 12.2, 31.5, 6.6, 2.4 });

Assume the array of integers values has been created and process is a method that has a single integer parameter. Which of the following is equivalent to the loop below? for (int val: values) { process(val); }

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

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 with three rows and two columns?

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

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

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

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.

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

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

searching

This code snippet is an implementation of what algorithm? int searchedValue = 42; int pos = 0; boolean found = false; while (pos < values.length && !found) { if (values[pos] == searchedValue) { found = true; } else { pos++; } }

sequential search

When an array reading and storing input runs out of space

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

The binary search is more efficient than the linear search, providing

the elements of the array are ordered.

It may be necessary to "grow" an array when reading inputs because

the number of inputs may not be known in advance.

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

Consider the code snippet below: int searchedValue = 42; int pos = 0; boolean found = false; while (pos < values.length && !found) { if (values[pos] == searchedValue) { found = true; } else { pos++; } } When the loop finishes what does the value of the boolean variable found indicate?

whether or not the value 42 was found in the array


Ensembles d'études connexes

Chapter 25 - Assessment of Cardiovascular Function

View Set

5a. Development Issues, Prenatal Development, and the Newborn

View Set

Study Questions for Chapter 9 Perseus, Chapter 10 Theseus, Chapter 11 Hercules, and Chapter 12 Atalanta

View Set