Page 591 - AP Computer Science A, 7th edition
P. 591
public static int compute(int n) {
for (int i = 1; i < 4; i++) n ∗= n;
return n; }
Which of the following could replace the body of compute, so that the new version returns the identical result as the original for all n?
(A) return 4 ∗ n;
(B) return 8 ∗ n;
(C) return 64 ∗ n;
(D) return (int) Math.pow(n, 4); (E) return (int) Math.pow(n, 8);
13. Consider the following instance variable and method. private int[] nums;
/∗∗ Precondition: nums contains int values in no particular order.
∗/
public int getValue() {
for (int k = 0; k < nums.length; k++) {
if (nums[k] % 2 != 0) return k;
}
return –1; }
Suppose the following statement is executed:
int j = getValue();
If the value returned in j is a positive integer, which of the following best describes the contents of nums?