These are drawn from the current AP example questions, but I have translated them from C++ to Java. The AP topic distribution changes slightly with the switch to Java, but this selection of questions is still representative of the type of questions you can expect.
Determine the answer to each of the following questions or incomplete statements, using the available space for any necessary scratchework. Then decide which is the best of the choices given and fill in the corresponding oval on the answer sheet. No credit will be given for anything written in the examination booklet. Do not spend too much time on any one problem.
As a correction for haphazard guessing, one-fourth of the number of questions answered incorrectly will be subtracted from the number of questions answered correctly.
Which of the following segments correctly computes the sum of the first 10 multiples of 5 (i.e., 5, 10,..., 50)?int num = 10; int sum;
(A) sum = 5; (D) sum = 0;
while(num > 1) { while(num >= 0) {
sum += sum; num--;
num--; sum += 5 * num;
} }
(B) sum = 5; (E) sum = 0;
while(num > 0) { while(num > 0) {
sum += sum; sum += 5 * num;
num--; num--;
} }
(C) sum = 0;
while(num > 0) {
num--;
sum += 5 * num;
}
int F1(int len) {
int tot = 0;
int k = 0;
while(k < len) {
tot += k;
k++;
}
return tot;
}
|
int F2(int len) {
int tot = 0;
int k;
for(k = 0; k < len; k++) {
tot += k;
}
return tot;
}
|
I. Any value less than zero
II. The value zero
III. Any value greater than zero
(A) I only
(B) II only
(C) III only
(D) I and II only
(E) I, II, and III
void mystery(int n) {
if(n >= 2) {
mystery(n / 3);
}
IO.print(n + " ");
}
Which of the following is output as a result of the call
mystery(27)?
(A) 0 27
(B) 1 3 9 27
(C) 3 9 27
(D) 27 0
(E) 27 9 3
int fun(String str) {
int n;
int k;
n = str.length();
for(k = 0; k < str.length(); k++) {
if(str.charAt(k) == 'x') {
n--;
}
}
return n;
}
Assume that s is a String. Which of the following
best characterizes the value returned by the call fun(s)?
String laugh(String word, int num) {
int k;
String result = "";
for(k = 0; k < num; k++) {
result += word;
result += " ";
}
return result;
}
void test() {
String str = "ha";
IO.println(laugh(laugh(str, 2), 3));
}
Which of the following is output as a result of the call
test()?
(A) ha
(B) ha ha
(C) ha ha ha
(D) ha ha ha ha ha ha
(E) Nothing will be output because an exception will occur.
class TheaterTicket {
public TheaterTicket() {
// body not shown
}
public void setInfo(String date, String location, int seat) {
// body not shown
}
// other methods and variables not shown
}
Consider modifying the TheaterTicket class so that it is
possible to initialize variables of type TheaterTicket with
ticket information when they are declared, as well as set their values
later using instance method setInfo. For example, the following
code should define and initialize two TheaterTicket variables.
TheaterTicket t1;
t1.setInfo("May 5, 1999", "AA", 22);
TheaterTicket t2 = new TheaterTicket("April 29, 1999", "B", 10);
Which of the following best describes the additional method that should
be provided?
(A) An overloaded version of setInfo with no arguments
(B) An overloaded version of setInfo with three arguments
(C) A constructor with no arguments
(D) A constructor with three arguments
(E) A constructor with five arguments
class SalesPerson {
public SalesPerson() { // constructor
// (body not shown)
}
public void setSales(int month, double sales) {
// sets value for one month's sales figures
// (body not shown)
}
public void printAnnualSales() {
// prints values for the annual sales
// (body not shown)
}
private double computeAnnualSales() {
// totals the sales for the last 12 months
// (body not shown)
}
private ArrayList mySales;
}
The method computeAnnualSales is NOT a public instance method
because
Of the following statements, which sets the fifth month's sales for salesperson clerk to 3705.87?SalesPerson clerk = new SalesPerson();
(A) SalesPerson clerk = new SalesPerson(5, 3704.87);
(B) clerk = setSales(5, 3705.87);
(C) setSales(5, 3705.87);
(D) clerk.setSales(3704.87, 5);
(E) clerk.setSales(5, 3705.87);
Case 1 - The elements are unordered.
Case 2 - The elements are sorted in ascending order.
Case 3 - The elements are sorted in descending order.
Case 1 Case 2 Case 3 (A) 1,000 1,000 1,000 (B) 1,000 11 11 (C) 1,000 11 1,000 (D) 1,000 1,000 500 (E) 500 500 500
(A) (x > y) && (y <= 3)
(B) (x > y) || (y <= 3)
(C) (x > y) || (y <= 3)
(D) (x <= y) || (y > 3)
(E) (x <= y) && (y > 3)
class BoxInfo {
public int boxNum;
public int numLetters; // number of letters currently in this box
}
Information about post office boxes that currently contain at least one
letter will be stored in an array. Each element is of type
BoxInfo. Two possible implementations are being considered.
Method A: Store the array entries in arbitrary order.Consider the following operations.
Method B: Store the array entries in sorted order by box number.
int minLoc(int[] A) {
int k;
int minIndex = 0;
for(k = 1; k < A.length; k++) {
if( <condition> ) {
<statement>
}
}
return minIndex;
}
Which of the following could be used to replace <condition> and
<statement> so that method minLoc works as intended?
<condition> <statement> (A) A[k] < minIndex minIndex = A[k]; (B) A[k] < A[minIndex] minIndex = A[minIndex]; (C) A[k] < A[minIndex] minIndex = k; (D) A[k] < minIndex minIndex = A[k]; (E) A[k] < A[minIndex] minIndex = k;
class Person {
public Person() { // constructor
// (body not shown)
}
public int numChildren() {
// returns this person's number of children
// (body not shown)
}
public Person kthChild(int k) {
// If this person has at least k children, then returns this
// person's kth child. If k is 1, returns the person's first
// child.
// (body not shown)
}
// (other members not shown)
}
Also assume that the following definitions have been made.
Person P; int k; boolean oddNum;
Which of the following code segments correctly sets oddNum to true if person P has an odd number of children, and to false otherwise?
I. oddNum = ((P.numChildren() % 2) == 1);
II. if((P.numChildren() % 2) == 0)
oddNum = false;
else
oddNum = true;
III. oddNum = false;
for(k = 1; k <= P.numChildren(); k++)
oddNum = !oddNum;
(A) I only
(B) II only
(C) III only
(D) I and II only
(E) I, II, and III
(A) total = P.numChildren();
(B) total = P.numChildren(P.numChildren());
(C) total = 0;
for(k = 1; k <= P.numChildren(); k++) {
total += P.kthChild(k);
}
(D) total = 0;
for(k = 1; k <= P.numChildren(); k++) {
total += P.kthChild(k).numChildren();
}
(E) total = 0;
for(k = 1; k <= P.numChildren(); k++) {
total += P.numChildren().kthChild(k);
}
int[][] M = new int[10][10];
int k;
for(k = 0; k < 10; k++) {
M[k][k] = 0;
}
Which of the following best describes what happens when the loop is
executed?
(A) All entries of matrix M are set to zero.
(B) One entry of matrix M is set to zero.
(C) One column of matrix M is set to zero.
(D) One row of matrix M is set to zero.
(E) One diagonal of matrix M is set to zero.
static int numLessThanValue(int[] A, int value) {
int total = 0;
int index;
for(index = 0; index < A.length; index++) {
if(A[index] < value) {
total ++;
}
}
return total;
}
What is the greatest possible value that can be returned by method
numLessThanValue?
(A) 0
(B) 1
(C) A.length - 1
(D) A.length
(E) A.length * 2
boolean unique(int[] A) {
// Assumes A contains at least one value, stored in unsorted order.
// Returns true if there are no duplicate values in A; otherwise,
// returns false.
int j, k;
<body of unique>
}
Which of the following code segments can be used to replace <body
of unique> so that method unique accomplishes its
goal?
I. for(j = 0; j < n - 1; j++) {
for(k = j + 1; k < n; k++) {
if(A[j] == A[k])
return false;
}
}
return true;
II. for(j = 1; j < n; j++) {
for(k = 0; k < j; k++) {
if(A[k] == A[j])
return false;
}
}
return true;
III. for(j = 0; j < n; j++) {
for(k = 1; k < n; k++) {
if(A[k] == A[j])
return false;
}
}
return true;
(A) I only
(B) II only
(C) I and II only
(D) I and III only
(E) I, II, and III
1. E 4. B 7. B 10. D 13. E 16. D 2. E 5. D 8. E 11. D 14. D 17. C 3. B 6. D 9. A 12. C 15. E