Chapter 7 Array and ArrayLists

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

A programmer must do the following before using an array:

declare then create the array.

Which expression adds 1 to the element of array arrayName at index i?

++arrayName[i].

Which flag in a format specifier indicates that values with fewer digits than the field width should begin with a leading 0?

0

Consider array items, which contains the values 0, 2, 4, 6 and 8. If method changeArray is called with the method call changeArray(items, items[2]), what values are stored in items after the method has finished executing? public static void changeArray(int[] passedArray, int value){ passedArray[value] = 12; value = 5;}

0, 2, 4, 6, 12.

How many Book objects are created by the following statement? Book[] books = new Book[10];

0. The statement creates an array of element that can refer to Book objects. Each element is initially null.

Consider the class below: public class Test{ public static void main(String[] args) { int[] a = {99, 22, 11, 3, 11, 55, 44, 88, 2, -3}; int result = 0; for (int i = 0; i < a.length; i++) { if (a[i] > 30) result += a[i]; } System.out.printf("Result is: %d%n", result); } }

Result is: 286.

Consider the program below: public class Test{ public static void main(String[] args) { int[] a; a = new int[10]; for (int i = 0; i < a.length; i++) a[i] = i + 2; int result = 0; for (int i = 0; i < a.length; i++) result += a[i];

Result is: 65.

What kind of application tests a class by creating an object of that class and calling the class's methods?

Test harness

Which of the following statements is false?

The catch block contains the code that might throw an exception, and the try block contains the code that handles the exception if one occurs.

Which of the following statements about creating arrays and initializing their elements is false?

The elements of an array of integers have a value of null before they are initialized. They actially have the value 0—the default for type int.

Which of the following statements about an arc is false?

The fillArc method draws an oval, with the section that is an arc filled in.

Which of the following is false?

The size of an ArrayList can be determined via its length instance variable.

Consider the code segment below. Which of the following statements is false? int[] g;g = new int[23];

The value of g[3] is -1.

Which of the following statements is false?

When a program is executed, array element indices are checked for validity—all indices must be greater than 0 and less than or equal to the length of the array.Actually, all indices must be greater than or equal to 0 and less than the length of the array.

Which of the following statements is false?

When an argument is passed by reference, the called method can access the argument's value in the caller directly but cannot modify it. Actually, when an argument is passed by reference, the called method can access the argument's value in the caller directly and possibly modify it.

What do the following statements do? double[] array;array = new double[14];

b. Create a double array containing 14 elements.

An argument type followed by a(n) in a method's parameter list indicates that the method receives a variable number of arguments of that particular type.

ellipsis (...)

Which of the following tasks cannot be performed using an enhanced for loop?

Incrementing the value stored in each element of the array.

Which method call converts the value in variable stringVariable to an integer?

Integer.parseInt(stringVariable)

Constant variables also are called .

Named constants

Which flag in a format specifier indicates that values with fewer digits than the field width should begin with a leading 0?

Negative Integers

Class Arrays provides method __________ for comparing arrays to determine whether they have the same contents.

equals

A(n) ________ indicates a problem that occurs while a program executes

exception

Exception handling helps you create ________ programs.

fault-tolerant

Arrays are ________.

fixed-length entities

Assume array items contains the integer values 0, 2, 4, 6 and 8. Which of the following uses the enhanced for loop to display each value in array items?

for (int i : items) System.out.prinf("%d%n", i);

Which set of statements totals the items in each row of two-dimensional array items, and displays each row's total?

for (int row = 0; row < items.length; row++){ int total = 0; for (int column = 0; column < items[row].length; column++) total += items[row][column];

Consider integer array values, which contains 5 elements. Which statements successfully swap the contents of the array at index 3 and index 4?

int temp = values[3];values[3] = values[4];values[4] = temp;

Which set of statements totals the values in two-dimensional int array items?

int total = 0;for (int[] subItems : items) for (int item : subItems) total += item;

Which of the following statements creates a multidimensional array with 3 rows, where the first row contains 1 element, the second row contains 4 elements and the final row contains 2 elements?

int[][] items = {{1}, {2, 3, 4, 5}, {6, 7}};

Which statement below initializes array items to contain 3 rows and 2 columns?

int[][] items = {{2, 4}, {6, 8}, {10, 12}};

Which of the following sets of statements creates a multidimensional array with 3 rows, where the first row contains 1 value, the second row contains 4 items and the final row contains 2 items?

int[][] items;items = new int[3][];items[0] = new int[1];items[1] = new int[4];items[2] = new int[2];

In array items, which expression below accesses the value at row 3 and column 4?

items[3][4]

Which command below runs TestProgram, and passes in the values files.txt and 3?

java TestProgram files.txt 3.

Which method sets the background color of a JPanel?

setBackground

Which statement correctly passes the array items to method takeArray? Array items contains 10 elements

takeArray(items).

When an argument is passed by reference, ________.

the called method can access the argument's value in the caller directly and modify that data.

An exception object's ________ method returns the exception's error message

toString

The preferred way to traverse a two-dimensional array is to use .

two nested for statements.

In Java, multidimensional arrays ________.

all of the above

For the array that was the correct answer in the previous question, what is the value returned by items[1][0]?

6

Consider the array: s[0] = 7 s[1] = 0 s[2] = -12 s[3] = 9 s[4] = 10 s[5] = 3 s[6] = 6 The value of s[s[6] - s[5]] is:

9

Which of the following statements about arrays are true?

Ans: b. A, B, D.

Attempting to access an array element outside of the bounds of an array, causes a(n) .

ArrayIndexOutOfBoundsException.

Class ________ represents a dynamically resizable array-like data structure.

ArrayList

An array with m rows and n columns is not ________. A. an m-by-n array. B. an n-by-m array. C. a two-dimensional array. D. a dual-transcripted array.

B and D.

Assume class Book has been declared. Which set of statements creates an array of Books?

Book[] books;books = new Book[numberElements];

Which of the following will not produce a compiler error?

Changing the value at a given index of an array after it is created.

Which of the following statements is true?

You can have many catch blocks to handle different types of exceptions.

When you pass an array or an individual array element of a reference type to a method, the called method receives ________. When you pass an individual element of a primitive type, the called method receives ________.

a copy of the element's reference, a copy of the element's value

Which of the following initializer lists would correctly set the elements of array n?

a. int[] n = {1, 2, 3, 4, 5};.

Class Arrays methods sort, binarySearch, equals and fill are overloaded for primitive-type arrays and Object arrays. In addition, methods __________ and __________ are overloaded with generic versions.

binarySearch, equals.


Conjuntos de estudio relacionados

Solving Trigonometric Inequalities

View Set

AP BIO: Unit 4 Practice Questions

View Set

Different Perspectives on Nationalism

View Set

Psychology Disorders of Children Final Exam Material

View Set