Ap Sample Questions From College Board
Ap Sample Questions From College Board
18 © 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org.
Sample Questions for Computer Science A
© 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org. 19
Sample Questions for Computer Science A
20 © 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org.
Sample Questions for Computer Science A
4. 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 ?
(a) II only
(b) III only
(c) I and II only
(d) I and III only
(e) I, II, and III
© 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org. 21
Sample Questions for Computer Science A
6. 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) I only
(b) II only
(c) III only
(d) I and II only
(e) I, II, and III
© 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org. 23
Sample Questions for Computer Science A
24 © 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org.
Sample Questions for Computer Science A
8. 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;
9. Consider the following declaration that appears in a class other than TimeRecord.
© 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org. 25
Sample Questions for Computer Science A
26 © 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org.
Sample Questions for Computer Science 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.
© 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org. 27
Sample Questions for Computer Science A
11. 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.
12. Which of the following changes should be made so that method findLongest
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.
28 © 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org.
Sample Questions for Computer Science A
Which of the following best describes the contents of numbers after the
following statement has been executed?
int m = mystery(n);
© 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org. 29
Sample Questions for Computer Science A
30 © 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org.
Sample Questions for Computer Science A
Assume that the following declaration appears in a class other than Dog.
© 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org. 31
Sample Questions for Computer Science A
32 © 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org.
Sample Questions for Computer Science A
© 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org. 33
Sample Questions for Computer Science A
/* 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
34 © 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org.
Sample Questions for Computer Science A
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}}
© 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org. 35
Sample Questions for Computer Science A
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?
(a) I only
(b) II only
(c) III only
(d) I and III only
(e) II and III only
36 © 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org.
Sample Questions for Computer Science A
21. 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;
}
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];
}
}
22. 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;
}
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
38 © 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org.
Sample Questions for Computer Science A
© 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org. 39
Sample Questions for Computer Science A
23. Consider a shuffle method that is intended to return a new array that contains
all the elements from nums, but in a different order. Let n be the number of
elements in nums. The shuffle method should alternate the elements from
nums[0] … nums[n / 2 – 1] with the elements from nums[n / 2] …
nums[n – 1], as illustrated in the following examples.
Example 1
0 1 2 3 4 5 6 7
nums 10 20 30 40 50 60 70 80
0 1 2 3 4 5 6 7
result 10 50 20 60 30 70 40 80
Example 2
0 1 2 3 4 5 6
nums 10 20 30 40 50 60 70
0 1 2 3 4 5 6
result 10 40 20 50 30 60 70
The following implementation of the shuffle method does not work as intended.
public static int[] shuffle(int[] nums)
{
int n = nums.length;
int[] result = new int[n];
return result;
}
Which of the following best describes the problem with the given implementation of
the shuffle method?
(a) Executing shuffle may cause an ArrayIndexOutOfBoundsException.
(b) The first element of the returned array (result[0]) may not have the
correct value.
(c) The last element of the returned array (result[result.length − 1])
may not have the correct value.
(d) One or more of nums[0] … nums[nums.length / 2 − 1] may have
been copied to the wrong position(s) in the returned array.
(e) One or more of nums[nums.length / 2] … nums[nums.length − 1]
may have been copied to the wrong position(s) in the returned array.
40 © 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org.
Sample Questions for Computer Science A
24. 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 */ }
/* missing code */
return sum;
}
}
Assume that sum1D works correctly. Which of the following can replace
/* missing code */ so that the sum2D method works correctly?
(a) I only
(b) II only
(c) I and II only
(d) II and III only
(e) I, II, and III
© 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org. 41
Sample Questions for Computer Science A
25. 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: }
42 © 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org.
Sample Questions for Computer Science A
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])
© 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org. 43
Sample Questions for Computer Science A
// There may be instance variables, constructors, and methods that are not
// shown.
}
For example, assume that t1 and t2 are Time objects where t1 represents
1:00 p.m. and t2 represents 2:15 p.m. The call t1.minutesUntil(t2) will
return 75 and the call t2.minutesUntil(t1) will return -75.
44 © 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org.
Sample Questions for Computer Science A
The declaration for the Flight class is shown below. It has methods to access the
departure time and the arrival time of a flight. You may assume that the departure
time of a flight is earlier than its arrival time.
// There may be instance variables, constructors, and methods that are not shown.
}
A trip consists of a sequence of flights and is represented by the Trip class. The Trip
class contains a List of Flight objects that are stored in chronological order. You
may assume that for each flight after the first flight in the list, the departure time of the
flight is later than the arrival time of the preceding flight in the list. A partial declaration
of the Trip class is shown below. You will write two methods for the Trip class.
/** @return the number of minutes from the departure of the first flight to the
* arrival of the last flight if there are one or more flights in the trip;
* 0, if there are no flights in the trip
*/
public int getDuration()
{ /* to be implemented in part (a) */ }
/** Precondition: the departure time for each flight is later than the arrival
* time of its preceding flight
* @return the smallest number of minutes between the arrival of
* a flight and the departure of the flight immediately after it,
* if there are two or more flights in the trip;
* -1, if there are fewer than two flights in the trip
*/
public int getShortestLayover()
{ /* to be implemented in part (b) */ }
// There may be instance variables, constructors, and methods that are not
// shown.
}
© 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org. 45
Sample Questions for Computer Science A
/** Precondition: the departure time for each flight is later than the arrival
* time of its preceding flight
* @return the smallest number of minutes between the arrival of a
* flight and the departure of the flight immediately after it, if
* there are two or more flights in the trip;
* -1, if there are fewer than two flights in the trip
*/
public int getShortestLayover()
46 © 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org.
Sample Questions for Computer Science A
2. Consider the following incomplete StringUtil class declaration. You will write
implementations for the two methods listed in this class. Information about the
Person class used in the replaceNameNickname method will be presented in
part (b).
© 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org. 47
Sample Questions for Computer Science A
48 © 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org.
Sample Questions for Computer Science A
(b) The following Person class contains information that includes a first (given) name
and a nickname for the person.
For example, assume the following table represents the data contained in the list
people.
getFirstName() getNickname()
0 "Henry" "Hank"
1 "Elizabeth" "Liz"
2 "John" "Jack"
3 "Margaret" "Peggy"
"After Hank drove Liz to dinner in Jackson City, Hank paid for
an appetizer and Liz paid for dessert."
© 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org. 49
Sample Questions for Computer Science A
50 © 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org.
Sample Questions for Computer Science A
Note that a Cat “is-a” Pet, a Dog “is-a” Pet, and a LoudDog “is-a” Dog.
The subclass Dog has the partial class declaration shown below.
(a) Given the class hierarchy shown above, write a complete class declaration for
the class Cat, including implementations of its constructor and method(s).
The Cat method speak returns "meow" when it is invoked.
© 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org. 51
Sample Questions for Computer Science A
(b) Assume that class Dog has been declared as shown at the beginning of
the question. If the String dog-sound is returned by the Dog method
speak, then the LoudDog method speak returns a String containing
dog-sound repeated two times.
Given the class hierarchy shown previously, write a complete class
declaration for the class LoudDog, including implementations of its
constructor and method(s).
/** For every Pet in the kennel, prints the name followed by
* the result of a call to its speak method, one line per Pet.
*/
public void allSpeak()
{ /* to be implemented in part (c) */ }
Write the Kennel method allSpeak. For each Pet in the kennel,
allSpeak prints a line with the name of the Pet followed by the result of
a call to its speak method.
In writing allSpeak, you may use any of the methods defined for any of
the classes specified for this problem. Assume that these methods work as
specified, regardless of what you wrote in parts (a) and (b).
/** For each Pet in the kennel, prints the name followed by
* the result of a call to its speak method, one line per Pet.
*/
public void allSpeak()
52 © 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org.
Sample Questions for Computer Science A
For example, if arr is the array {11, 12, 13, 14, 15} and num is
27, the call to shiftArray changes arr as shown below.
Before call 0 1 2 3 4
arr: 11 12 13 14 15
After call 0 1 2 3 4
arr: 27 11 12 13 14
© 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org. 53
Sample Questions for Computer Science A
54 © 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org.
Sample Questions for Computer Science A
/** Shifts each matrix element to the next position in row-major order
* and inserts the new number at the front. The last element in the last
* row is discarded.
* @param num the new number to insert at the front of matrix
* Postcondition: The original elements of matrix have been shifted to
* the next higher position in row-major order, and
* matrix[0][0] == num.
* The original last element in the last row is discarded.
*/
public void shiftMatrix(int num)
{ /* to be implemented in part (b) */ }
/** Rotates each matrix element to the next higher position in row-major
* order.
* Postcondition: The original elements of matrix have been shifted
* to the next higher position in row-major order, and
* matrix[0][0] == the original last element.
*/
public void rotateMatrix()
{ /* to be implemented in part (c) */ }
// There may be instance variables, constructors, and methods that are not
// shown.
}
© 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org. 55
Sample Questions for Computer Science A
/** Shifts each matrix element to the next position in row-major order
* and inserts the new number at the front. The last element in the last
* row is discarded.
* @param num the new number to insert at the front of matrix
* Postcondition: The original elements of matrix have been shifted
* to the next higher position in row-major order, and
* matrix[0][0] == num.
* The original last element in the last row is discarded.
*/
public void shiftMatrix(int num)
/** Rotates each matrix element to the next higher position in row-major
* order.
* Postcondition: The original elements of matrix have been shifted to
* the next higher position in row-major order, and
* matrix[0][0] == the original last element.
*/
public void rotateMatrix()
56 © 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org.
Sample Questions for Computer Science A
(b)
return shortest;
}
© 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org. 57
Sample Questions for Computer Science A
Question 2
(a)
Iterative version:
Recursive version:
58 © 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org.
Sample Questions for Computer Science A
(b)
Question 3
(a)
(b)
© 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org. 59
Sample Questions for Computer Science A
(c)
Question 4
(a)
arr[0] = num;
}
(b)
(c)
60 © 2014 The College Board. Visit the College Board on the Web: www.collegeboard.org.