AP Comp Sci A 2020 Practice Questions
AP Comp Sci A 2020 Practice Questions
AP Comp Sci A 2020 Practice Questions
Practice Exam #1
and Notes
For the
Spring 2020
Exam
© 2020 College Board. College Board, Advanced Placement, AP, AP Central, and the acorn logo are registered
trademarks of College Board.
Visit College Board on the web: collegeboard.org.
I. Practice Exam
Exam Content and Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Administering the Practice Exam . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Answer Sheet for Multiple-Choice Section. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
AP® Computer Science A Practice Exam. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
II. Notes on the Practice Exam
Multiple-Choice Section. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Answer Key and Question Alignment to Course Framework. . . . . . . . . . 116
Free-Response Section. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Contact Us . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
You have 1 hour and 30 minutes for this part. Open your Section I
booklet and begin.
Note Start Time ________ . Note Stop Time ________ . After 1 hour and
20 minutes, say:
You have 1 hour and 30 minutes to complete Section II. You may use
any blank space of the page the questions or documents are printed on
to organize your answers and for scratch work. You must write your
answers in the answer booklet for free-response questions. At the top
of each page in your booklet you must fill in the circle that indicates
the question number you are answering. Open your Section II booklet
and begin.
Note Start Time ________ . Note Stop Time ________ . After 1 hour and
20 minutes, say
There are 10 minutes remaining.
Collect a Section II booklet from each student and check that each student wrote
his or her answers on the pages corresponding to each question. Then say:
The exam is over. You are now dismissed.
Instructions
At a Glance
The Java Quick Reference is located inside the front cover of this booklet.
Total Time
1 hour and 30 minutes Section I of this exam contains 40 multiple-choice questions.
Number of Questions Indicate all of your answers to the multiple-choice questions on the answer sheet. No credit
40 will be given for anything written in this exam booklet, but you may use the booklet for
Percent of Total Score notes or scratch work.
50%
Writing Instrument Use your time effectively, working as quickly as you can without losing accuracy. Do not
Pencil required spend too much time on any one question. Go on to other questions and come back to
Electronic Device the ones you have not answered if you have time. It is not expected that everyone will
None allowed know the answers to all of the multiple-choice questions.
Your total score on the multiple-choice section is based only on the number of questions
answered correctly. Points are not deducted for incorrect answers or unanswered
questions.
E remove(int index) Removes element from position index, moving elements at position
index + 1 and higher to the left (subtracts 1 from their indices) and
subtracts 1 from size; returns the element formerly at position index
Object Class
boolean equals(Object other)
String toString()
Directions: Determine the answer to each of the following questions or incomplete statements, using the available
space for any necessary scratch work. Then decide which is the best of the choices given and then enter the letter
in the corresponding space on the answer sheet. No credit will be given for anything written in the exam booklet.
Do not spend too much time on any one problem.
Notes:
• Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
• Assume that declarations of variables and methods appear within the context of an enclosing class.
• Assume that method calls that are not prefixed with an object or class name and are not shown within a complete
class definition appear within the context of an enclosing class.
• Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods
are called only when their preconditions are satisfied.
(A) 14.0
(B) 18.0
(C) 20.0
(D) 20.5
(E) 26.0
2. Consider the following code segment. Assume num is a properly declared and initialized int variable.
if (num > 0)
{
if (num % 2 == 0)
{
System.out.println("A");
}
else
{
System.out.println("B");
}
}
Which of the following best describes the result of executing the code segment?
(A) When num is a negative odd integer, "B" is printed; otherwise, "A" is printed.
(B) When num is a negative even integer, "B" is printed; otherwise, nothing is printed.
(C) When num is a positive even integer, "A" is printed; otherwise, "B" is printed.
(D) When num is a positive even integer, "A" is printed; when num is a positive odd integer, "B" is
printed; otherwise, nothing is printed.
(E) When num is a positive odd integer, "A" is printed; when num is a positive even integer, "B" is
printed; otherwise, nothing is printed.
The following table shows two examples of the intended behavior of getHours, based on the int
parameters marker1 and marker2.
(A) XY
(B) XYZ
(C) Y
(D) YY
(E) Z
(A) In line 6, the Points constructor must have a void return type.
(B) In lines 8 and 9, int values cannot be assigned to double variables.
(C) In line 12, the incrementPoints method must have a non-void return type.
(D) In lines 14 and 15, the variables n1 and n2 are not defined.
(E) In lines 14 and 15, the variable value is not defined.
numList.add(3);
numList.add(2);
numList.add(1);
numList.add(1, 0);
numList.set(0, 2);
System.out.print(numList);
if ( /* missing condition */ )
{
System.out.println(" Borrower: " + borrower.getName());
}
}
(A) I only
(B) II only
(C) III only
(D) I and II
(E) II and III
13. Assume that a, b, and c are boolean variables that have been properly declared and initialized.
Which of the following boolean expressions is equivalent to !(a && b) || c ?
(A) a && b && c
(B) a || b || c
(C) !a && !b || c
(A) I only
(B) III only
(C) I and II only
(D) I and III only
(E) I, II, and III
15. Consider the following code segment. Assume that a is greater than zero.
int a = /* value not shown */;
int b = a + (int) (Math.random() * a);
Which of the following best describes the value assigned to b when the code segment is executed?
(A) a
(B) 2 * a
(C) A random integer between 0 and a - 1, inclusive
(D) A random integer between a and 2 * a, inclusive
(E) A random integer between a and 2 * a - 1, inclusive
stars(num - 1);
(A) *****
(B) **
***
****
*****
(C) *
**
***
****
*****
(D) *****
****
***
**
(E) *****
****
***
**
*
(A) 10
(B) 20
(C) 60
(D) 70
(E) 100
(A) 4 times
(B) 5 times
(C) 6 times
(D) 15 times
(E) 30 times
(B) 1
(C) 11
(D) 14
(E) 30
return temp;
}
What, if anything, is returned by the method call rearrange("apple") ?
(A) "appl"
(B) "apple"
(C) "elppa"
(D) "lppa"
(E) Nothing is returned due to a run-time error.
Assume that the initial values of m and n are the same in code segment I as they are in code segment II.
Which of the following correctly compares the number of times that "A" and "B" are printed when each
code segment is executed?
while (x >= 0)
{
a = a.substring(0, x) + a.substring(x + b.length());
x = a.indexOf(b);
}
return a;
}
What, if anything, is returned by the method call abMethod("sing the song", "ng") ?
(A) "si"
(B) "si the so"
(C) "si the song"
(D) "sig the sog"
(E) Nothing is returned because a StringIndexOutOfBoundsException is thrown.
public Rectangle()
{
height = 1;
width = 1;
}
public Rectangle(int x)
{
height = x;
width = x;
}
(A) super();
(B) super(x);
(C) Rectangle(x);
(A) I only
(B) II only
(C) I and II only
(D) I and III only
(E) I, II, and III
System.out.println();
}
What is printed as a result of executing this code segment?
(A) A E I
F J
K
(B) B F J
C G K
D H L
(C) E I
F J
G K
H L
(D) F G H
J K L
(E) F J
G K
H L
int i = 0;
while (i < numList.size())
{
int num = numList.get(i);
if (num % key == 0)
{
numList.remove(i);
returnList.add(num);
}
i++;
}
return returnList;
}
As an example, if the method is called with an ArrayList containing the values [5, 2, 10, 20, 16]
and the parameter key has the value 5, then numList should contain [2, 16] at the end of the
method and an ArrayList containing [5, 10, 20] should be returned.
Which of the following best explains why the method does not always work as intended?
(A) The method attempts to add an element to returnList after that element has already been removed
from numList.
(B) The method causes a NullPointerException to be thrown when no matches are found.
(C) The method causes an IndexOutOfBoundsException to be thrown.
(D) The method fails to correctly determine whether an element of numList is divisible by key.
(E) The method skips some elements of numList during the traversal.
(A) I only
(B) II only
(C) III only
(D) I and II
(E) I and III
(A) The statement in line 3 will cause a compiler error because the message method for obj1 cannot
be found.
(B) The statement in line 4 will cause a compiler error because the message method for obj2 cannot
be found.
(C) As a result of the method call in line 3, the message method in class B will be executed instead of
the message method in class A.
(D) As a result of the method call in line 4, the message method in class B will be executed instead of
the message method in class A.
(E) The behavior of the code segment will remain unchanged.
Instructions
At a Glance
The questions for Section II are printed in this booklet. You may use the pages in this
Total Time booklet to organize your answers and for scratch work, but you must write your answers in
1 hour and 30 minutes the blank space provided for each question.
Number of Questions
4 The Java Quick Reference is located inside the front cover of this booklet.
Percent of Total Score
50%
Write your answer to each question in the blank space provided. Begin your response to
each question at the top of a new page and completely fill in the circle at the top of each
Writing Instrument
Pencil page that corresponds to the question you are answering.
Electronic Device All program segments must be written in Java. Show all your work. Credit for partial
None allowed solutions will be given. Write clearly and legibly. Erased or crossed-out work will not be
Weight scored.
The questions are
weighted equally. Manage your time carefully. Do not spend too much time on any one question. You may
proceed freely from one question to the next. You may review your responses if you finish
before the end of the exam is announced.
E remove(int index) Removes element from position index, moving elements at position
index + 1 and higher to the left (subtracts 1 from their indices) and
subtracts 1 from size; returns the element formerly at position index
Object Class
boolean equals(Object other)
String toString()
Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE
WRITTEN IN JAVA. You may plan your answers in this Questions booklet, but no credit will be given for anything
written in this booklet. You will only earn credit for what you write in the Free Response booklet.
Notes:
• Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
• Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods are
called only when their preconditions are satisfied.
• In writing solutions for each question, you may use any of the accessible methods that are listed in classes defined
in that question. Writing significant amounts of code that can be replaced by a call to one of these methods will not
receive full credit.
For this question, assume that when the rules are applied, the sequence will eventually terminate with the term
n = 1.
Example 1: 5, 16, 8, 4, 2, 1
• The first term is 5, so the second term is 5 * 3 + 1 =16.
16
• The second term is 16, so the third term is = 8.
2
8
• The third term is 8, so the fourth term is = 4.
2
4
• The fourth term is 4, so the fifth term is = 2.
2
2
• The fifth term is 2, so the sixth term is = 1.
2
• Since the sixth term is 1, the sequence terminates.
Example 2: 8, 4, 2, 1
8
• The first term is 8, so the second term is = 4.
2
4
• The second term is 4, so the third term is = 2.
2
2
• The third term is 2, so the fourth term is = 1.
2
• Since the fourth term is 1, the sequence terminates.
/** Returns true if the hailstone sequence that starts with n is considered long
* and false otherwise, as described in part (b).
* Precondition: n > 0
*/
public static boolean isLongSeq(int n)
{ /* to be implemented in part (b) */ }
/** Returns the proportion of the first n hailstone sequences that are considered long,
* as described in part (c).
* Precondition: n > 0
*/
public static double propLong(int n)
{ /* to be implemented in part (c) */ }
Write the method hailstoneLength(int n), which returns the length of the hailstone sequence
that starts with n.
/** Returns the length of a hailstone sequence that starts with n, as described in part (a).
* Precondition: n > 0
*/
public static int hailstoneLength(int n)
____________________________________________________________________
Begin your response at the top of a new page in the Free Response booklet
and fill in the appropriate circle indicating the question number.
If there are multiple parts to this question, write the part letter with your response.
Write the method isLongSeq(int n), which returns true if the hailstone sequence starting
with n is considered long and returns false otherwise. Assume that hailstoneLength
works as intended, regardless of what you wrote in part (a). You must use hailstoneLength
appropriately to receive full credit.
/** Returns true if the hailstone sequence that starts with n is considered long
* and false otherwise, as described in part (b).
* Precondition: n > 0
*/
public static boolean isLongSeq(int n)
____________________________________________________________________
Begin your response at the top of a new page in the Free Response booklet
and fill in the appropriate circle indicating the question number.
If there are multiple parts to this question, write the part letter with your response.
Consider the following table, which provides data about the hailstone sequences with starting values
between 1 and 10, inclusive.
The method call Hailstone.propLong(10) returns 0.5, since 5 of the 10 hailstone sequences
shown in the table are considered long.
Write the propLong method. Assume that hailstoneLength and isLongSeq work as
intended, regardless of what you wrote in parts (a) and (b). You must use isLongSeq appropriately to
receive full credit.
/** Returns the proportion of the first n hailstone sequences that are considered long,
* as described in part (c).
* Precondition: n > 0
*/
public static double propLong(int n)
____________________________________________________________________
Begin your response at the top of a new page in the Free Response booklet
and fill in the appropriate circle indicating the question number.
If there are multiple parts to this question, write the part letter with your response.
Value Returned
Statements (blank if no value Comment
returned)
GameSpinner g = new Creates a new spinner with four sectors
GameSpinner(4);
Returns the length of the current run. The length
g.currentRun(); 0 of the current run is initially 0 because no
spins have occurred.
Returns a random integer between 1 and 4,
g.spin(); 3 inclusive. In this case, 3 is returned.
The length of the current run is 1 because
g.currentRun(); 1 there has been one spin of 3 so far.
Returns a random integer between 1 and 4,
g.spin(); 3 inclusive. In this case, 3 is returned.
The length of the current run is 2 because
g.currentRun(); 2 there have been two 3s in a row.
Returns a random integer between 1 and 4,
g.spin(); 4 inclusive. In this case, 4 is returned.
The length of the current run is 1 because the
g.currentRun(); 1 spin of 4 is different from the value of the spin
in the previous run of two 3s.
Returns a random integer between 1 and 4,
g.spin(); 3 inclusive. In this case, 3 is returned.
The length of the current run is 1 because the
g.currentRun(); 1 spin of 3 is different from the value of the spin
in the previous run of one 4.
Returns a random integer between 1 and 4,
g.spin(); 1 inclusive. In this case, 1 is returned.
Returns a random integer between 1 and 4,
g.spin(); 1 inclusive. In this case, 1 is returned.
Returns a random integer between 1 and 4,
g.spin(); 1 inclusive. In this case, 1 is returned.
The length of the current run is 3 because
g.currentRun(); 3 there have been three consecutive 1s since the
previous run of one 3.
____________________________________________________________________
Begin your response at the top of a new page in the Free Response booklet
and fill in the appropriate circle indicating the question number.
If there are multiple parts to this question, write the part letter with your response.
The ReviewCollector class, shown below, is used to represent a collection of reviews to be analyzed.
public class ReviewCollector
{
private ArrayList<ProductReview> reviewList;
private ArrayList<String> productList;
/** Adds a new review to the collection of reviews, as described in part (a). */
public void addReview(ProductReview prodReview)
{ /* to be implemented in part (a) */ }
/** Returns the number of good reviews for a given product name, as described in part (b). */
public int getNumGoodReviews(String prodName)
{ /* to be implemented in part (b) */ }
____________________________________________________________________
Begin your response at the top of a new page in the Free Response booklet
and fill in the appropriate circle indicating the question number.
If there are multiple parts to this question, write the part letter with your response.
____________________________________________________________________
Begin your response at the top of a new page in the Free Response booklet
and fill in the appropriate circle indicating the question number.
If there are multiple parts to this question, write the part letter with your response.
public ReviewCollector()
public void addReview(ProductReview prodReview)
public int getNumGoodReviews(String prodName)
The Seat class, shown below, represents seats in the theater. The boolean instance variable
available is false if a ticket for the seat has been sold (the seat is no longer available). The int
instance variable tier indicates whether the seat is a tier 1 or tier 2 seat.
public class Seat
{
private boolean available;
private int tier;
/** Returns true if a seat holder was reassigned from the seat at fromRow, fromCol
* to the seat at toRow, toCol; otherwise it returns false, as described in part (b).
* Precondition: fromRow, fromCol, toRow, and toCol represent valid row and
* column positions in the theater.
* The seat at fromRow, fromCol is not available.
*/
public boolean reassignSeat(int fromRow, int fromCol,
int toRow, int toCol)
{ /* to be implemented in part (b) */ }
}
Row 0 of the theaterSeats array represents the row closest to the stage. All tier 1 seats are
closer to the stage than tier 2 seats.
____________________________________________________________________
Begin your response at the top of a new page in the Free Response booklet
and fill in the appropriate circle indicating the question number.
If there are multiple parts to this question, write the part letter with your response.
The reassignSeat method has four int parameters representing the row and column indexes of the
source (“from”) and destination (“to”) seats. If the reassignment is possible, the source seat becomes
available, the destination seat becomes unavailable, and the method returns true. If the seat
reassignment is not possible, no changes are made to either seat and the method returns false. Assume
that the source seat is occupied when the method is called.
____________________________________________________________________
Begin your response at the top of a new page in the Free Response booklet
and fill in the appropriate circle indicating the question number.
If there are multiple parts to this question, write the part letter with your response.
END OF EXAM
Page 2
Use a pencil only. Do NOT write your name. Do NOT write outside the box.
Page 3
Use a pencil only. Do NOT write your name. Do NOT write outside the box.
Page 4
Use a pencil only. Do NOT write your name. Do NOT write outside the box.
Page 5
Use a pencil only. Do NOT write your name. Do NOT write outside the box.
Page 6
Use a pencil only. Do NOT write your name. Do NOT write outside the box.
Page 7
Use a pencil only. Do NOT write your name. Do NOT write outside the box.
Page 8
Use a pencil only. Do NOT write your name. Do NOT write outside the box.
Page 9
Use a pencil only. Do NOT write your name. Do NOT write outside the box.
Page 10
Use a pencil only. Do NOT write your name. Do NOT write outside the box.
Page 11
Use a pencil only. Do NOT write your name. Do NOT write outside the box.
Page 12
Use a pencil only. Do NOT write your name. Do NOT write outside the box.