AP COMPUTER SCIENCE A Test Booklet
Unit 1 Questions
1. In the code segment below, assume that the int variable n has been properly declared and initialized. The code
segment is intended to print a value that is 1 more than twice the value of n.
/* missing code */
System.out.print(result);
Which of the following can be used to replace /* missing code */ so that the code segment works as intended?
I. int result = 2 * n;
result = result + 1;
II. int result = n + 1;
result = result * 2;
III. int result = (n + 1) * 2;
(A) I only
(B) II only
(C) III only
(D) I and III
(E) II and III
2. Consider the following code segment.
int a = 5;
int b = 8;
int c = 3;
System.out.println(a + b / c * 2);
What is printed as a result of executing this code?
(A) 2
(B) 6
(C) 8
(D) 9
(E) 14
3. In the code segment below, assume that the int variables a and b have been properly declared and initialized.
int c = a;
int d = b;
c += 3;
d--;
double num = c;
num /= d;
Which of the following best describes the behavior of the code segment?
AP Computer Science A Page 1 of 15
Test Booklet
Unit 1 Questions
(A) The code segment stores the value of (a + 3) / b in the variable num.
(B) The code segment stores the value of (a + 3) / (b - 1) in the variable num.
(C) The code segment stores the value of (a + 3) / (b - 2) in the variable num.
(D) The code segment stores the value of (a + 3) / (1 - b) in the variable num.
The code segment causes a runtime error in the last line of code because num is type double and d is
(E)
type int.
4. Consider the following code segment, which is intended to find the average of two positive integers, x and y.
int x;
int y;
int sum = x + y;
double average = (double) (sum / 2);
Which of the following best describes the error, if any, in the code segment?
(A) There is no error, and the code works as intended.
In the expression (double) (sum / 2), the cast to double is applied too late, so the average will
(B)
be less than the expected result for even values of sum.
In the expression (double) (sum / 2), the cast to double is applied too late, so the average will
(C)
be greater than the expected result for even values of sum.
In the expression (double) (sum / 2), the cast to double is applied too late, so the average will
(D)
be less than the expected result for odd values of sum.
In the expression (double) (sum / 2), the cast to double is applied too late, so the average will
(E)
be greater than the expected result for odd values of sum.
Page 2 of 15 AP Computer Science A
Test Booklet
Unit 1 Questions
5. Consider the following static method.
public static int calculate(int x)
x = x + x;
x = x + x;
x = x + x;
return x;
Which of the following can be used to replace the body of calculate so that the modified version of calculate will
return the same result as the original version for all x ?
(A) return 3 + x;
(B) return 3 * x;
(C) return 4 * x;
(D) return 6 * x;
(E) return 8 * x;
6. Consider the following static method.
public static int calculate(int x)
x = x + x;
x = x + x;
x = x + x;
return x;
Which of the following can be used to replace the body of calculate so that the modified version of calculate will
return the same result as the original version for all x ?
AP Computer Science A Page 3 of 15
Test Booklet
Unit 1 Questions
(A) return 2 * x;
(B) return 4 * x;
(C) return 8 * x;
(D) return 3 * calculate(x);
(E) return x + calculate(x - 1);
7. Consider the following code segment.
double num = 9 / 4;
System.out.print(num);
System.out.print(" ");
System.out.print((int) num);
What is printed as a result of executing the code segment?
(A) 2 2
(B) 2.0 2
(C) 2.0 2.0
(D) 2.25 2
(E) 2.25 2.0
8. Which of the following expressions evaluate to 3.5 ?
I. (double) 2 / 4 + 3
II. (double) (2 / 4) + 3
III. (double) (2 / 4 + 3)
(A) I only
(B) III only
(C) I and II only
(D) II and III only
(E) I, II, and III
9. Consider the following code segment.
double x = (int) (5.5 - 2.5);
double y = (int) 5.5 - 2.5;
System.out.println(x - y);
What is printed as a result of executing the code segment?
(A) -1.0
(B) -0.5
(C) 0.0
(D) 0.5
(E) 1.0
Page 4 of 15 AP Computer Science A
Test Booklet
Unit 1 Questions
10. Consider the following code segment.
int w = 1;
int x = w / 2;
double y = 3;
int z = (int) (x + y);
Which of the following best describes the results of compiling the code segment?
(A) The code segment compiles without error.
The code segment does not compile, because the int variable x cannot be assigned the result of the
(B)
operation w / 2.
The code segment does not compile, because the integer value 3 cannot be assigned to the double
(C)
variable y.
The code segment does not compile, because the operands of the addition operator cannot be of different
(D)
types int and double.
The code segment does not compile because the result of the addition operation is of type double and
(E)
cannot be cast to an int.
11. Consider the following code segment.
double x = 4.5;
int y = (int) x * 2;
System.out.print(y);
What is printed as a result of executing the code segment?
(A) 8
(B) 8.0
(C) 9
(D) 9.0
(E) 10
12. The code segment below is intended to calculate the circumference c of a circle with the diameter d of 1.5.
The circumference of a circle is equal to its diameter times pi.
/* missing declarations */
c = pi * d;
Which of the following variable declarations are most appropriate to replace /* missing declarations */ in this
code segment?
AP Computer Science A Page 5 of 15
Test Booklet
Unit 1 Questions
int pi = 3.14159;
(A) int d = 1.5;
final int c;
final int pi = 3.14159;
(B) int d = 1.5;
int c;
final double pi = 3.14159;
(C) double d = 1.5;
double c;
double pi = 3.14159;
(D) double d = 1.5;
final double c = 0.0;
final double pi = 3.14159;
(E) final double d = 1.5;
final double c = 0.0;
13. Consider the following code segment.
int a = 5;
int b = 4;
int c = 2;
a *= 3;
b += a;
b /= c;
System.out.print(b);
What is printed when the code segment is executed?
(A) 2
(B) 4
(C) 9
(D) 9.5
(E) 19
14. Consider the following code segment.
int x = 5;
int y = 6;
/* missing code */
z = (x + y) / 2;
Which of the following can be used to replace /* missing code */ so that the code segment will compile?
I. int z = 0;
II. int z;
III. boolean z = false;
Page 6 of 15 AP Computer Science A
Test Booklet
Unit 1 Questions
(A) I only
(B) II only
(C) I and II only
(D) II and III only
(E) I, II, and III
15. A code segment (not shown) is intended to determine the number of players whose average score in a game exceeds
0.5. A player’s average score is stored in avgScore, and the number of players who meet the criterion is stored
in the variable count.
Which of the following pairs of declarations is most appropriate for the code segment described?
double avgScore;
(A) boolean count;
double avgScore;
(B) double count;
double avgScore;
(C) int count;
int avgScore;
(D) boolean count;
int avgScore;
(E) int count;
16. Consider the following code segment.
int a = 5;
int b = 2;
double c = 3.0;
System.out.println(5 + a / b * c - 1);
What is printed when the code segment is executed?
(A) 0.666666666666667
(B) 9.0
(C) 10.0
(D) 11.5
(E) 14.0
17. Consider the following code segment.
System.out.print("Hello System.out.println");
System.out.print("!!!");
What is printed as a result of executing the code segment?
AP Computer Science A Page 7 of 15
Test Booklet
Unit 1 Questions
(A) Hello!!!
(B) Hello System.out.println!!!
Hello
(C) !!!
Hello System.out.println
(D) !!!
(E) Nothing is printed because the text "System.out.println" cannot appear inside a print statement.
18. Consider the following code segment.
double firstDouble = 2.5;
int firstInt = 30;
int secondInt = 5;
double secondDouble = firstInt - secondInt / firstDouble + 2.5;
What value will be assigned to secondDouble when the code segment is executed?
(A) 5.0
(B) 12.5
(C) 25.5
(D) 29.0
(E) 30.5
19. The following code segment is intended to interchange the values of the int variables x and y. Assume that x
and y have been properly declared and initialized.
int temp = x;
/* missing code */
Which of the following can be used to replace /* missing code */ so that the code segment works as intended?
x = y;
(A) x = temp;
x = y;
(B) y = temp;
y = x;
(C) x = temp;
y = x;
(D) temp = y;
y = x;
(E) temp = x;
Page 8 of 15 AP Computer Science A
Test Booklet
Unit 1 Questions
20. Consider the following code segment.
num += num;
num *= num;
Assume that num has been previously declared and initialized to contain an integer value. Which of the following
best describes the behavior of the code segment?
(A) The value of num is two times its original value.
(B) The value of num is the square its original value.
(C) The value of num is two times the square of its original value.
(D) The value of num is the square of twice its original value.
(E) It cannot be determined without knowing the initial value of num.
21. Consider the following code segment, which is intended to print the digits of the two-digit int number num in
reverse order. For example, if num has the value 75, the code segment should print 57. Assume that num
has been properly declared and initialized.
/* missing code */
System.out.print(onesDigit);
System.out.print(tensDigit);
Which of the following can be used to replace /* missing code */ so that the code segment works as intended?
int onesDigit = num % 10;
(A) int tensDigit = num / 10;
int onesDigit = num / 10;
(B) int tensDigit = num % 10;
int onesDigit = 10 / num;
(C) int tensDigit = 10 % num;
int onesDigit = num % 100;
(D) int tensDigit = num / 100;
int onesDigit = num / 100;
(E) int tensDigit = num % 100;
22. Consider the following code segment.
int a = 3 + 2 * 3;
int b = 4 + 3 / 2;
int c = 7 % 4 + 3;
double d = a + b + c;
What is the value of d after the code segment is executed?
AP Computer Science A Page 9 of 15
Test Booklet
Unit 1 Questions
(A) 14.0
(B) 18.0
(C) 20.0
(D) 20.5
(E) 26.0
23. Which of the following expressions evaluate to 7 ?
I. 9 + 10 % 12
II. (9 + 10) % 12
III. 9 – 2 % 12
(A) I only
(B) II only
(C) I and III
(D) II and III
(E) I, II, and III
24. Consider the following code segment.
int x = 5;
x += 6 * 2;
x -= 3 / 2;
What value is stored in x after the code segment executes?
(A) -1.5
(B) 1
(C) 9
(D) 15.5
(E) 16
25. Consider the following code segment, where k and count are properly declared and initialized int variables.
k++;
k++;
count++;
k--;
count++;
k--;
Which of the following best describes the behavior of the code segment?
Page 10 of 15 AP Computer Science A
Test Booklet
Unit 1 Questions
(A) The code segment leaves both k and count unchanged.
(B) The code segment increases both k and count by 2.
(C) The code segment increases k by 4 and count by 2.
(D) The code segment leaves k unchanged and increases count by 2.
(E) The code segment increases k by 2 and leaves count unchanged.
26. Consider the following code segment.
int a = 4;
int b = 5;
a++;
b++;
int c = a + b;
a -= 1;
System.out.println(a + c);
What is printed when the code segment is executed?
(A) 9
(B) 10
(C) 14
(D) 15
(E) 25
27. Consider the following code segment.
System.out.print("AP");
System.out.println();
System.out.println("CS");
System.out.print("A");
What is printed as a result of executing the code segment?
(A) APCSA
APCS
(B) A
AP
(C) CSA
AP
(D) CS
A
AP
(E) CS
A
AP Computer Science A Page 11 of 15
Test Booklet
Unit 1 Questions
28. Consider the following code segment.
System.out.print(I do not fear computers. ); // Line 1
System.out.println(I fear the lack of them.); // Line 2
System.out.println(--Isaac Asimov); // Line 3
The code segment is intended to produce the following output but may not work as intended.
I do not fear computers. I fear the lack of them.
--Isaac Asimov
Which change, if any, can be made so that the code segment produces the intended output?
(A) In line 1, print should be changed to println.
(B) In lines 2 and 3, println should be changed to print.
(C) The statement System.out.println() should be inserted between lines 2 and 3.
(D) In lines 1, 2, and 3, the text that appears in parentheses should be enclosed in quotation marks.
(E) No change is needed; the code segment works correctly as is.
29. Consider the following code segment.
System.out.print(*); // Line 1
System.out.print("*"); // Line 2
System.out.println(); // Line 3
System.out.println("*"); // Line 4
The code segment is intended to produce the following output, but may not work as intended.
**
*
Which line of code, if any, causes an error?
(A) Line 1
(B) Line 2
(C) Line 3
(D) Line 4
(E) The code segment works as intended.
30. Consider the following code segment.
System.out.print("*");
System.out.println("**");
System.out.println("***");
System.out.print("****");
What is printed as a result of executing the code segment?
Page 12 of 15 AP Computer Science A
Test Booklet
Unit 1 Questions
*
**
(A) ***
****
*
(B) **
*******
*
(C) *****
****
***
(D) ***
****
***
(E) *******
31. Consider the following code segment.
System.out.print("One"); // Line 1
System.out.print("Two"); // Line 2
System.out.print("Three"); // Line 3
System.out.print("Four"); // Line 4
The code segment is intended to produce the following output, but does not work as intended.
OneTwo
ThreeFour
Which of the following changes can be made so that the code segment produces the intended output?
(A) Changing print to println in line 1 only
(B) Changing print to println in line 2 only
(C) Changing print to println in line 3 only
(D) Changing print to println in lines 2 and 3 only
(E) Changing print to println in lines 1, 2, 3, and 4
32. What is printed as a result of executing the following statement?
(A) 4
(B) 5
(C) 41
(D) 401
(E) 405
AP Computer Science A Page 13 of 15
Test Booklet
Unit 1 Questions
33. The following code segment is intended to round val to the nearest integer and print the result.
double val = -0.7;
int roundedVal = (int) (val + 0.5);
System.out.println(roundedVal);
Which of the following best describes the behavior of the code segment?
(A) The code segment works as intended.
The code segment does not work as intended because val and roundedVal should be declared as the
(B)
same data type.
The code segment does not work as intended because the expression (val + 0.5) should be cast to a
(C)
double instead of an int.
The code segment does not work as intended because val should be cast to an int before 0.5 is
(D)
added to it.
The code segment does not work as intended because the expression (int) (val + 0.5) rounds to
(E)
the nearest integer only when val is positive.
34. Which of the following statements stores the value 3 in x ?
(A) int x = 4 / 7;
(B) int x = 7 / 3;
(C) int x = 7 / 4;
(D) int x = 5 % 8;
(E) int x = 8 % 5;
Page 14 of 15 AP Computer Science A
Test Booklet
Unit 1 Questions
Directions: Select the choice that best fits each statement. The following question(s) refer to the following incomplete
class declaration.
35. 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;
hours = hours + minutes / 60;
(C)
minutes = minutes % 60;
hours = hours + minutes % 60;
(D)
minutes = minutes / 60;
(E) hours = hours + minutes / 60;
AP Computer Science A Page 15 of 15