Answer Key Review Final Exam Fall AP Computer Science Mr. Locascio 1. Name: ________________________________ Period: _____ Date: ____________________ Consider the following method of a MyArrays class: public static void doSomething(int a[]) { a[2] = a[1]; a[3] = a[2]; } If a is declared as int a[] = {1, 2, 3, 4}; what are the values in a after MyArrays.doSomething(a) is called? 2. A. B. C. D. 1, 1, 1, 4 1, 1, 2, 4 1, 2, 2, 3 1, 2, 2, 2 What is the value of sum after the following code fragment is executed? int sum = 0, x = 1; while (sum < 20 && x <= 5) { sum += 2 * x; x++; } 3. A. B. C. D. 0 12 20 30 What is the value of the int variable d after the following loop is executed? for (d = 1; d < 567; d *= 10) { < … Missing code > } A. B. C. D. 1 10 1000 Unpredictable 4. What is displayed after the following code fragment is executed? int n = 4; int k, fact = 1; for (k = n; k >= 1; k++) fact *= k; System.out.println(n + "! = " + fact); A. B. C. D. 4! = 0 4! = 1 4! = 24 Nothing: the program hangs for quite a while Questions 5 - 8 deal with a method getLengths that takes an array of strings as its argument and returns an array of integers, the lengths of the corresponding strings. 5. Which of the following can serve as a header for getLengths? 6. int getLengths(String words[]) int getLengths[] (String words[]) int getLengths(String words[]) [] int[] getLengths(String words[]) A. B. C. D. int wLengths[] int wLengths = int[] wLengths int wLengths[] = new int[words.length]; new int[words.length]; = new int[words[0].length]; = new int[words[0].length()]; Suppose the getLengths method sets wLengths[k] to the length of the k-th word (within a loop). Which of the following could serve as that assignment statement? 8. public public public public Which of the following could serve as the first statement in getLengths? 7. A. B. C. D. A. B. C. D. wLengths[k] wLengths[k] wLengths[k] wLengths[k] = = = = words[k].length; words.length(k); words[k].length(); words.length[k]; Which of the following could serve as the return statement in getLengths? A. B. C. D. return return return return words.length; wLengths; wLengths[]; wLengths[wLengths.length]; 9. The method below is supposed to return the sum of all the elements in a 2-D array, but it has a bug: public static int addAll(int m[][]) { int r, c; int sum = 0; for (r = 0; r < m.length; r++) for (c = 0; r < m[0].length; r++) sum += m[r][c]; return sum; } What will be its return value for an array with elements 1 2 3 4 5 6 10. A. B. C. D. 1 3 4 8 Consider the following code segment: String s1 = “abc”; String s2 = “abc”; String s3 = “ABC”; After this code executes, which of the following expressions would evaluate to true? I. s1.equals(s2) II. s1 == s2 III. s1 == s3 A. B. C. D. E. I only II only III only I & II only I, II and III 11. The following code fragment is intended to find the smallest value in arr[0] … arr[n-1]. //Precondition: arr[0] … arr[n-1] initialized with integers // arr is an array, arr.length = n //Postcondition: min = smallest value in arr[0] … arr[n-1] int min = arr[0]; int i = 1; while (i < n) { i++; if (arr[i] < min) { min = arr[i]; } } This code is incorrect. For the segment to work as intended, which of the following modifications could be made? I. Change the line int i = 1; to int i = 0; Make no other changes. II. Change the body of the while loop to { if (arr[i] < min) { min = arr[i]; } i++; } Make no other changes. III. Change the test for the while loop as follows: while (i <= n) Make no other changes. A. B. C. D. E. I only II only III only I or II only I, II or III 12. Write a method that tests whether a given string contains only digits. public boolean allDigits(String s) //precondition: method receives a string //postcondition: returns true if each character in the string // is a digit // returns false if there are any non-digit // characters in the string { char d = ‘ ‘; for (int loop = 0; loop < s.length(); loop ++) { d = s.charAt(loop); if ( ! Character.isDigit(d)) return false; } return true; } 13. Write a method that determines the number of negative values in an array of integers. public int negNumCount(int grid[]) //precondition: method receives an array of integers // //postcondition: returns the number of negative values in the // array //Example: an array containing {3, -5, -2, 7} // // has two negative values { int neg = 0; for(int loop = 0; loop < grid.length; loop++) if (grid[loop] < 0) neg ++; return neg; } 14. Write a method that determines the average of all values in an array. public double average(int nums[]) //precondition: method receives an array of integers //postcondition: returns the average of all values in the // array //Example: an array containing {-1, 5, 2, -3, 7} // has an average of 2.0 { int total = 0; for(int loop=0; loop<nums.length; loop++) total += nums[loop]; return (double)total/nums.length; } 15. True or false? In OOP, an object’s data elements may be other objects. The functionality of an object is defined by the class to which it belongs. (a) (b) (c) A program can employ several objects of the same class. 16. What is a class’s constructor? A. A utility program that generates Java code from formal specifications B. The main method in a GUI application C. A special method for creating objects of a class D. An IDE component for prototyping GUI 17. What does an import statement do? A. Makes an object created in one method available to another method B. Specifies a fully-qualified name of a class so that the program can refer to that class by its short name C. Makes a private method of a class available to an object of another class D. Copies a library class into the current project directory 18. Which package is automatically imported into all Java source files? A. B. C. D. java.lang java.util java.awt javax.swing 19. What is the primary reason for making certain methods of a class private as opposed to public? A. The class compiles faster. B. The code of a private method can be changed without changing other classes in the program. C. A private method has access to private fields. D. Private methods are not inherited in derived classes. 20. True or false? 21. (a) (b) (c) A class may have only one constructor. A programmer defines methods for each individual object in the program. A class may have several private methods. Why is balance_due not a good name for a variable? A. Not commonly accepted Java style B. Underscore is not allowed in names C. Underscore is not allowed in the middle of a name D. Too long 22. What is the binary representation of 123? A. B. C. D. 10110111 1111011 7B 1101110 23. What is the hexadecimal representation of 7531? A. B. C. D. 1D6B 8C9 A2B1 CC1