2020 MC Corrections
Review for AP Exam
Q3 hours to travel
double hours = (double) (Math.abs(marker1 - marker2)/60);
is incorrect, because hours is casted to a double too late. /60
does integer division. The correct answer is double hours = Math.abs(marker1 - marker2)/60.0;
Q5 new Bird constructors
E
is incorrect because it has the same signature as the existing constructor, causing a compiler error.
Q6 Cast to double
Casting integer division will not result in a double (double) 2/4 + 3
is correct because it casts 2 to a double, then performs floating point division.
Q7 if and compound boolean equivalence
b1 is true if it between 0 and 100 exclusive, or if it is less than -100. This is what E
assigns to b2.
Q10 printSome method
printSome(20,5)
is correct, because 10 is divisibly by 5 and in the range from 0 to 19.
Q12 Book class
If no Person object has been assigned to borroer, .getName()
throws NullPointerException. borrower != null
is the only option that contains a reference to an object.
Q14 zip code categories
II
is incorrect becuse it uses one-way selection statements, and will always return rural. III
also works because it will return to correct value immediately, even if it has one-way selection statements.
Q18 countPeaks method
int p = 1; p < data.length - 1; p++
is the correct answer. We start at index 1 to look for the first local maximum, and the last candidate is data.length - 2 (next to last element).
Q21 strArrMethod - first day of spring The method assigns the shortest string between arr[n] and arr[arr.length - 1] to result[n]. The shortest string is "of", so it will be assigned to every element, except for the last element.
Q22 strArrMethod - last day of the school year Line 12 is executed each time sm is updated because a new smallest value is found. This happens 2 times (last, day, the, school)
Q24 increment value in SomeClass
Since y is declared as a static variable, it is associated with the class and all objects of the class share the single variable y. y is equal to three when all of the classes are initialized. It is then incremented by 1. It is then incremented by 10. When getY
is classed on the third object, it is 14.
Q26 code segments print A and B There are m n iterations of the for loop in segment I. In Segment II, there are m (n-1). Thus, A is printed m more times than B.
Q27 conditions for Boolean expression
Use a truth table. (a && b) || (!a && b);
evaluates to false when b is false.
Q28 sing the song
abMethod
sets x to the index of an occurrence of b in a, then assignes the result of the concatenation of the parts of a before an after the occurrence of b. Thus, it removes all non-overlapping occrrences of string b from string a.
Q28 calcMethod
calcMethod(16)
returns 16 + 8 + 4 + 2 + 1 + 10 = 41. Make sure to include the end case.
Q31 count negative values III does not work because the variable i represents an element of the array rather than an index.
Q34 remove some ArrayList elements The method skips some lemenets of numList during traversal. When element at position i is removed from the numList, subsequent elements are shifted left.
Q38 sum of ArrayList values II is correct, because it uses an enhanced for loop to traverse the valueList array.
Q39 recursive String method Recursion.
Q40 remove a method The statement in line 3 will cause a compiler error because the message method for obj1 cannot be found. Methods in or inherited by the declared type must work.