2020 Exam-Sample-Questions-Computer-Science
2020 Exam-Sample-Questions-Computer-Science
Sample
Questions
AP® COMPUTER SCIENCE A
2020 Exam Sample Question 1
(Adapted from: AP® Computer Science A Course and Exam Description)
Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE
TO BE WRITTEN IN JAVA.
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.
Question 1 - Array/ArrayList
Allotted time: 25 minutes (plus 5 minutes to submit)
The Gizmo class represents gadgets that people purchase. Some Gizmo objects are
electronic and others are not. A partial definition of the Gizmo class is shown below.
public class Gizmo
{
/** Returns the name of the manufacturer of this Gizmo. */
public String getMaker()
{ /* implementation not shown */ }
/** Returns true if this Gizmo is electronic, and false otherwise.
*/
public boolean isElectronic()
{ /* implementation not shown */ }
/** Returns true if this Gizmo is equivalent to the Gizmo object
* represented by the parameter, and false otherwise.
*/
public boolean equals(Object other)
{ /* implementation not shown */ }
// There may be instance variables, constructors, and methods not shown.
}
/** Returns the number of purchased Gizmo objects that are electronic and are
* manufactured by maker, as described in part (a).
*/
public int countElectronicsByMaker(String maker)
{ /* to be implemented in part (a) */ }
/** Returns true if any pair of adjacent purchased Gizmo objects are equivalent, and
* false otherwise, as described in part (b).
*/
public boolean hasAdjacentEqualPair()
{ /* to be implemented in part (b) */ }
Index in purchases 0 1 2 3 4 5
Value returned by method
true false true false true
false
call isElectronic()
Value returned by method
“ABC” “ABC” “XYZ” “lmnop” “ABC”
“ABC”
call getMaker()
Complete method countElectronicsByMaker below.
/** Returns the number of purchased Gizmo objects that are electronic and
* whose manufacturer is maker, as described in part (a).
*/
public int countElectronicsByMaker(String maker)
(b) When purchasing items online, users occasionally purchase two identical
items in rapid succession without intending to do so (e.g., by clicking a
purchase button twice). A vendor may want to check a user’s purchase
history to detect such occurrences and request confirmation.
Write the hasAdjacentEqualPair method. The method detects
whether two adjacent Gizmo objects in purchases are equivalent, using
the equals method of the Gizmo class. If an adjacent equivalent pair is
found, the hasAdjacentEqualPair method returns true. If no such
pair is found, or if purchases has fewer than two elements, the method
returns false.
Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE
TO BE WRITTEN IN JAVA.
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.
This question involves the use of check digits, which can be used to help detect if
an error has occurred when a number is entered or transmitted electronically.
Return
Method Call Explanation
Value
getCheck(159) 2 The check digit for 159 is 2.
The number 1592 is a valid
isValid(1592) true combination of a number (159) and its
check digit (2).
The number 1593 is not a valid
isValid(1593) false
combination of a number (159) and its
check digit (3) because 2 is the check
digit for 159.
The following verbs are in addition to the Task Verbs in Free-Response Questions
on page 190 of the AP Computer Science A Course and Exam Description:
•• Identify: Provide a name for the specific addition or modification as it relates to the
problem, without elaboration or explanation. For example, “I need a new variable
to represent the item that appears the greatest number of times in the list.”
•• Describe: Provide the relevant features or characteristics (including any
visibility or type) of your proposed modifications. For example, “I need a new
instance variable, which will be a new private String object in the Sample class
representing the item that appears the greatest number of times in the list.”
This example includes the identification and description necessary for this
specific addition.
Note that these examples are not responses to the sample questions given but
highlight what is expected when students are asked to identify or describe.
For features and characteristics of variables, constructors, and methods, see the
following topics in the AP Computer Science A Course and Exam Description:
•• Variables: 1.2, 2.2, 5.7, 5.8
•• Constructors: 2.2, 5.2
•• Methods: 5.4, 5.5, 5.6, 5.7, 5.8