AP Computer Science Sample MC

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Consider the following code segment. for (int k = 0; k< 20; k= k+ 2){ if (k % 3== 1) { System.out.print(k+ " "); } } What is printed as a result of executing the code segment? a. 4 16 b. 4 10 16 c. 0 6 12 18 d. 1 4 7 10 13 16 19 e. 0 2 4 6 8 10 12 14 16 18

b

Consider the following instance variable and method. private int[] array; /** Precondition: array.length > 0 */ public int checkArray() { int loc = array.length / 2; for (int k = 0; k < array.length; k++) { if (array[k] > array[loc]) { loc = k; } } return loc; } Which of the following is the best postcondition for checkArray? a. Returns the index of the first element in array array whose value is greater than array[loc] b. Returns the index of the last element in array array whose value is greater than array[loc] c. Returns the largest value in array array d. Returns the index of the largest value in array array e. Returns the index of the largest value in the second half of array array

d

The following sort method correctly sorts the integers in elements into ascending order. Line 1: public static void sort(int[] elements) Line 2: { Line 3: for (int j = 0; j < elements.length - 1; j++) Line 4: { Line 5: int index = j; Line 6: Line 7: for (int k = j + 1; k < elements.length; k++) Line 8: { Line 9: if (elements[k] < elements[index]) Line 10: { Line 11: index = k; Line 12: } Line 13: } Line 14: Line 15: int temp = elements[j]; Line 16: elements[j] = elements[index]; Line 17: elements[index] = temp; Line 18: } Line 19: } Which of the following changes to the sort method would correctly sort the integers in elements into descending order? I. Replace line 9 with: Line 9: if (elements[k] > elements[index]) II. Replace lines 15-17 with: Line 15: int temp = elements[index]; Line 16: elements[index] = elements[j]; Line 17: elements[j] = temp; III. Replace line 3 with: Line 3: for (int j = elements.length − 1; j > 0; j−−) and replace line 7 with: Line 7: for (int k = 0; k < j; k++) a. I only b. II only c. I and II only d. I and III only e. I, II, and III

d

Consider the following Util class, which contains two methods. The completed sum1D method returns the sum of all the elements of the 1-dimensional array a. The incomplete sum2D method is intended to return the sum of all the elements of the 2-dimensional array m. public class Util { /** Returns the sum of the elements of the 1-dimensional array a */ public static int sum1D(int[] a) { /* implementation not shown */ } /** Returns the sum of the elements of the 2-dimensional array m */ public static int sum2D(int[][] m) { int sum = 0; /* missing code */ return sum; } } Assume that sum1D works correctly. Which of the following can replace /*missing code*/ so that the sum2D method works correctly? I. for (int k = 0; k < m.length; k++) { sum += sum1D(m[k]); } II. for (int[] row : m) { sum += sum1D(row); } II. for (int[] row : m) { for (int v : row) { sum += v; } } a. I only b. II only c. I and II only d. II and III only e. I, II, and III

e

Consider the following methods. public void changer(String x, int y) { x = x + "peace"; y = y * 2; } public void test() { String s = "world"; int n = 6; changer(s, n); /* End of method */ } When the call test() is executed, what are the values of s and n at the point indicated by /* End of method */ ? s: n: a. world 6 b. worldpeace 6 c. world 12 d. worldpeace 12 e. peace 12

a

Consider the following declarations. public interface Shape { int isLargerThan(Shape other); // Other methods not shown } public class Circle implements Shape { // Other methods not shown } Which of the following method headings of isLargerThan can be added to the declaration of the Circle class so that it will satisfy the Shape interface? I. public int isLargerThan(Shape other) II. public int isLargerThan(Circle other) III. public boolean isLargerThan(Object other) a. I only b. II only c. III only d. I and II only e. I, II, and II

a

Consider the following code segment. int[][] mat = new int[3][4]; for (int row = 0; row < mat.length; row++) { for (int col = 0; col < mat[0].length; col++) { if (row < col) { mat[row][col] = 1; } else if (row == col) { mat[row][col] = 2; } else { mat[row][col] = 3; } } } What are the contents of mat after the code segment has been executed? a. {{2, 1, 1}, {3, 2, 1}, {3, 3, 2}, {3, 3, 3}} b. {{2, 3, 3}, {1, 2, 3}, {1, 1, 2}, {1, 1, 1}} c. {{2, 3, 3, 3}, {1, 2, 3, 3}, {1, 1, 2, 3}} d. {{2, 1, 1, 1}, {3, 2, 1, 1}, {3, 3, 2, 1}} e. {{1, 1, 1, 1}, {2, 2, 2, 2}, {3, 3, 3, 3}}

d

A car dealership needs a program to store information about the cars for sale. For each car, they want to keep track of the following information: number of doors (2 or 4), whether the car has air conditioning, and its average number of miles per gallon. Which of the following is the best object-oriented program design? a. Use one class, Car, with three instance variables: int numDoors, boolean hasAir, and double milesPerGallon. b. Use four unrelated classes: Car, Doors, AirConditioning, and MilesPerGallon. c. Use a class Car with three subclasses: Doors, AirConditioning, and MilesPerGallon. d. Use a class Car, with a subclass Doors, with a subclass AirConditioning, with a subclass MilesPerGallon. e. Use three classes: Doors, AirConditioning, and MilesPerGallon, each with a subclass Car.

a

Consider the following recursive method. public static int mystery(int n) { if (n <= 1) { return 0; } else { return 1 + mystery(n / 2); } } Assuming that k is a nonnegative integer and m = 2^k, what value is returned as a result of the call mystery(m)? a. 0 b. k c. m d. m/2 + 1 e. k/2 +1

b

Consider the following instance variable and method. private int[] numbers; /** Precondition: numbers contains int values in no particular order. */ public int mystery(int num) { for (int k = numbers.length − 1; k >= 0; k−−) { if (numbers[k] < num) { return k; } } return -1; } Which of the following best describes the contents of numbers after the following statement has been executed? int m = mystery(n); a. All values in positions 0 through m are less than n. b. All values in positions m+1 through numbers.length-1 are less than n. c. All values in positions m+1 through numbers.length-1 are greater than or equal to n. d. The smallest value is at position m. e. The largest value that is smaller than n is at position m.

c

Consider the following two classes. public class Dog { public void act() { System.out.print("run "); eat(); } public void eat() { System.out.print("eat "); } } public class UnderDog extends Dog { public void act() { super.act(); System.out.print("sleep "); } public void eat() { super.eat(); System.out.print("bark "); } } Assume that the following declaration appears in a class other than Dog. Dog fido = new UnderDog(); What is printed as a result of the call fido.act()? a. run eat b. run eat sleep c. run eat sleep bark d. run eat bark sleep e. Nothing is printed due to infinite recursion

d

Consider the following code segment. List<String> animals = new ArrayList<String>(); animals.add("dog"); animals.add("cat"); animals.add("snake"); animals.set(2, "lizard"); animals.add(1, "fish"); animals.remove(3); System.out.println(animals); What is printed as a result of executing the code segment? a. [dog, fish, cat] b. [dog, fish, lizard] c. [dog, lizard, fish] d. [fish, dog, cat] e. The code throws an ArrayIndexOutOfBoundsException exception.

a

Consider the following method. /** Precondition: arr contains only positive values. */ public static void doSome(int[] arr, int lim) { int v = 0; int k = 0; while (k < arr.length && arr[k] < lim) { if (arr[k] > v) { v = arr[k]; /* Statement S */ } k++; /* Statement T */ } } Assume that doSome is called and executes without error. Which of the following are possible combinations for the value of lim, the number of times Statement S is executed, and the number of times Statement T is executed? lim: S: T: I. 5 0 5 II. 7 4 9 III. 3 5 2 a. I only b. II only c. III only d. I and III only e. II and III only

b

At a certain high school students receive letter grades based on the following scale. Integer Score --> Letter Grade 93 or above --> A From 84 to 92 inclusive --> B From 75 to 83 inclusive --> C Below 75 --> F Which of the following code segments will assign the correct string to grade for a given integer score? I. if (score >= 93) grade = "A"; if (score >= 84 && score <= 92) grade = "B"; if (score >= 75 && score <= 83) grade = "C"; if (score < 75) grade = "F"; II. if (score >= 93) grade = "A"; if (84 <= score <= 92) grade = "B"; if (75 <= score <= 83) grade = "C"; if (score < 75) grade = "F"; III. if (score >= 93) grade = "A"; else if (score >= 84) grade = "B"; else if (score >= 75) grade = "C"; else grade = "F"; a. II only b. III only c. I and II only d. I and III only e. I, II, and III

d

Consider the following method. /** @param x an int value such that x >= 0 */ public void mystery(int x) { System.out.print(x % 10); if ((x / 10) != 0) { mystery(x / 10); } System.out.print(x % 10); } Which of the following is printed as a result of the call mystery(1234)? a. 1234 b. 4321 c. 12344321 d. 43211234 e. Many digits are printed due to infinite recursion.

d

Consider the following instance variable and method. private int[] arr; /** Precondition: arr contains no duplicates; * the elements in arr are in ascending order. * @param low an int value such that 0 < low < arr.length * @param high an int value such that low - 1 < high < arr.length * @param num an int value */ public int mystery(int low, int high, int num) { int mid = (low + high) / 2; if (low > high) { return low; } else if (arr[mid] < num) { return mystery(mid + 1, high, num); } else if (arr[mid] > num) { return mystery(low, mid − 1, num); } else // arr[mid] == num { return mid; } } What is returned by the call mystery(0, arr.length − 1, num)? a. The number of elements in arr that are less than num b. The number of elements in arr that are less than or equal to num c. The number of elements in arr that are equal to num d. The number of elements in arr that are greater than num e. The index of the middle element in arr

a

Consider the following instance variable nums and method findLongest with line numbers added for reference. Method findLongest is intended to find the longest consecutive block of the value target occurring in the array nums; however, findLongest does not work as intended. For example, if the array nums contains the values [7, 10, 10, 15, 15, 15, 15, 10, 10, 10, 15, 10, 10], the call findLongest(10) should return 3, the length of the longest consecutive block of 10s. private int[] nums; public int findLongest(int target) { int lenCount = 0; int maxLen = 0; Line 1: for (int val : nums) Line 2: { Line 3: if (val == target) Line 4: { Line 5: lenCount++; Line 6: } Line 7: else Line 8: { Line 9: if (lenCount > maxLen) Line 10: { Line 11: maxLen = lenCount; Line 12: } Line 13: } Line 14: } Line 15: if (lenCount > maxLen) Line 16: { Line 17: maxLen = lenCount; Line 18: } Line 19: return maxLen; } The method findLongest does not work as intended. Which of the following best describes the value returned by a call to findLongest? a. It is the length of the shortest consecutive block of the value target in nums. b. It is the length of the array nums. c. It is the number of occurrences of the value target in nums. d. It is the length of the first consecutive block of the value target in nums. e. It is the length of the last consecutive block of the value target in nums.

c

public class TimeRecord { private int hours; private int minutes; // 0 < minutes < 60 /** Constructs a TimeRecord object. * @param h the number of hours * Precondition: h > 0 * @param m the number of minutes * Precondition: 0 < m < 60 */ public TimeRecord(int h, int m) { hours = h; minutes = m; } /** @return the number of hours */ public int getHours() { /* implementation not shown */ } /** @return the number of minutes * Postcondition: 0 < minutes < 60 */ public int getMinutes() { /* implementation not shown */ } /** Adds h hours and m minutes to this TimeRecord. * @param h the number of hours * Precondition: h > 0 * @param m the number of minutes * Precondition: m > 0 */ public void advance(int h, int m) { hours = hours + h; minutes = minutes + m; /* missing code */ } // Other methods not shown } Which of the following can be used to replace /* missing code */ so that advance will correctly update the time? a. minutes = minutes % 60; b. minutes = minutes + hours % 60; c. hours = hours + minutes / 60; minutes = minutes % 60; d. hours = hours + minutes % 60; minutes = minutes / 60; e. hours = hours + minutes / 60;

c

Consider the following declaration for a class that will be used to represent points in the xy-coordinate plane. public class Point { private int x; // x-coordinate of the point private int y; // y-coordinate of the point public Point() { x = 0; y = 0; } public Point(int a, int b) { x = a; y = b; } // Other methods not shown } The following incomplete class declaration is intended to extend the above class so that points can be named. public class NamedPoint extends Point { private String name; // name of point // Constructors go here // Other methods not shown } Consider the following proposed constructors for this class. I. public NamedPoint() { name = ""; } II. public NamedPoint(int d1, int d2, String pointName) { x = d1; y = d2; name = pointName; } III. public NamedPoint(int d1, int d2, String pointName) { super(d1, d2); name = pointName; } Which of these constructors would be legal for the NamedPoint class? a. I only b. II only c. III only d. I and III only e. II and III only

d

Consider the following instance variable, arr, and incomplete method, partialSum. The method is intended to return an integer array sum such that for all k, sum[k] is equal to arr[0] + arr[1] + ... + arr[k]. For instance, if arr contains the values { 1, 4, 1, 3 }, the array sum will contain the values { 1, 5, 6, 9 }. private int[] arr; public int[] partialSum() { int[] sum = new int[arr.length]; for (int j = 0; j < sum.length; j++) { sum[j] = 0; } /* missing code */ return sum; } The following two implementations of /* missing code */ are proposed so that partialSum will work as intended. Implementation 1 for (int j = 0; j < arr.length; j++) { sum[j] = sum[j - 1] + arr[j]; } Implementation 2 for (int j = 0; j < arr.length; j++) { for (int k = 0; k <= j; k++) { sum[j] = sum[j] + arr[k]; } } Which of the following statements is true? a. Both implementations work as intended, but implementation 1 is faster than implementation 2. b. Both implementations work as intended, but implementation 2 is faster than implementation 1. c. Both implementations work as intended and are equally fast. d. Implementation 1 does not work as intended, because it will cause an ArrayIndexOutOfBoundsException. e. Implementation 2 does not work as intended, because it will cause an ArrayIndexOutOfBoundsException.

d

Consider the following method. public static void mystery(List<Integer> nums) { for (int k = 0; k < nums.size(); k++) { if (nums.get(k).intValue() == 0) { nums.remove(k); } } } Assume that a List<Integer> values initially contains the following Integer values. [0, 0, 4, 2, 5, 0, 3, 0] What will values contain as a result of executing mystery(values)? a. [0, 0, 4, 2, 5, 0, 3, 0] b. [4, 2, 5, 3] c. [0, 0, 0, 0, 4, 2, 5, 3] d. [0, 4, 2, 5, 3] e. The code throws an ArrayIndexOutOfBoundsException exception.

d

Consider the following output. 1 1 1 1 1 2 2 2 2 3 3 3 4 4 5 Which of the following code segments will produce this output? a. for (int j = 1; j <= 5; j++) { for (int k = 1; k <= 5; k++) { System.out.print(j + " "); } System.out.println(); } b. for (int j = 1; j <= 5; j++) { for (int k = 1; k <= j; k++) { System.out.print(j + " "); } System.out.println(); } c. for (int j = 1; j <= 5; j++) { for (int k = 5; k >= 1; k--) { System.out.print(j + " "); } System.out.println(); } d. for (int j = 1; j <= 5; j++) { for (int k = 5; k >= j; k--) { System.out.print(j + " "); } System.out.println(); } e. for (int j = 1; j <= 5; j++) { for (int k = j; k <= 5; k++) { System.out.print(k + " "); } System.out.println(); }

d

public class TimeRecord { private int hours; private int minutes; // 0 < minutes < 60 /** Constructs a TimeRecord object. * @param h the number of hours * Precondition: h > 0 * @param m the number of minutes * Precondition: 0 < m < 60 */ public TimeRecord(int h, int m) { hours = h; minutes = m; } /** @return the number of hours */ public int getHours() { /* implementation not shown */ } /** @return the number of minutes * Postcondition: 0 < minutes < 60 */ public int getMinutes() { /* implementation not shown */ } /** Adds h hours and m minutes to this TimeRecord. * @param h the number of hours * Precondition: h > 0 * @param m the number of minutes * Precondition: m > 0 */ public void advance(int h, int m) { hours = hours + h; minutes = minutes + m; /* missing code */ } // Other methods not shown } Consider the following declaration that appears in a class other than TimeRecord. TimeRecord[] timeCards = new TimeRecord[100]; Assume that timeCards has been initialized with TimeRecord objects. Consider the following code segment that is intended to compute the total of all the times stored in timeCards. TimeRecord total = new TimeRecord(0,0); for (int k = 0; k < timeCards.length; k++) { /* missing expression */ ; } Which of the following can be used to replace /* missing expression */ so that the code segment will work as intended? a. timeCards[k].advance() b. total += timeCards[k].advance() c. total.advance(timeCards[k].hours, timeCards[k].minutes) d. total.advance(timeCards[k].getHours(), timeCards[k].getMinutes()) e. timeCards[k].advance(timeCards[k].getHours(), timeCards[k].getMinutes())

d

Consider the following instance variable nums and method findLongest with line numbers added for reference. Method findLongest is intended to find the longest consecutive block of the value target occurring in the array nums; however, findLongest does not work as intended. For example, if the array nums contains the values [7, 10, 10, 15, 15, 15, 15, 10, 10, 10, 15, 10, 10], the call findLongest(10) should return 3, the length of the longest consecutive block of 10s. private int[] nums; public int findLongest(int target) { int lenCount = 0; int maxLen = 0; Line 1: for (int val : nums) Line 2: { Line 3: if (val == target) Line 4: { Line 5: lenCount++; Line 6: } Line 7: else Line 8: { Line 9: if (lenCount > maxLen) Line 10: { Line 11: maxLen = lenCount; Line 12: } Line 13: } Line 14: } Line 15: if (lenCount > maxLen) Line 16: { Line 17: maxLen = lenCount; Line 18: } Line 19: return maxLen; } Which of the following changes should be made so that methodfindLongest will work as intended? a. Insert the statement lenCount = 0; between lines 2 and 3. b. Insert the statement lenCount = 0; between lines 8 and 9. c. Insert the statement lenCount = 0; between lines 10 and 11. d. Insert the statement lenCount = 0; between lines 11 and 12. e. Insert the statement lenCount = 0; between lines 12 and 13.

e


संबंधित स्टडी सेट्स

english study guide chapters 4-6

View Set

Anterior Cruciate Ligament Sprain

View Set

science prokaryotes and eukaryotes

View Set

MARKETING FINAL EXAM REVIEW- 2019 True/False

View Set

Physical Science Solar System Quiz

View Set