AP
®
COMPUTER SCIENCE A
2019 SCORING COMMENTARY
Question 3
Overview
This question tested the student's ability to:
• Write program code to satisfy methods using expressions, conditional statements, and iterative
stateme
nts; and
• Write program code to create, traverse, and manipulate elements in 1D array or
ArrayList
objects.
This question involved manipulation of both a one-dimensional array and an
ArrayList, both containing
String
values. Students were expected to write two methods in the enclosing Delimiters class, making
use of two instance variables of type
String.
In part (a) students were expected to create an
ArrayList of String objects, then add to it all the values
from the given array that matched either of two instance variables. Students had to construct a new
ArrayList
and write a loop to access each element of an array parameter. Inside the loop, students were
expected to compare each
String value in the array with each of the two instance variables and add each
matching
String value to the constructed ArrayList.
In part (b) students were given an
ArrayList containing String objects representing open and close
delimiters. Students were asked to develop an algorithm to determine whether the given
ArrayList
represents a balanced sequence of open and close delimiters. A sequence is balanced when two conditions are
met: (1) When traversing the
ArrayList from the first element to the last element, there is no point at which
there are more close delimiters than open delimiters. (2) The total number of open delimiters is equal to the total
number of close delimiters. Students had to write a loop to access each element of the given
ArrayList.
Inside the loop, students had to compare each
String value in the ArrayList to the instance variables
and then update accumulator(s) appropriately.
Sample: 3A
Sc
ore: 9
In
part (a) the response constructs a new
ArrayList and assigns it to a local variable. Therefore the response
earned point 1. All elements of
tokens are accessed with an enhanced for loop, so point 2 was earned.
Each token from the array is compared to both instance variables using the
equals method, and the response
earned point 3. Finally, each identified delimiter is added to the
ArrayList, and point 4 was earned. Part (a)
earned 4 points.
In part (b) the response initializes a variable as an accumulator and updates it la
ter; therefore, it earned point 5.
All necessary elements of
delimiters are accessed with a for loop using ArrayList notation
correctly, so point 6 was earned. Each delimiter from the
ArrayList is compared to an instance variable
using the
equals method, and the accumulator is updated. Therefore the response earned point 7. The
response returns
false if there are more close delimiters than open delimiters within the loop by determining
if the number of open delimiters that have not yet matched a close delimiter ever becomes negative. It also
returns
true after the loop if and only if there are no more open delimiters that have not matched a close
delimiter. Because the response returns the correct
boolean value for both conditions, points 8 and 9 were
both earned. Part (b) earned 5 points.
© 2019 The College Board.
Visit the College Board on the web: collegeboard.org.