You are on page 1of 11

Gaddis Starting Out With Java 5 From Control Structures to Objects Chapter 08 Arrays and the ArrayList Class

2. What will be the value of x[8] after the following code has been executed? final int SUB 12; int[] x new int[SUB]; int y 100; for(int i 0; i SUB; i) { x[i] y; y 10; } a. 170 b. 180 c. 190 d. 200 ANS: B 3. What will be the results of the following code? final int ARRAY_SIZE 5; float[] x new float[ARRAY_SIZE]; for(int i 1; i ARRAY_SIZE; i) { x[i] 10.0; } a. All the values in the array are initialized to 10.0 b. All the values, except the first, are set to 10.0 c. The program will crash when it is executed d. There will be a compilation error ANS: C , you cannot compare array elements ANS: A

6. What will be the value for x[1] after the following code is executed? int[] x {22, 33, 44}; arrayProcess(x); public static void arrayProcess(int[] a) { for(int k 0; k 3; k) { a[k] a[k] 5; } }

a. b. c. d. ANS: C

27 33 38 49

7. When an array is passed to a method a. A reference to the array is passed b. It is passed just as an object c. The method has direct access to the original array d. All of the above ANS: D 9. What would be the results of the following code? int[] array1 new int[25]; // Code that will put values in array1 int value array1[0]; for (int a 1; a array1.length; a) { if (array1[a] value) value array1[a]; } a. value contains the highest value in array1 b. value contains the lowest value in array1 c. value contains the sum of all the values in array1 d. value contains the average of the values in array1 ANS: B 10. A partially-filled array is normally used a. When you know how many elements will be in the array b. With an accompanying parallel array c. With an accompanying integer value that holds the number of items stored in the array d. To display array elements ANS: C

13. Given String[] str has been initialized, to convert all the characters in the String str[0] to upper case, use the following statement: a. str.uppercase(); b. str[0].upperCase(); c. str.toUpperCase(); d. str[0].toUpperCase(); ANS: D

15. The sequential search algorithm a. Requires the array to be ordered b. Returns the value it was searching for c. Uses a loop to sequentially step through an array, starting with the first element d. Will not execute, if the element is not in the array ANS: C 16. A sorting algorithm is a technique for scanning through an array and rearranging its contents in some specific order. a. True b. False ANS: A 17. In order to do a binary search on an array, a. The values of the array must be numeric b. The array must first be sorted in ascending order c. You must first do a sequential search of the array to assure the element you are looking for is there d. There are no requirements ANS: B 18. Any items typed on the command-line, separated by space, after the name of the class are considered to be one or more arguments that are to be passed into the main method. a. True b. False ANS: A

20. If numbers is a two-dimensional array, which of the following would give the length of row, r? a. numbers.length b. numbers.length[r] c. numbers[r].length[r] d. numbers[r].length ANS: D

25. What will be the value of x[8] after the following code has been executed? final int SUB 12; int[] x new int[SUB]; int y 20; for(int i 0; i SUB; i) { x[i] y; y 5; }

a. b. c. d. ANS: C

50 55 60 65

26. What will be the result of executing the following code? int[] x {0, 1, 2, 3, 4, 5}; a. An array of 6 values ranging from 05 and referenced by the variable x will be created b. A compilation error will occur c. The program will crash when it is executed d. The value of x[0] will be 15 and x[1] x[5] will be set to 0 ANS: A

29. What will be the value for x[1] after the following code is executed? int[] x {22, 33, 44}; arrayProcess(x[1]); public static void arrayProcess(int a) { a a 5; } a. 27 b. 33 c. 38 d. 49 ANS: B 30. When an individual element of an array is passed to a method a. A reference to the array is passed b. It is passed just as an object c. The method does not have direct access to the original array d. All of the above ANS: C 31. If a[] and b[] are two integer arrays, then if( a b) compares the array contents. a. True b. False ANS: B 32. What would be the results of the following code? int[] array1 new int[25]; // Code that will put values in array1 int value 0;

for (int a 0; a array1.length; a) { value array1[a]; value array1[a]; } a. value contains the highest value in array1 b. value contains the lowest value in array1 c. value contains the sum of all the values in array1 d. This would cause the program to crash ANS: D

35. For the following code, what would be the value of str[2]? String[] str {abc, def, ghi, jkl}; a. ghi b. def: c. A reference to the String ghi d. A reference to the String def: ANS: C 36. Which of the following statements is a valid for statement, given the following? String[] names {abc, def, ghi, jkl}; a. for (int i 0; i names.length; i) System.out.println(names[i].length); b. for (int i 0; i names.length(); i) System.out.println(names[i].length); c. for (int i 0; i names.length; i) System.out.println(names[i].length()); d. for (int i 0; i names.length(); i) System.out.println(names[i].length()); ANS: C

38. A search algorithm is a a. Method for locating a specific item in a larger collection of data b. Is rarely used with arrays c. Arranges elements in ascending order d. Arranges elements in descending order ANS: A 39. A sorting algorithm is used to locate a specific item in a larger collection of data. a. True b. False ANS: B

40. The binary search algorithm a. Is less efficient than the sequential search algorithm b. Will cut the portion of the array being searched in half each time the loop fails to locate the search value c. Will have a maximum number of comparisons equal to the number of elements in the array d. Will have an average of N/2 comparisons, where N is the number of elements in the array ANS: B

42. Given the following two-dimensional array declaration, which statement is true? int [] [] numbers new int [6] [9]; a. The array numbers has 6 columns and 9 rows b. The array numbers has 6 rows and 9 columns c. The array numbers has 15 rows d. The array numbers has 54 rows ANS: B 43. If numbers is a two-dimensional int array that has been initialized and total is an int that has been set to 0, which of the following will sum all the elements in the array? a. for (int row 1; row numbers.length; row) { for (int col 1; col numbers.length; col} total numbers[row][col]; } b. for (int row 0; row numbers.length; row) { for (int col 0; col numbers.length; col} total numbers[row][col]; } c. for (int row 0; row numbers[row].length; row) { for (int col 0; col numbers.length; col} total numbers[row][col]; } d. for (int row 0; row numbers.length; row) { for (int col 0; col numbers[row].length; col} total numbers[row][col]; } ANS: D

45. Which of the following is a valid declaration for a ragged array, after which you would declare each row? a. int[] ragged new int[5]; b. int[][] ragged new int[5][6]; c. int[][] ragged new int[5][];

d. ANS: C

int[][] ragged new int[][5];

Gaddis Starting Out With Java 5 From Control Structures to Objects Chapter 12 Exceptions and More About Stream I/O Multiple Choice
1. A(n) ____ is an object that is generated in memory as the result of an error or an unexpected event. a. Exception handler b. Exception c. Default exception handler d. Error message ANS: B 2. All of the exceptions that you will handle are instances of classes that are derived from a. RunTimeException b. IOException c. Error d. Exception ANS: D 3. In a try/catch construct, after the catch statement is executed a. The program returns to the statement following the statement in which the exception occurred b. The program terminates c. The program resumes at the statement that immediately follows the try/catch construct d. The program resumes at the first statement of the try statement ANS: C 4. The default error message can be retrieved using the ____ method. a. getMessage() b. getErrorMessage() c. getDefaultMessage() d. getDefaultErrorMessage() ANS: A 5. The following catch statement can catch (Exception e) {} a. Handle all error codes by using polymorphic reference as a parameter in the catch clause b. Handle all throwable objects by using polymorphic reference as a parameter in the catch clause c. Handle all exceptions by using polymorphic reference as a parameter in the catch clause

d. ANS: C

Is an error since no objects are instantiated in the Exception class

6. When the code in a try block may throw more than one type of exception, you need to write a catch clause for each type of exception that could potentially be thrown. a. True b. False ANS: A 7. If 14t8 is entered for input in the following code, what does the program do? while (input !null) { try { totalIncome Double.parsedouble(input); months; } catch(NumberFormatException e) { System.out.println(Non-numeric data encountered in the file: e.getMessage()); input inputFile.readLine(); } } a. input will be converted to a double and added to totalIncome, months will be incremented by 1, and the while statement will be repeated until a null is entered b. input will cause a NumberFormatError, the catch clause will be executed, then the program will continue by asking for the next input value c. input will cause a NumberFormatError, the catch clause will be executed, then the program will terminate d. input will cause a NumberFormatError, the catch clause will be executed, then the while statement will be repeated until a value that can be parsed or a null is entered. If a null is entered, the program continues with the statement after the while statement; otherwise, this will be an endless loop. ANS: D 8. If, within one try statement you want to have catch clauses of the following types, in which order should they appear in your program: 1. Exception 2. IllegalArgumentException 3. RuntimeException 4. Throwable a. 1, 2, 3, 4 b. 2, 3, 1, 4 IllegalArgumentException, RutimeException, Exception, Throwable c. 4, 1, 3, 2 d. 3, 1, 2, 4 ANS: B

9. What will be the value of totalIncome after the following values are entered for input: null, 2.5, 8.5, 3.0, 5.5? double totalIncome 0.0; input inputFile.readLine(); while (input !null) { try { totalIncome Double.parsedouble(input); months; } catch(NumberFormatException e) { System.out.println(Non-numeric data encountered in the file: e.getMessage()); } finally { totalIncome 35.5; } input inputFile.readLine(); } a. 19.5 b. 0.0 c. 35.5 d. 75.0 ANS: C

15. Given the following constructor, what must be true about the calling program? public Book(String ISBNOfBook, double priceOfBook, int numberOrderedOfBook) throws BlankISBN, NegativePrice, NegativeNumberOrdered { if (ISBNOfBook ) throw new BlankISBN(); if (priceOfBook 0) throw new NegativePrice(priceOfBook); if (numberedOrderedOfBook 0) throw new NegativeNumberOrdered(numberOrdered); ISBN ISBNOfBook; price priceOfBook; numberedOrdered numberOrderedOfBook; } a. A throws clause should be added to the constructor header b. Classes derived from the Exception class should be created for each of the exceptions in the constructor c. The calling program must handle the throw conditions of the constructor d. All of the above

ANS: D

19. The following two statements can be written as: FileReader freader new FileReader(InputFile.txt); BufferedReader inputFile new BufferedReader(freader); a. BufferedReader inputFile new FileReader(InputFile.txt); b. BufferedReader inputFile new BufferedReader(new FileReader(InputFile.txt)); c. FileReader freader new BufferedReader(InputFile.txt); d. FileReader freader new FileReader(new BufferedReader(inputFile)); ANS: B

23. When writing a string to a binary file or reading a string from a binary file, it is recommended that you use a. Methods that use UTF-8 encoding b. The BufferedReader and PrintWriter methods c. The FileReader and Writer class methods d. The System.In and System.Out methods ANS: A 26. A(n) ____ is a section of code that gracefully responds to exceptions when they are thrown. a. Thrown class b. Default exception handler c. Exception d. Exception handler ANS: D

34. What will be the value of totalIncome after the following values are entered for input: 2.5, 8.5, 3.0, 5.5, null? double totalIncome 0.0; input inputFile.readLine(); while (input !null) { try { totalIncome Double.parsedouble(input); months; } catch(NumberFormatException e) { System.out.println(Non-numeric data encountered in the file: e.getMessage()); } finally {

totalIncome 55.5; } input inputFile.readLine(); } a. b. c. d. 19.5 5.5 55.5 75.0

ANS: C

41. When you use a checked exception class, you must a. Have a throws clause in the method heading b. Override the default error method c. Use each class only once in a method d. Ensure that the error will occur at least once each time the program is executed ANS: A

You might also like