AP Computer Science Practice Exam A

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

int[] oldArray = {1, 2, 3, 4,5, 6, 7, 8, 9}; int[][] newArray = new int[3][3]; int row = 0; int col = 0; for (int value : oldArray) { newArray[row][col] = value; row++; if ((row % 3) == 0) { col++; row = 0; } } What is printed as a result of executing the code segment? (A) 3 (B) 4 (C) 5 (D) 7 (E) 8

(D) 7

Which of thefollowing can be used to replace /* missing code */ so that isSorted will work as inteded? 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; } return true (A) I only (B) II only (C) III only (D) I and II (E) I and III only

(A) I only

Assume that the array ar has been defined and initialized as follows. int[] arr = /* initial values for the array */ Which of the following with 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 != 0) System.out.printIn(x) (B) for (int k = 1; k < arr.length; k++) if (arr[k] % 2 != 0) System.out.printIn(arr[k]); (C) for (int x : arr) if (x % 2 != 0) System.out.printIn(arr[x]); (D) for (int k = 0; k < arr.length; k++) if (arr[k] % 2 != 0 System.out.printIn(k); (E) for (int x : arr) if (arr[x] % 2 != 0) System.out.printIn(arr[x]);

(A) for (int x: arr) if (x % 2 != 0) System.out.printIn(x)

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; } 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);

(A) int x = obj.getA

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)

(B) (a != b) || (b <= 7)

Assume myList is an ArrayList that has been correctly constructed and populated with objects. Which of the following expression produces a valid random index for myList? Assume myList is an ArrayList that has been correctly constructed and populated with objects. Which of the following expression produces a valid random index for myList? (A) (int) ( Math.random() * myList.size() ) - 1 (B) (int) ( Math.random() * myList.size() ) (C) (int) ( Math.random() * myList.size() ) + 1 (D) (int) ( Math.random() * (myListsize() + 1) ) (E) Math.random(myList.size())

(B) (int) ( Math.random() * myList.size() )

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

(B) 0 7 2 5 3 3

Consider the following code segment. int x = 7; int y = 3; if ((x < 10) && (y < 0)) System.out.printIn("Value is: " + x * y); else System.out.printIn("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

(B) 2.3333333

Which of the following will 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

(B) II only

Consider the following code segment that appears in a class other than Book or AudioBook. Line1: [Book] books = new Book[2]; Line2: books[0] = new AudioBook(100, 300, "The Jungle"); Line3: books[1] = new Book(400, "Captains Courageous"); Line4: System.out.printin(books[0].pages.PerMinute()); Line5: System.out.printin(books[0].toString()); Line6: System.out.printin(books[0].length()); Line7: System.out.printin(books[1].toString()); Which of the following best explains why the code segment will not compile? (A) Line 2 will not compile because variables of type Book may new refer to variables of type AudoBook. (B) Line 4 will not compile because the variables of type Book may only call methods in the Book class. (C) Line 5 will not compile because the AudioBook class does not have a method named toString delcared or implemented. (D) Line 6 will not compile because the statement is ambiguos. The complier cannot determine which length method should be called. (E) Line 7 will not compile because the element at index 1 in the array named books may not have been initialized.

(B) Line 4 will not compile because the variables of type Book may only call methods in the Book class.

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)

(B) if (last < 0) return -1;

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) public int getC() { return myC; } (C) private int getC() { return myC; } (D) public void getC(int x) (E) private void getC(int x) { x = myC; }

(B) public int getC() { return myC; }

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

(C) I and II only

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 replaced the /* 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];

(B) sum += key[i - 1];

public String mystery(String imput) { 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

(C) "optr"

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}; (A) 5 (B) 6 (C) 7 (D) 10 (E) 17

(C) 7

Consider the following instance variable and method. private List<String> animals; public void manipulate() { for (int k = animals.size() - 1; k > 0; k--) { if (animals.get(k).substring(0, 1).equals("b")) { animals.add(animals.size() - k, animals.remove(k)}; } } } Assume that animals has ben instantiated and initialized with the following contents. (A) ["baboon", "zebra", "bass", "cat", "bear", "koala"] (B) ["bear", "zebra", "bass", "cat", "koala", "baboon"] (C) ["baboon", "bear", "zebra", "bass", "cat", "koala"] (D) ["bear", "baboon", "zebra", "bass", "cat", "koala"] (E) ["zebra", "cat", "koala", "baboon", "bass", "bear"]

(C) ["baboon", "bear", "zebra", "bass", "cat", "koala"]

Consider the following method. public ArrayList<Integer> mystery(int n) { ArrayList<Integer> seq = new ArrayList<Integer>(); for (int k = 1; k <= n; k++) seq.add(new Integer(k * k + 3)); return seq } Which of the folowing is printed as a result of executing the following statement? System.out.printIn.(mystery(6)); (A) [3, 4, 7, 12, 19, 28] (B) [3, 4, 7, 12, 19, 28, 39] (C) [4, 7, 12, 19, 28, 39] (D) [39, 28, 19, 12, 7, 4] (E) [39, 28, 19, 12, 7, 4, 3]

(C) [4, 7, 12, 19, 28, 39]

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}

(C) {1, 2, 3, 5, 6, 7, 7}

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; } What value is returned as a result of the call mystery(6) ? (A) 1 (B) 5 (C) 6 (D) 8 (E) 13

(D) 8

Which of the interfaces if correctly implemented by a Box class would be sufficient functionality for a user of the Box class to determine if one Box can fit inside another? (A) I only (B) II only (C) III only (D) I and II only

(D) I and II only

Consider the following method, which is intended to return the element of a 2-dimensional array that is closest in value to a specified number, val. /** @return the element of 2-dimensional array mat whose value is closest to val */ public double findCloset(double[][] mat, double val) { double answer = mat[0][0]; double minDiff = Math.abs(answer - val); for (double[] row : mat) { for (double num : row) { answer = num; minDiff = Math.abs(num - val); } } } return answer; } Which of the following could be used to replace /* missing */ so that findClosest will work as intended? (A)val - row[num] < minDiff (B) Math.abs(num - minDIff) < minDiff (C) val - num < 0.0 (D) Math.abs(num - val) < minDiff (E) Math.abs(row[num] - val) < minDiff

(D) Math.abs(num - val) < minDiff

Consider the following incomplete method that is intended to return an array that contains the contents of its first array parameter follow by contents of its second array parameter. public static int[] appen(int[] al, int[] a2) { int[] result = new int[al.length + a2.length] for (int j = 0; j < al.length; j++) result[j] = al[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 + al.length - 1 (D) k + al.length (E) k + al.length + 1

(D) k + al.length

Which of the following statements is the most appropiate for changing the name of student "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");

(D) student.setName("Tom");

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[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.

(E) The contents of the array nums are reversed.

public interface Vechile { /** @return the mileage traveled by this Vehicle */ double getMileage(); } public class Fleet { private ArrayList<Vehicle> myVehicles; /** @return the mileage traveled by all vehicles in this Fleet */ public double getTotalMileage() { double sum = 0.0; for sum = 0.0 for (Vehicle v : myVehicles) { sum += /* expression */ ; } return sum; } // There may be istance variables constructors, and methods that are not shown. } Which of the following can be used to replace /*/ expression */ so that getTotalMileage returns the total of miles traveled for all vehicles in the fleet? (A) getMileage(v) (B) myVehicles[v].getMileage() (C) Vehicle.get(v).getMileage() (D) myVehicles.get(v).getMileage() (E) v.getMileage()

(E) v.getMileage()


Ensembles d'études connexes

Tenses: Present Simple and Present Continuous

View Set

Psychology of applied modern life

View Set

Epi lecture 4 - the 2x2 contingency table

View Set

Ch 4: Conversions and Calculations Used by Pharmacy Technicians

View Set