Page 63 - AP Computer Science A, 7th edition
P. 63
35.
}
When the programmer tests the constructor of the Caller class, she gets a NullPointerException. Which could be the cause of this error?
(A) (B) (C) (D)
The Caller object in the driver class was not created with new.
The programmer forgot the return statement in getList that returns the list of Integers.
The declaration of numbers is incorrect. It needed to be private List<Integer> numbers = null;
In the getList method, an attempt was made to add an Integer to an ArrayList that had not been created with new.
(E)
Consider method findSomething below:
The shuffleNumbers algorithm went out of range, causing a null Integer to be shuffled into the ArrayList.
/∗ ∗ Precondition: a.length is equal to b.length. ∗ / public static boolean findSomething(int[] a, int[] b)
{
for (int aValue: a) {
boolean found = false; for (int bValue: b)
{
if (bValue == aValue) found = true;
}
if (!found)
return false;
}
return true; }