AP CS Java Arrays Answers
AP CS Java Arrays Answers
C
25. D
26. A
27. E
28. D
29. D
30. E
31. A
32. A
33. B
34. D
35. B
36. D
37. D
38. E
39. E
40. D
41. C
42. A
43. E
Answer Explanations
1. (E) Segment I is an initializer list which is equivalent to
12. (E) Both correct solutions are careful not to lose the student who has
the highest idNum so far. Segment II does it by storing a reference to
the student, highestSoFar. Segment III does it by storing the array
index of that student. Code segment I is incorrect because it returns
the first student whose idNum is greater than max, not necessarily the
student with the highest idNum in the list.
13. (B) For each i, tickList[i] is a new Ticket object that must be
constructed using the Ticket constructor. Therefore eliminate
choices C, D, and E. Choice A is wrong because getRow(),
getSeat(), and getPrice() are accessors for values that already
exist for some Ticket object. Note also the absence of the dot
member construct.
14. (C) To access the price for each Ticket in the tickList array, the
getPrice() accessor in the Ticket class must be used, since price
is private to that class. This eliminates choices A and E. Choice B
uses the array name incorrectly. Choices D and E incorrectly declare
a Transaction object. (The method applies to an existing
Transaction object.)
16. (B) There are two problems with the segment as given:
1. arr[1] is not tested.
2. When i has a value of n-1, incrementing i will lead to an
out-of-range error for the if(arr[i] < min) test.
Modification II corrects both these errors. The change suggested in
III corrects neither of these errors. The change in I corrects (1) but not
(2).
17. (A) Notice that either vIndex or wIndex is incremented at the end of
the loop. This means that, when the loop is exited, the current values
of v[vIndex] and w[wIndex] have not been compared. Therefore,
you can only make an assertion for values v[0]..v[vIndex-1] and
w[0]..w[wIndex-1]. Also, notice that if there is no common value in
the arrays, the exiting condition for the while loop will be that the
end of one of the arrays has been reached, namely vIndex equals N
or wIndex equals M.
18. (B) Objects in an array can be changed in an enhanced for loop by
using mutator methods of the objects’ class. The changeStatus
method, a mutator in the Book class, will work as intended in the
given code. Choice C would be true if it were not given that each
Book in bookList was initialized. If any given b had a value of null,
then a NullPointerException would be thrown.
19. (D) The declaration must start with the type of value in the array,
namely BingoCard. This eliminates choices A and E. Eliminate
choice B: The type on the right of the assignment should be
BingoCard. Choice C is wrong because the number of slots in the
array should be NUMPLAYERS, not 20.
20. (C) Segment III is the only segment that works, since the enhanced
for loop should not be used to replace elements in an array. After the
declaration
BingoCard[] players = new BingoCard[NUMPLAYERS];
each element in the players array is null. The intent in the given
code is to replace each null reference with a newly constructed
BingoCard.
21. (D) The big defect of Algorithm 2 is that it eventually slows down.
This is because every time it selects an empty element, it has to loop
again. Each of the other choices is true. In choice A, for example, the
element cards[0] always moves to shuffled[0], eliminating all
permutations that have cards[0] in a different slot. For choice B, by
the time you get to assign the last element, all but two slots of the
cards array are marked empty. So, on average, you will need to go
through NUMCARDS tries to find one of those two nonempty slots. For
choice C, even though Algorithm 2 is slow, in theory every element
in cards could land in any given slot in shuffled. This is not true
for Algorithm 1, where the first element never budges out of the first
slot. For choice E, because of the precise ordering of elements in
Algorithm 1, the array will always eventually return to its original
state, assuming there are sufficient iterations.
22. (B) If starts with the value NUMCARDS, the method encounters
k
cards[NUMCARDS] on Line 7 and throws an
ArrayIndexOutOfBoundsException.
23. (E) All elements added to strList must be of type String. Each
choice satisfies this except choice E. Note that in choice D, the
expression ch + 8 becomes a String since ch is a String (just one
of the operands needs to be a String to convert the whole expression
to a String). In choice E, neither intOb nor 8 is a String.
24. (C) The effect of choice C is to adjust the size of the list to 7 and to
add the Integer 9 to the last slot (i.e., the slot with index 6).
Choices A, D, and E will all cause an IndexOutOfBoundsException
because there is no slot with index 6: the last slot has index 5. Choice
B will cause a compile-time error, since it is attempting to add an
element of type Double to a list of type Integer.
25. (D) If element is smaller than the last item in the list, it will be
compared with every item in the list. Eventually index will be
incremented to a value that is out of bounds. To avoid this error, the
test in the while loop should be
28. (D) Code segment III is wrong because the equals method is defined
for objects only. Since getValue returns a double, the quantities
c.getValue() and aCoin.getValue() must be compared either
using ==, or as described in the box on p. 70 (better).
29. (D) Segment II is the straightforward solution. Segment I is correct
because it initializes all slots of the matrix to 0, a perfect square. (By
default, all arrays of int or double are initialized to 0.) Segment III
fails because r is undefined in the condition c < mat[r].length. In
order to do a column-by-column traversal, you need to get the
number of columns in each row. The outer for loop could be
Now segment III works. Note that since the array is rectangular, you
can use any index k in the conditional c < mat[k].length, provided
that k satisfies the condition 0 ≤ k < mat.length (the number of
rows).
30. (E) When col is 0: row is 3, then 2, then 1.
When col is 1: row is 3, then 2.
When col is 2: row is 3.
Here are the corresponding elements, in order, that are printed:
The fifth element in the list is mat[2][1], which is 7.
31. (A) Method alter shifts all the columns, starting at column c+1, one
column to the left. Also, it does it in a way that overwrites column c.
Here are the replacements for the method call alter(1):
32. (A) matStuff processes the row selected by the row parameter, 2 in
the method call. The row value, 2, overwrites each element in row 2.
Don’t make the mistake of selecting choice B—the row labels are 0,
1, 2.
33. (B) Hand execute this for a 2 × 2 matrix. i goes from 0 to 0 and j
goes from 0 to 0, so the only interchange is swap mat[0][0] with
mat[1][1], which suggests choice B. Check with a 3 × 3 matrix:
Now consider one element, 12, say. It must be replaced by its vertical
mirror image 9, i.e., mat[2][3]=mat[2][0]. The value of width is 4.
See which expression in the answer choices correctly makes this
assignment. Eliminate choices A and D right away because col can
only have the values 0 and 1 in this algorithm, so mat[2][3] will not
be assigned. In choice B, when col has value 1, mat[2][3]=mat[2]
[1], an incorrect assignment. Choice C works: when row is 2 and col
is 0, mat[2][3]=mat[2][0]. In choice E, when row is 2 and col is 0,
the assignment mat[2][3]=mat[0][2] is incorrect.
42. (A) Method alter places a mirror along the major diagonal and
reflects the elements from left to right across this diagonal.
In this algorithm, when row is 1, col can only be 0, and when row is
2, col takes on the values 0 and 1. Thus, only three elements are
altered: mat[0][1], mat[0][2], and mat[1][2]. (Note that the
method assigns values to mat[col][row].) These elements are all to
the right of the diagonal. Choice A is the only choice that leaves
elements to the left of the diagonal unchanged.
43. (E) There are three things that must be done in each row:
s = s + "|";
s = s + "|\n";
for (int r = …)