Top Posters
Since Sunday
r
4
L
4
3
d
3
M
3
l
3
V
3
s
3
d
3
a
3
g
3
j
3
A free membership is required to access uploaded content. Login or Register.

AP CSA Semester 1 Test

Uploaded: 5 years ago
Contributor: kbeck24
Category: Anatomy
Type: Lecture Notes
Rating: N/A
Helpful
Unhelpful
Filename:   AP CSA Semester 1 Test.docx (27.08 kB)
Page Count: 13
Credit Cost: 1
Views: 109
Last Download: N/A
Transcript
1. Consider the following method. public static int mystery(int[] arr) { int x = 0; for (int k = 0; k < arr.length; k = k + 2) x = x + arr[k]; return x; } Assume that the array nums has been declared and initialized as follows. int[] nums = {3, 6, 1, 0, 1, 4, 2}; What value will be returned as a result of the call mystery(nums) ? (A) 5 (B) 6 (C) 7 (D) 10 (E) 17 4. Consider the following code segment. int x = 7; int y = 3; if ((x < 10) && (y < 0)) System.out.println("Value is: " + x * y); else System.out.println("Value is: " + x / y); What is printed as a result of executing the code segment? (A) Value is: 21 (B) Value is: 2.3333333 (C) Value is: 2 (D) Value is: 0 (E) Value is: 1 32. Consider the following method. public int compute(int n, int k) { int answer = 1; for (int i = 1; i <= k; i++) answer *= n; return answer; } Which of the following represents the value returned as a result of the call compute(n, k) ? (A) n*k (B) n! (C) nk (D) 2k (E) kn 9. Consider the following method that’s intended to return the sum of the elements in array key. public static int sumArray(int[] key) { int sum = 0; for (int i = 1; i <= key.length; i++) { /* missing code */ } return sum; } Which of the following statements should be used to replace /* missing code */ so that sumArray will work as intended? (A) sum = key[i]; (B) sum += key[i - 1]; (C) sum += key[i]; (D) sum += sum + key[i - 1]; (E) sum += sum + key[i]; 12. Consider the following method. public String mystery(String input) { String output = ""; for (int k = 1; k < input.length(); k = k + 2) { output += input.substring(k, k + 1); } return output; } What is returned as a result of the call mystery("computer") ? (A) "computer" (B) "cmue" (C) "optr" (D) "ompute" (E) Nothing is returned because an IndexOutOfBoundsException is thrown. Questions 2-3 refer to the following information. Consider the following partial class declaration. public class SomeClass { private int myA; private int myB; private int myC; // Constructor(s) not shown public int getA() { return myA; } public void setB(int value) { myB = value; } } 2. The following declaration appears in another class. SomeClass obj = new SomeClass(); Which of the following code segments will compile without error? (A) int x = obj.getA(); (B) int x; obj.getA(x); (C) int x = obj.myA; (D) int x = SomeClass.getA(); (E) int x = getA(obj); 3. Which of the following changes to SomeClass will allow other classes to access but not modify the value of myC ? (A) Make myC public. (B) Include the method: public int getC() { return myC; } (C) Include the method: private int getC() { return myC; } (D) Include the method: public void getC(int x) { x = myC; } (E) Include the method: private void getC(int x) { x = myC; } 7. Consider the following class declaration. public class Person { private String myName; private int myYearOfBirth; public Person(String name, int yearOfBirth) { myName = name; myYearOfBirth = yearOfBirth; } public String getName() { return myName; } public void setName(String name) { myName = name; } // There may be instance variables, constructors, and methods that // are not shown. } Assume that the following declaration has been made. Person student = new Person("Thomas", 1995); Which of the following statements is the most appropriate for changing the name of student from "Thomas" to "Tom" ? (A) student = new Person("Tom", 1995); (B) student.myName = "Tom"; (C) student.getName("Tom"); (D) student.setName("Tom"); (E) Person.setName("Tom"); 4. Consider the following code segment. int x = 7; int y = 3; if ((x < 10) && (y < 0)) System.out.println("Value is: " + x * y); else System.out.println("Value is: " + x / y); What is printed as a result of executing the code segment? (A) Value is: 21 (B) Value is: 2.3333333 (C) Value is: 2 (D) Value is: 0 (E) Value is: 1 8. Consider the following class declaration. public class Student { private String myName; private int myAge; public Student() { /* implementation not shown */ } public Student(String name, int age) { /* implementation not shown */ } // No other constructors } Which of the following declarations will compile without error? I. Student a = new Student(); II. Student b = new Student("Juan", 15); III. Student c = new Student("Juan", "15"); (A) I only (B) II only (C) I and II only (D) I and III only (E) I, II, and III 16. Consider the following incomplete method that is intended to return an array that contains the contents of its first array parameter followed by the contents of its second array parameter. public static int[] append(int[] a1, int[] a2) { int[] result = new int[a1.length + a2.length]; for (int j = 0; j < a1.length; j++) result[j] = a1[j]; for (int k = 0; k < a2.length; k++) result[ /* index */ ] = a2[k]; return result; } Which of the following expressions can be used to replace /* index */ so that append will work as intended? (A) j (B) k (C) k + a1.length - 1 (D) k + a1.length (E) k + a1.length + 1 Questions 10-11 refer to the following information. Consider the following instance variable and methods. You may assume that data has been initialized with length > 0. The methods are intended to return the index of an array element equal to target, or -1 if no such element exists. private int[] data; public int seqSearchRec(int target) { return seqSearchRecHelper(target, data.length - 1); } private int seqSearchRecHelper(int target, int last) { // Line 1 if (data[last] == target) return last; else return seqSearchRecHelper(target, last - 1); } 10. For which of the following test cases will the call seqSearchRec(5) always result in an error? I. data contains only one element. II. data does not contain the value 5. III. data contains the value 5 multiple times. (A) I only (B) II only (C) III only (D) I and II only (E) I, II, and III 11. Which of the following should be used to replace // Line 1 in seqSearchRecHelper so that seqSearchRec will work as intended? (A) if (last <= 0) return -1; (B) if (last < 0) return -1; (C) if (last < data.length) return -1; (D) while (last < data.length) (E) while (last >= 0) 15. Consider the following method, isSorted, which is intended to return true if an array of integers is sorted in nondecreasing order and to return false otherwise. /** @param data an array of integers * @return true if the values in the array appear in sorted (nondecreasing) order */ public static boolean isSorted(int[] data) { /* missing code */ } Which of the following can be used to replace /* missing code */ so that isSorted will work as intended? I. for (int k = 1; k < data.length; k++) { if (data[k - 1] > data[k]) return false; } return true; II. for (int k = 0; k < data.length; k++) { if (data[k] > data[k + 1]) return false; } return true; III. for (int k = 0; k < data.length - 1; k++) { if (data[k] > data[k + 1]) return false; else return true; } return true; (A) I only (B) II only (C) III only (D) I and II only (E) I and III only 17. Consider the following code segment. int[] arr = {1, 2, 3, 4, 5, 6, 7}; for (int k = 3; k < arr.length - 1; k++) arr[k] = arr[k + 1]; Which of the following represents the contents of arr as a result of executing the code segment? (A) {1, 2, 3, 4, 5, 6, 7} (B) {1, 2, 3, 5, 6, 7} (C) {1, 2, 3, 5, 6, 7, 7} (D) {1, 2, 3, 5, 6, 7, 8} (E) {2, 3, 4, 5, 6, 7, 7} 19. Assume that a and b have been defined and initialized as int values. The expression !(!(a != b ) && (b > 7)) is equivalent to which of the following? (A) (a != b) || (b < 7) (B) (a != b) || (b <= 7) (C) (a == b) || (b <= 7) (D) (a != b) && (b <= 7) (E) (a == b) && (b > 7) 20. Consider the following method. public static void arrayMethod(int nums[]) { int j = 0; int k = nums.length - 1; while (j < k) { int x = nums[j]; nums[j] = nums[k]; nums[k] = x; j++; k--; } } Which of the following describes what the method arrayMethod() does to the array nums? (A) The array nums is unchanged. (B) The first value in nums is copied to every location in the array. (C) The last value in nums is copied to every location in the array. (D) The method generates an ArrayIndexOutOfBoundsException. (E) The contents of the array nums are reversed. 26. Assume that the array arr has been defined and initialized as follows. int[] arr = /* initial values for the array */ ; Which of the following will correctly print all of the odd integers contained in arr but none of the even integers contained in arr ? (A) for (int x : arr) if (x % 2 == 1) System.out.println(x); (B) for (int k = 1; k < arr.length; k++) if (arr[k] % 2 == 1) System.out.println(arr[k]); (C) for (int x : arr) if (x % 2 == 1) System.out.println(arr[x]); (D) for (int k = 0; k < arr.length; k++) if (arr[k] % 2 == 1) System.out.println(k); (E) for (int x : arr) if (arr[x] % 2 == 1) System.out.println(arr[x]); 29. Consider the following code segment. for (int k = 1; k <= 100; k++) if ((k % 4) == 0) System.out.println(k); Which of the following code segments will produce the same output as the code segment above? (A) for (int k = 1; k <= 25; k++) System.out.println(k); (B) for (int k = 1; k <= 100; k = k + 4) System.out.println(k); (C) for (int k = 1; k <= 100; k++) System.out.println(k % 4); (D) for (int k = 4; k <= 25; k = 4 * k) System.out.println(k); (E) for (int k = 4; k <= 100; k = k + 4) System.out.println(k); Questions 27-28 refer to the following method. public static int mystery(int n) { int x = 1; int y = 1; // Point A while (n > 2) { x = x + y; // Point B y = x – y; n--; } // Point C return x; } 27. What value is returned as a result of the call mystery(6) ? (A) 1 (B) 5 (C) 6 (D) 8 (E) 13 28. Which of the following is true of method mystery ? (A) x will sometimes be 1 at // Point B. (B) x will never be 1 at // Point C. (C) n will never be greater than 2 at // Point A. (D) n will sometimes be greater than 2 at // Point C. (E) n will always be greater than 2 at // Point B. 13. Consider the following code segment. int[] arr = {7, 2, 5, 3, 0, 10}; for (int k = 0; k < arr.length - 1; k++) { if (arr[k] > arr[k + 1]) System.out.print(k + " " + arr[k] + " "); } What will be printed as a result of executing the code segment? (A) 0 2 2 3 3 0 (B) 0 7 2 5 3 3 (C) 0 7 2 5 5 10 (D) 1 7 3 5 4 3 (E) 7 2 5 3 3 0 37. Consider the following code segment. int x = 1; while ( /* missing code */ ) { System.out.print(x + " "); x = x + 2; } Consider the following possible replacements for /* missing code */. I. x < 6 II. x != 6 III. x < 7 Which of the proposed replacements for /* missing code */ will cause the code segment to print only the values 1 3 5 ? (A) I only (B) II only (C) I and II only (D) I and III only (E) I, II, and III 31. Consider the following method. public void mystery(int[] data) { for (int k = 0; k < data.length - 1; k++) data[k + 1] = data[k] + data[k + 1]; } The following code segment appears in another method in the same class. int[] values = {5, 2, 1, 3, 8}; mystery(values); for (int v : values) System.out.print(v + " "); System.out.println(); What is printed as a result of executing the code segment? (A) 5 2 1 3 8 (B) 5 7 3 4 11 (C) 5 7 8 11 19 (D) 7 3 4 11 8 (E) Nothing is printed because an ArrayIndexOutOfBoundsException is thrown during the execution of method mystery. 30. Consider the following method. public static String scramble(String word, int howFar) { return word.substring(howFar + 1, word.length()) + word.substring(0, howFar); } What value is returned as a result of the call scramble("compiler", 3)? (A) "compiler" (B) "pilercom" (C) "ilercom" (D) "ilercomp" (E) No value is returned, an IndexOutOfBoundsException will be thrown. 33. Consider the following code segment. int sum = 0; int k = 1; while (sum < 12 || k < 4) sum += k; System.out.println(sum); What is printed as a result of executing the code segment? (A) 6 (B) 10 (C) 12 (D) 15 (E) Nothing is printed due to an infinite loop. 35. Consider the following code segment. int num = 2574; int result = 0; while (num > 0) { result = result * 10 + num % 10; num /= 10; } System.out.println(result); What is printed as a result of executing the code segment? (A) 2 (B) 4 (C) 18 (D) 2574 (E) 4752 38. Assume that x and y have been declared and initialized with int values. Consider the following Java expression. (y > 10000) || (x > 1000 && x < 1500) Which of the following is equivalent to the expression given above? (A) (y > 10000 || x > 1000) && (y > 10000 || x < 1500) (B) (y > 10000 || x > 1000) || (y > 10000 || x < 1500) (C) (y > 10000) && (x > 1000 || x < 1500) (D) (y > 10000 && x > 1000) || (y > 10000 && x < 1500) (E) (y > 10000 && x > 1000) && (y > 10000 && x < 1500) 36. Consider the following method. public void test(int x) { int y; if (x % 2 == 0) y = 3; else if (x > 9) y = 5; else y = 1; System.out.println("y = " + y); } Which of the following test data sets would test each possible output for the method? (A) 8, 9, 12 (B) 7, 9, 11 (C) 8, 9, 11 (D) 8, 11, 13 (E) 7, 9, 10

Related Downloads
Explore
Post your homework questions and get free online help from our incredible volunteers
  1605 People Browsing
Your Opinion
How often do you eat-out per week?
Votes: 81

Previous poll results: What's your favorite math subject?