BP - CH 6: Arrays and ArrayLists

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Introduction to Collections and Class ArrayList Class ________ represents a dynamically resizable array-like data structure. a. Array b. ArrayList c. Arrays d. None of the above.

ANS: b. ArrayList

Types in Java are divided into two categories. The primitive types are boolean, byte, char, short, int, long, float and double. All other types are ________ types. a. static b. reference c. declared d. source

ANS: b. reference

Which of the following statements is false? a. A reference to an object is required to invoke an object's methods. b. A primitive-type variable does not refer to an object. c. Reference-type instance variables are initialized by default to the value void. d. A primitive-type variable cannot be used to invoke a method.

ANS: c. Reference-type instance variables are initialized by default to the value void. Actually, Reference-type instance variables are initialized by default to the value null.

Reference-type variables (called references) store ________ in memory. a. the value of an object b. a copy of an object c. the location of an object d. the size of an object

ANS: c. the location of an object

Multidimensional Arrays In Java, multidimensional arrays ________. a. are not directly supported. b. are implemented as arrays of arrays. c. are often used to represent tables of values. d. All of the above.

Ans: d. All of the above.

Which of the following statements about an arc is false? a. An arc is a section of an oval. b. The sweep is the amount of arc to cover. c. Method drawArc draws the edges of an arc. d. The fillArc method draws an oval, with the section that is an arc filled in.

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

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. a copy of the element's reference, a copy of the element's reference b. a copy of the element's value, a copy of the element's reference c. a copy of the element's value, a copy of the element's value d. a copy of the element's reference, a copy of the element's value

Ans: d. a copy of the element's reference, a copy of the element's value

Declaring and Creating Arrays A programmer must do the following before using an array: a. declare then reference the array. b. create then declare the array. c. create then reference the array. d. declare then create the array.

Ans: d. declare then create the array.

Exception Handling: Processing the Incorrect Response A(n) ________ indicates a problem that occurs while a program executes. a. syntax error b. omitted import c. missing semicolon d. exception

Ans: d. exception

toString Method of the Exception Parameter An exception object's ________ method returns the exception's error message. a. String b. Message c. Error d. toString

Ans: toString

Class Arrays 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. a. sort, binarySearch. b. sort, fill. c. binarySearch, equals. d. binarySearch, fill.

ANS: c. binarySearch, equals.

Which of the following statements is false? a. A primitive-type variable can store exactly one value of its declared type at a time. b. Primitive-type instance variables are initialized by default. c. Variables of types byte, char, short, int, long, float and double are initialized to 0. d. Variables of type boolean are initialized to true.

ANS: d. Variables of type boolean are initialized to true. Actually, variables of type boolean are initialized to false.

Class Arrays provides method __________ for comparing arrays to determine whether they have the same contents. a. compare b. compares c. equal d. equals

ANS: d. equals.

Which of the following is false? a. The size of an ArrayList can be determined via its length instance variable. b. The size of an ArrayList can be determined via its size method. c. You can add a new item to the end of an ArrayList with its add method. c. You can get an item from a specified index in an ArrayList with its get method.

Ans: a.

Which set of statements totals the items in each row of two-dimensional array items, and displays each row's total? a. for (int row = 0; row < items.length; row++) { int total = 0; for (int column = 0; column < items[row].length; column++) total += items[row][column]; System.out.printf("Row %d's total is %d%n", row, total); } b. int total = 0; for (int row = 0; row < items.length; row++) { for (int column = 0; column < items[row].length; column++) total += items[row][column]; System.out.printf("Row %d's total is %d%n", row, total); } c. int total = 0; for (int row = 0; row < items.length; row++) { for (int column = 0; column < items[column].length; column++) total += items[row][column]; System.out.printf("Row %d's total is %d%n", row, total); } d. for (int row = 0; row < items.length; row++) { int total = 0; for (int column = 0; column < items[column].length; column++) total += items[row][column]; System.out.printf("Row %d's total is %d%n", row, total); }

Ans: a. for (int row = 0; row < items.length; row++) { int total = 0; for (int column = 0; column < items[row].length; column++) total += items[row][column]; System.out.printf("Row %d's total is %d%n", row, total); }

Using Arrays to Analyze Survey Results Which expression adds 1 to the element of array arrayName at index i? a. ++arrayName[i]. b. arrayName++[i]. c. arrayName[i++]. d. None of the above.

Ans: a. ++arrayName[i].

Consider the code segment below. Which of the following statements is false? int[] g; g = new int[23]; a. The value of g[3] is -1. b. The first statement declares an array reference. c. The second statement creates the array. d. g is a reference to an array of integers.

Ans: a. The value of g[3] is -1.

Pass-By-Value vs. Pass-By-Reference Which of the following statements is false? a. When an argument is passed by reference, the called method can access the argument's value in the caller directly but cannot modify it. b. All arguments in Java are passed by value. c. To pass an individual array element to a method, use the indexed name of the array. d. To pass an object reference to a method, simply specify in the method call the name of the variable that refers to the object.

Ans: a. 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.

Using an Array Initializer Which of the following initializer lists would correctly set the elements of array n? a. int[] n = {1, 2, 3, 4, 5};. b. array n[int] = {1, 2, 3, 4, 5};. c. int n[5] = {1; 2; 3; 4; 5};. d. int n = new int(1, 2, 3, 4, 5);.

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

Which statement below initializes array items to contain 3 rows and 2 columns? a. int[][] items = {{2, 4}, {6, 8}, {10, 12}}; b. int[][] items = {{2, 6, 10}, {4, 8, 12}}; c. int[][] items = {2, 4}, {6, 8}, {10, 12}; d. int[][] items = {2, 6, 10}, {4, 8, 12};

Ans: a. int[][] items = {{2, 4}, {6, 8}, {10, 12}};

Using Command-Line Arguments Which command below runs TestProgram, and passes in the values files.txt and 3? a. java TestProgram files.txt 3. b. java TestProgram files.txt, 3. c. java TestProgram "files.txt", "3". d. None of the above.

Ans: a. java TestProgram files.txt 3.

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? a. int[][] items; items = new int[3][?]; items[0] = new int[1]; items[1] = new int[4]; items[2] = new int[2]; b. int[][] items; items = new int[3][]; items[0] = new int[1]; items[1] = new int[4]; items[2] = new int[2]; c. int[][] items; items = new int[?][?]; items[0] = new int[1]; items[1] = new int[4]; items[2] = new int[2]; d. int[][] items; items[0] = new int[1]; items[1] = new int[4]; items[2] = new int[2];

Ans: b. int[][] items; items = new int[3][]; items[0] = new int[1]; items[1] = new int[4]; items[2] = new int[2];

Arrays 6.3 Q1: Which of the following statements about arrays are true? A. An array is a group of variables containing values that all have the same type. B. Elements are located by index. C. The length of an array c is determined by the expression c.length();. D. The zeroth element of array c is specified by c[0]. a. A, C, D. b. A, B, D. c. C, D. d. A, B, C, D.

Ans: b. A, B, D.

Which of the following will not produce a compiler error? a. Changing the value of a constant after it is initialized. b. Changing the value at a given index of an array after it is created. c. Using a final variable before it is initialized. d. All of the above will produce compiler errors.

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

What do the following statements do? double[] array; array = new double[14]; a. Create a double array containing 13 elements. b. Create a double array containing 14 elements. c. Create a double array containing 15 elements. d. Declare but do not create a double array.

Ans: b. Create a double array containing 14 elements.

Using the Elements of an Array as Counters Invalid possibilities for array indices include . a. Positive integers. b. Negative integers. c. Zero. d. None of the above.

Ans: b. Negative integers.

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); } } The output of this Java program will be: a. Result is: 280. b. Result is: 286. c. Result is: 154. d. Result is: 332.

Ans: b. Result is: 286.

Variable-Length Argument Lists 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. a. square brackets ([]) b. ellipsis (...) c. varargs keyword d. All of the above are acceptable to indicate a variable number of arguments.

Ans: b. ellipsis (...)

Arrays are ________. a. variable-length entities b. fixed-length entities c. data structures that contain up to 10 related data items d. used to draw a sequence of lines, or "rays"

Ans: b. fixed-length entities.

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? a. int[][] items = {{1, null, null, null}, {2, 3, 4, 5}, {6, 7, null, null}}; b. int[][] items = {{1}, {2, 3, 4, 5}, {6, 7}}; c. int[][] items = {{1}, {2, 3, 4, 5}, {6, 7}, {}); d. int[][] items = {{1}, {4}, {2}};

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

Which method sets the background color of a JPanel? a. setBack. b. setBackground. c. setBackgroundColor. d. setColor.

Ans: b. setBackground.

Passing Arrays to Methods Which statement correctly passes the array items to method takeArray? Array items contains 10 elements. a. takeArray(items[]). b. takeArray(items). c. takeArray(items[9]). d. Arrays cannot be passed to methods—each item must be sent to the method separately.

Ans: b. takeArray(items).

Enhanced for Statement 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? a. for (int i = 0; i < items.length; i++) System.out.prinf("%d%n", items[i]); b. for (int i : items) System.out.prinf("%d%n", items[i]); c. for (int i : items) System.out.prinf("%d%n", i); d. for (int i = 0 : items.length) System.out.prinf("%d%n", items[i]);

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

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: a. 0. b. 3. c. 9. d. 0.

Ans: c. 9.

Attempting to access an array element outside of the bounds of an array, causes a(n) . a. ArrayOutOfBoundsException. b. ArrayElementOutOfBoundsException. c. ArrayIndexOutOfBoundsException. d. ArrayException.

Ans: c. ArrayIndexOutOfBoundsException.

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. a. A and C. b. A and D. c. B and D. d. B and C.

Ans: c. B and D.

Which method call converts the value in variable stringVariable to an integer? a. Convert.toInt(stringVariable) b. Convert.parseInt(stringVariable) c. Integer.parseInt(stringVariable) d. Integer.toInt(stringVariable)

Ans: c. Integer.parseInt(stringVariable)

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]; System.out.printf("Result is: %d%n", result); } } The output of this program will be: a. Result is: 62. b. Result is: 64. c. Result is: 65. d. Result is: 67.

Ans: c. Result is: 65.

Which of the following statements is false? a. An exception indicates a problem that occurs while a program executes. b. Exception handling enables you to create fault-tolerant programs that can resolve (or handle) exceptions—in many cases, this allows a program to continue executing as if no problems were encountered. c. 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. d. Inside the catch block, you can use the parameter's identifier to interact with a caught exception object.

Ans: c. 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.

Creating and Initializing an Array 6.5.1 Q1: Which of the following statements about creating arrays and initializing their elements is false? a. The new keyword should be used to create an array. b. When an array is created with operator new, the number of elements must be placed in square brackets following the type of element being stored. c. The elements of an array of integers have a value of null before they are initialized. d. A for loop is commonly used to set the values of the elements of an array.

Ans: c. 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.

Executing the catch Block Which of the following statements is false? a. A catch block declares a type and an exception parameter name. b. Inside the catch block, you can use the parameter's name to interact with a caught exception object. c. 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. d. If an attempt is made to use an invalid index to access an element, an ArrayIndexOutOfBoundsException exception occurs.

Ans: c. 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.

The try Statement Which of the following statements is true? a. The catch block contains the code that might throw an exception. b. The try block contains the code that handles the exception if one occurs. c. You can have many catch blocks to handle different types of exceptions. d. When a try block terminates, any variables declared in the try block are preserved.

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

Exception handling helps you create ________ programs. a. high-performance b. logic-error-free c. fault-tolerant d. compilation-error-free

Ans: c. fault-tolerant

In array items, which expression below accesses the value at row 3 and column 4? a. items[3].[4] b. items[3[4]] c. items[3][4] d. items[3, 4]

Ans: c. items[3][4]

Calculating a Value to Store in Each Array Element 6.5.3 Q1: Constant variables also are called . a. write-only variables b. finals c. named constants d. All of the above

Ans: c. named constants.

When an argument is passed by reference, ________. a. a copy of the argument's value is passed to the called method b. changes to the argument do not affect the original variable's value in the caller c. the called method can access the argument's value in the caller directly and modify that data d. the original value is removed from memory

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

The preferred way to traverse a two-dimensional array is to use . a. a do while statement. b. a for statement. c. two nested for statements. d. three nested for statements.

Ans: c. two nested for statements.

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; } a. 0, 2, 5, 6, 12. b. 0, 2, 12, 6, 8. c. 0, 2, 4, 6, 5. d. 0, 2, 4, 6, 12.

Ans: d. 0, 2, 4, 6, 12.

For the array that was the correct answer in the previous question, what is the value returned by items[1][0]? a. 4. b. 8. c. 12. d. 6.

Ans: d. 6.

Which of the following tasks cannot be performed using an enhanced for loop? a. Calculating the product of all the values in an array. b. Displaying all even element values in an array. c. Comparing the elements in an array to a specific value. d. Incrementing the value stored in each element of the array.

Ans: d. Incrementing the value stored in each element of the array.


Kaugnay na mga set ng pag-aaral

The Menstrual Cycle (Women's Health)

View Set

Data Structures and Algorithms Final review

View Set

Chapter 18: Sterilization and Disinfection

View Set

EAQ CARDIOVASCULAR AND RESPIRATORY HW #2

View Set

Upper and lower motor neuron syndromes

View Set

Texas Real Estate 1 practice exam questions

View Set

2.4 Revising and Editing (pg. 72-83)

View Set