AP Computer Science A Practice Test 2018
AP Computer Science A Practice Test 2018
Barbara Ericson
Georgia Tech
if (x >= 1) x = x * 3;
if (x > 3) x = 0;
(A) x = 0;
(B) if (x > 1) x = 0;
(C) if (x > 3) x = 0;
(D) if (x >=1) x = 0;
(E) none of the above
(A) Pizza
(B) Taco
(C) Studying
(D) Eating
(E) Studying
Eating
3. Given the following code which of the answers best describes the conditions needed
for temp to be true when it is returned?
(A) [a, c, e, d, g]
(B) [c, e, d, b, g]
(C) [a, c, e, g]
(D) [a, b, e, d, g]
(E) [a, c, e, d, b, g]
public ElectricCar() {
super("Ford");
}
public ElectricCar(String theMake) {
super(theMake);
}
}
(A) v.test(sporty,v);
(B) sporty.test(c,c);
(C) v.test(sporty,c);
(D) sporty.test(sporty,v);
(E) c.test(sporty,sporty);
7. When is the following Boolean expression true (a and b are integers)?
8. The following incomplete method is intended to sort the array a in ascending order.
temp = a[i];
a[i] = a[lIndex];
a[lIndex] = temp;
}
1
22
333
4444
I.
for (int i = 1; i < 5; i++) {
for (int j = i; j > 0; j--) {
System.out.print(i+1);
}
System.out.println();
}
II.
for (int i = 0; i < 5; i++) {
for (int j = 0; j < i; j++) {
System.out.print(i);
}
System.out.println();
}
III.
for (int i = 1; i <= 5; i++) {
for (int j = i; j > 0; j--) {
System.out.print(i);
}
System.out.println();
}
IV.
for (int i = 1; i < 6; i++) {
for (int j = 0; j < i; j++) {
System.out.println(i);
}
}
V.
for (int i = 0; i < 5; i++) {
for (int j = 0; j < i; j++) {
System.out.print(i+1);
}
System.out.println();
}
(A) I
(B) II
(C) III
(D) IV
(E) V
(A) 0
(B) 1
(C) 2
(D) n - 1
(E) n - 2
12. Given the following method and what would the result be when m is executed?
public void m(int[][]p) {
int height = p.length;
for (int row = 0; row < height / 2; row++) {
for (int col = 0; col <p[0].length; col++) {
p[row][col] = p[height - row - 1][col];
}
}
}
(A) Copies the values from the top half to the bottom half of the 2D array
(B) Copies the values from the left halt to the right half of the 2D array
(C) Copies the values from the bottom half to the top half of the 2D array
(D) Copies the values from the right half to the left half of the 2D array
(E) All values remain the same.
while (p <= 8)
{
sum += p % q;
p++;
q++;
}
What is the value of sum after the code is executed?
(A) 1
(B) 0
(C) 13
(D) 7
(E) 4
14. What is the output from mystery(4321) when mystery is defined as follows:
//precondition: x >=0
public static void mystery (int x) {
System.out.print(x % 10);
if ((x / 10) != 0) {
mystery(x / 10);
}
}
(A) 12344321
(B) 1234
(C) 4321
(D) 43211234
(E) 32144123
15. Which of the following reasons for using an inheritance hierarchy are valid?
16. Which of the following correctly shows the iterations of an ascending (from left to
right) insertion sort on an array with the following elements: {7,3,8,5,2}?
(A) 130
(B) 133
(C) 131
(D) 132
(E) 136
Pick the answer below that best describes all the cases when this method will return
true.
(A) 1 3 5 7 9 11 13 15 17 19
(B) 0 2 4 6 8 10 12 14 16 18
(C) 2 4 6 8 10 12 14 16 18 20
(D) 3 6 9 12 15 18
(E) 0 2 4 6 8 10 13 14 16 18 20
20. Consider the following partial class definitions.
public class C1 {
private int num;
private String name;
(A) II only
(B) III only
(C) I and II only
(D) I, II, and III
(E) I only
22. Consider the following code segment
What are the values of s and b after the following has executed?
int[] s = {3,4};
int b = 4;
test(s,b);
(A) 012345
(B) 0112233445
(C) 001122334455
(D) 012123234345
(E) You will get an IndexOutOfBoundsException
If matrix is initialized to be: {{-1, -2, 3},{4, -5, 6}}. What will the values in matrix be
after changeMatrix(matrix) is called?
(A) {{4, -5, 6},{-1, -2, 3}}
(B) {{4, 5, 6},{1, 2, 3}}
(C) {{1, 2, 3},{4, 5, 6}}
(D) {{-1, -2, 3},{4, -5, 6}}
(E) {{1, -2, 3},{4, 5, 6}}
(A) 3
(B) 6
(C) 9
(D) 12
(E) 15
27. What is printed when the following main method is executed?
(A) -1
(B) 0
(C) 1
(D) 2
(E) 3
28. What are the values of a and b after the for loop finishes?
int a = 10;
int b = 3;
int t = 0;
for (int i = 1; i < 4; i++)
{
t = a;
a = i + b;
b = t - i;
}
(A) a = 5 and b = -2
(B) a = 6 and b = 7
(C) a = 6 and b = 3
(D) a = 12 and b = 1
(E) a = 5 and b = 8
29. Consider the following method. What value is returned from a call of mystery(5)?
A) 243
B) 0
C) 3
D) 81
E) 27
30. Given the following class declarations. Assume that Parent p = new
Child(); appears in a client program. What is the result of the call p.m1()?
(A) pm1pm2cm1cm2
(B) pm1pm2
(C) pm1pm2cm2cm1
(D) pm1cm1
(E) pm1
31. Which of the following correctly shows the iterations of an ascending (from left to
right) selection sort on an array with the following elements: {6,3,8,5,1}?
I. s1.equals(s3)
II. s1 == s4
III.s1.equals(s4)
(A) mput
(B) mpu
(C) mp
(D) omp
(E) om
(A) 1
(B) 2
(C) 3
(D) 4
(E) 5
36. If you have a parent class Animal that has a method speak() which returns "Awk"
and you have children classes that do the following:
What is the output from looping through this array of animals and asking each to speak()?
Animal[] a = { new Cat(), new Cow(), new Dog(), new Pig(), new Bird() }
I. Insertion sort takes longer when the array is sorted in ascending order and you
want it sorted in descending order.
III. Selection sort takes less time to execute if the array is already sorted in the
correct order.
(A) I only
(B) II only
(C) III only
(D) I and II only
(E) I , II, and III
38. Consider the following method.
Which of the following code segments would return the same values as
outOfRange?
I. if (value < 0)
{
if (value > 100)
return true;
else
return false;
}
else
return false;
(A) I only
(B) II only
(C) III only
(D) I and III
(E) II and III
39. Given the following values for a 2D array m and the following code
1 1 1 1
1 2 3 4
2 2 2 2
2 4 6 8
int sum = 0;
for (int k = 0; k < m.length; k++) {
sum = sum + m[m.length-1-k][1];
}
(A) 6
(B) 9
(C) 10
(D) 4
(E) 20
40. Consider the following method.
Which of the following methods will print the same result as the method above no
matter what values are passed for num1 and num2?
I.
public static void method1(int num1, int num2) {
int result=99;
II.
public static void method2(int num1, int num2) {
int result = 99;
III.
public static void method3(int num1, int num2) {
int result = 99 ;