Pg.
1 of 22
AP CSA Practice Exam 2019
1) Suppose x, y and z are variables of type int. Consider the following three conditions
I. (x == y) && (y == z) && (x == z)
II. (x==y) || (y==z) && (x == z)
III. (x - y) * (x - z) * (y - z) == 0
Which of these conditions is (are) always true if x == y is true?
(A) I only
(B) II only
(C) Ill only
(D) I and II only
(E) II and Ill only
2) Based on the diagram below, which of the following declarations will cause an error?
I. Libraryltem item = new Libraryltem() ;
II. Book b = new Libraryltem() ;
III. Libraryltem cd = new CD() ;
(A) I only
(B) II only
(C) Ill only
(D) I and II only
(E) II and Ill only
Pg. 2 of 22
3) Consider the following method.
What will be returned by a call to multiply(8) ?
(A) 1
(B) 8
(C) 8 * 7 * 6 * 5 * 4 * 3 * 2
(D) 8 * 6 * 4 * 2
(E) 7 * 5 * 3
4) An array of hospital records is sorted by patient ID numbers. Suppose a patient's record must be located,
given the patient's name only. Which of the following procedures is the most efficient for locating that patient's
record?
(A) Do a binary search.
(B) Do a sequential search.
(C) Sort the array into alphabetical order, then do a binary search.
(D) Sort the array into alphabetical order, then do a sequential search.
(E) Start with the first record and examine every fifth record. If this fails, start with the second record and
examine every fourth record. If this fails, start with the third record and examine every third record. Proceed in
this way until the record with the given name is found.
5) Which of the following code segments correctly stores in x a random real number such that 0.6< x < 1?
Pg. 3 of 22
6) Suppose methods f1 and f2 are defined as follows:
What value is returned as a result of the call f1(5)?
(A) -4
(B) 4
(C) 6
(D) 8
(E) -6
7) Consider the following static method, countDiff. Method countDiff is intended to count the number of
different values in array arr. It does not, however, work as intended.
Pg. 4 of 22
Which of the following changes should be made so that countDiff will work as intended?
8) Which is equal to 2Dhex?
(A) 32dec
(B) 44dec
(C) 45dec
(D) 46dec
(E) 720dec
9) Which statement about constructors is false?
(A) It is legal for the value of a static variable to be changed in a constructor.
(B) The constructor in a subclass must use the keyword super to initialize the
private instance variables from its superclass.
(C) An interface never has constructors.
(D) An abstract class never has constructors.
(E) If a subclass does not explicitly provide a constructor and its superclass has
just one constructor with a parameter, an error will occur when an attempt is
made to create an instance of a subclass object.
Pg. 5 of 22
10) What will the output of mystery(6) return?
(A) 10
(B) 12
(C) 16
(D) 26
(E) 32
11) Suppose a and b are boolean variables. The expression will evaluate to false whenever
I. a and b are both false.
II. a is false and b is true.
Ill. a is true and b is false.
A) I only
B) II only
C) Ill only
D) II and Ill only
E) I, II, and Ill
12) Consider the following code segment:
int x = 10, y =0;
while (x > 5)
{
y = 3;
while (y < x)
Pg. 6 of 22
{
y *= 2;
if (y % x == 1)
y += x;
}
x = x -3;
}
System.out.println(x + " " + y);
What will be the output after execution of this code segment?
(A) 1 6
(B) 7 12
(C) -3 12
(D) 4 12
(E) -3 6
13) What value is stored in result if:
int result = 13 - 3 * 6 / 4 % 3;
(A) -5
(B) 0
(C) 13
(D) -1
(E) 12
Pg. 7 of 22
14) Consider the following code segment. Assume k is some positive integer
greater than 2. How many times will “SMALL” be printed?
for (int i = 2; i <= k; i++)
{
if (arr[i] < someValue) {
System.out.print("SMALL");
}
}
(A) 0
(B) 1
(C) k - 1
(D) k - 2
(E) k
Questions 15 and 16 refer to the following:
Pg. 8 of 22
15) Assuming that both Bird a nd Sparrow have default constructors, which is (are) valid?
I. Flyer f1 = new Bird();
II. Bird b = new Sparrow();
III. Flyer f2 = new Sparrow();
(A) I only
(B) II only
(C) III only
(D) I and II only
(E) I, II, and III
16) Consider the following declarations in a client class
Which method calls would be legal?
I. s.fly();
II. b.flyLow(s);
III. s.flyLow(b);
(A) I only
(B) II only
(C) III only
(D) II and III only
(E) I, II, and III
Pg. 9 of 22
ethod below does?
17) Which best describes what the printSomething m
(A) It prints string s
(B) It prints string s in reverse order
(C) It prints only the first character of string s
(D) It prints only the first two characters of string s
(E) It prints only the last character of string s
18) A square matrix is declared as
int[][] mat = new int [SIZE][SIZE]
where SIZE is an appropriate integer constant. Consider the following:
If mat is initialized to be
263
451
712
Pg. 10 of 22
What value will be returned by a call to sum(mat)?
(A) 9
(B) 15
(C) 19
(D) 21
(E) 31
19) The elements of an array arr are to be sorted in increasing order. Which represents the first step of a merge
sort algorithm?
(A) Find the smallest element in arr and swap it into arr[0] .
(B) Compare arr[0] with arr[1] and, if necessary, shift and replace elements such that arr[0] is less than
arr[1] .
(C) Compare arr[0] with the middle element of arr, arr[mid] , and if arr[0] is less than arr[mid] , swap these
elements.
(D) Compare the first and last elements, and if the first is less than the last, swap these elements.
(E) Break the array into roughly two equal halves.
20) Consider the following method:
public static int compute(int n)
{
for(int i = 1; i < 4; i++)
{
n *= n;
}
return n;
}
Which of the following could replace the body of compute so it does the same thing
(A) return 4 * n;
Pg. 11 of 22
(B) return 8 * n;
(C) return 64 * n;
(D) return (int) Math.pow(n,4);
(E) return (int) Math.pow(n,8);
21) Consider the following program that changes a 2D array mat of type int
Suppose matrix mat is originally
1357
2468
9876
0123
Pg. 12 of 22
22) What is the output of the following code segment?
String str1 = "Happy ";
String str2 = str1;
str2 += "New Year! ";
str1 = str2.substring(6);
System.out.println(str1 + str2);
(A) Happy New Year!
(B) Happy Happy New Year!
(C) New Year! New Year!
(D) New Year! Happy New Year!
(E) Happy New Year! Happy New Year!
23) for(int k = 4; k < 10; k += 2){
for(int j = 1; j <= 5; j++){
System.out.print("*");
}
}
How many times will the asterisk ("*") be printed?
(A) 36
(B) 30
(C) 35
(D) 15
(E) 18
Pg. 13 of 22
24) What is the output of the following code segment?
for(int i = 5; i > 0; i--)
{
for(int j = 1; j <= i; j++)
{
System.out.print(j * j + " ");
}
System.out.println();
}
Pg. 14 of 22
25) Consider the following code.
What is printed?
(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.
Pg. 15 of 22
26) Consider the following code segment
int x = 3, y = -2;
while(x > y)
{
x--;
y++;
}
System.out.println(x - y);
What is the output of the System.out.println statement?
(A) -1
(B) 1
(C) 0
(D) -2
(E) 2
27) Consider the following segment of code
int sum = 0;
for(int i = 0; i < 3; i++)
{
if((i % 2) - 1 == 0)
sum += 3;
else
sum++;
}
What will be the value of sum after the execution of code above?
(A) 4
(B) 5
(C) 6
Pg. 16 of 22
(D) 7
(E) 8
28) Under what condition will an ascending (lowest to highest) insertion sort execute faster?
(A) The elements are in random order
(B) The elements are in sorted in descending order.
(C) The elements are integers
(D) Best case, average case, and worst case are all the same.
(E) The elements are already sorted in ascending order.
29) Read the code below.
Pg. 17 of 22
What are the values for changerObj.str and chngerObj.n that are printed after this code executes?
str n
(A) world 6
(B) worldpeace 6
(C) world 12
(D) worldpeace 12
(E) peace 12
30) Consider the following code segment.
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.set(1,"c");
list.add(2, "d");
list.set(2, "e");
list.add("g");
System.out.println(list);
What is printed as a result of executing the code segment?
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]
31) When is the following Boolean expression true (a and b are integers)?
(a < b) && !(b > a)
A) It is always true
B) It is never true
Pg. 18 of 22
C) When a = b
D) When a < b
E) When a > b
32) Which of the following reasons for using an inheritance hierarchy are valid?
I. Methods from a superclass can be used in a subclass without rewriting or copying code.
II. Objects from subclasses can be passed as arguments to a method designed for the superclass
III. Objects from subclasses can be stored in the same array
IV. All of the above
V. None of the above
A) I and II
B) I and III
C) IV
D) V
E) I only
33) Which of the following code segments is equivalent to the code below?
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
Pg. 19 of 22
34) Consider the following segment of code
String word = "conflagration";
int x = word.indexOf("flag");
String str = word.substring(0,x);
What will be the result of executing the above segment?
(A) A syntax error will occur
(B) String str will be the empty string
(C) String str will contain "flag"
(D) String str will contain "conf"
(E) String str will contain "con"
35) 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}?
A) {3,6,8,5,1}, {3,5,6,8,1}, {1,3,5,6,8}
B) {1,3,8,5,6}, {1,3,8,5,6}, {1,3,5,8,6}, {1,3,5,6,8}
C) {3,6,8,5,1}, {3,6,8,5,1}, {3,5,6,8,1}, {1,3,5,6,8}
D) {1,3,8,5,6}, {1,3,5,8,6}, {1,3,5,6,8}
E) {1,6,3,8,5}, {1,3,6,8,5}, {1,3,5,6,8}
36) What is the output from the following code?
String s = "Computer Science is fun!";
String s1 = s.substring(0,8);
String s2 = s1.substring(1);
String s3 = s2.substring(1,3);
System.out.println(s3);
A) mput
B) mpu
C) mp
Pg. 20 of 22
D) omp
E) Om
37) Consider an array arr and a list list that is an ArrayList<String>. Both arr and list are initialized with string
values. Which of the following code segments correctly appends all the strings in arr to the end of list?
I. for(String s : arr)
list.add(s)
II. for(String s : arr)
list.add(list.size(),s);
III. for(int i = 0; i < arr.length; i++)
list.add(arr[i]);
(A) I only
(B) II only
(C) III only
(D) I and III only
(E) I, II, and III
38) Refer to the following method
public static int mystery(int n)
{
if(n == 1)
return 3;
else
return 3 * mystery(n-1);
}
What value does mystery(4) return?
(A) 3
Pg. 21 of 22
(B) 9
(C) 12
(D) 27
(E) 81
39) An algorithm for finding the average of N numbers is
average = sum/N
Where sum a nd N are both integers. Using this algorithm, if N is equal to 0 and the programmer doesn’t have
any built in tests to check if N is equal to zero, when will the error be detected?
(A) At compile time
(B) At edit time
(C) As soon as the value of N is entered
(D) During run time
(E) When an incorrect result is output
Pg. 22 of 22
40) Consider the following method. What is the output from conditionTest(3,-2)?
public static void conditionTest(int num1, int num2) {
if ((num1>0) && (num2>0)) {
if (num1>num2)
System.out.println("A");
else
System.out.println("B");
else if ((num2<0) || (num1<0)) {
System.out.println("C");
else if (num2 < 0) {
System.out.println("D");
else {
System.out.println("E");
A) A
B) B
C) C
D) D
E) E