AP
®
COMPUTER SCIENCE A
2012 SCORING COMMENTARY
Question 3
Overview
This question entailed understanding array operations, array traversals, loops, and interfaces. Students
were provided with an interface,
Horse, and the framework for the HorseBarn class. In part (a)
students were required to implement the method findHorseSpace that searches an array instance
variable and returns the index of an element of the array that refers to an object whose name matches a
parameter value. This could be done by traversing the array, skipping over null elements, and
comparing the parameter with the value returned by the getName method of each object referenced by
the elements. If no matching object was found, the method needed to return -1. In part (b) students were
required to move the elements of the array such that the order of non-null elements was maintained,
and those elements were clustered beginning at index 0 with no interspersed null elements. There
were many ways to achieve this result, including two common approaches of either shifting array
elements in place or using a temporary auxiliary collection.
Sample: 3A
Score: 8
In part (a) the student uses a loop to examine the elem
ents in spaces. During each iteration of the loop,
the student uses the equals() method to compare the name of the horse currently being examined
with the name of the horse to find. The student does not check if the current array element is
null
before invoking getName() to obtain the name of the horse. If the current element is null, an
exception will be thrown at runtim
e. This was a common error made by students and resulted in the loss of
1 point. Assuming that the comparison does not throw an exception, the student correctly returns the
index of the space containing the horse, if the horse is in the barn. If the specified horse is not in the barn,
the return statement after the loop correctly returns -1. Part (a) earned 3 points.
In part (b) the student creates a new array that is the same
size as the array spaces and holds
references to instances of Horse objects. The student uses a loop to iterate through spaces and copy,
in order, the non-null elements to the new array. A local variable count is used to keep track of where
the next reference should be copied to. Because count is initialized to 0 and is incremented by 1 each
time a reference is copied to the new array, the references are copied to the new array starting at index 0
with no spaces between any two references. Note that because array elements are initialized to null,
there is no need to make any further modifications to the new array. The last statement in the method
changes the instance variable spaces so that it refers to the new array. Part (b) earned all 5 points.
Sample: 3B
Score: 5
In part (a) the student uses a loop to examine the elements in spaces. During each iteration of the loop,
the student attempts to use the equals() method to compare the name of the horse currently being
examined with the name of the horse to find. The student does not use the method
getName() to
obtain a reference to the nam
e of the horse and so lost 1 point. Additionally, the student does not check if
the current array element is null and so lost 1 point. Assuming that the comparison is done correctly,
the student returns the index of the space containing the horse. If the specified horse is not in the barn,
the return statem
ent after the loop correctly returns -1. Part (a) earned 2 points.
© 2012 The College Board.
Visit the College Board on the Web: www.collegeboard.org.