Hw8
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.
a. ++arrayName[i]
Assume class Book has been declared. Which set of statements creates an array of Books? a. Book[] books; books = new Book[numberElements]; b. Book[] books[]; books = new Book()[numberElements]; c. new Book() books[]; books = new Book[numberElements]; d. All of the above
a. Book[] books; books = new Book[numberElements];
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. d. You can get an item from a specified index in an ArrayList with its get method.
a. 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]; 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.
a. The value of g[3] is -1
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.
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.
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);
a. int[] n = {1, 2, 3, 4, 5};
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
a. java TestProgram files.txt 3
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
b. A, B, D
Class ________ represents a dynamically resizable array-like data structure. a. Array b. ArrayList c. Arrays d. None of the above
b. ArrayList
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.
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.
b. Create a double array containing 14 elements.
Which statement correctly passes the array items to method takeArray? Array items contains 10 elements. a. takeArray(items[]) b. takeArray(items) c. takeArray(item[9]) d. Arrays cannot be passed to methods—each item must be sent to the method separately.
b. takeArray(items)
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
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
c. fault-tolerant
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]);
c. for (int i : items) System.out.prinf("%d%n", i);
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
d. Declare then create the array
A(n) ________ indicates a problem that occurs while a program executes. a. Syntax error b. Omitted import c. Missing semicolon d. Exception
d. Exception
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.
d. Incrementing the value stored in each element of the array.
Invalid possibilities for array indices include ______. a. Positive integers b. Negative integers c. Zero d. None of the above
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
b. Result is: 286
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"
b. fixed-length entities
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
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.
c. ArrayIndexOutOfBoundsException.
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)
c. Integer.parseInt(stringVariable)
In array items, which expression below accesses the value at row 3 and column 4? a. Item[3].[4] b. Item[3[4]] c. Item[3][4] d. Item[3,4]
c. Item[3][4]
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
c. Result is: 65
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.
c. The elements of an array of integers have a value of null before they are initialized.
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.
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.
Consider integer array values, which contains 5 elements. Which statements successfully swap the contents of the array at index 3 and index 4? a. values[3] = values[4]; values[4] = values[3]; b. values[4] = values[3]; values[3] = values[4]; c. int temp = values[3]; values[3] = values[4]; values[4] = temp; d. int temp = values[3]; values[3] = values[4]; values[4] = values[3];
c. int temp = values[3]; values[3] = values[4]; values[4] = temp;
Constant variables also are called ______. a. write-only variables b. finals c. named constants d. all of the above
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
c. the called method can access the argument's value in the caller directly and modify that data
Which flag in a format specifier indicates that values with fewer digits than the field width should begin with a leading 0? a. p b. l c. w d. 0
d. 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; } a. 0, 2, 5, 6, 12 b. 0, 2, 12, 6, 8 c. 0, 2, 4, 6, 5 d. 0, 2, 4, 6, 12
d. 0, 2, 4, 6, 12
Class Arrays provides method __________ for comparing arrays to determine whether they have the same contents a. compare b. compares c. equal d. equals
d. equals
An exception object's ________ method returns the exception's error message. a. String b. Message c. Error d. toString
d. toString