Page 586 - AP Computer Science A, 7th edition
P. 586

(A) (int)(Math.random() ∗ 50) – 2 (B) (int)(Math.random() ∗ 49) – 2 (C) (int)(Math.random() ∗ 49) + 2 (D) (int)(Math.random() ∗ 50) + 2 (E) (int)(Math.random() ∗ 48) + 2
4. Consider the following code segment.
int num = 0, score = 10;
if (num != 0 && score / num > SOME_CONSTANT)
statement1; else
statement2;
What is the result of executing this statement?
(A) An ArithmeticException will be thrown.
(B) A syntax error will occur.
(C) statement1, but not statement2, will be executed.
(D) statement2, but not statement1, will be executed.
(E) Neither statement1 nor statement2 will be executed; control
will pass to the first statement following the if statement.
5. The following shuffle algorithm is used to shuffle an array of
int values, nums.
public void shuffle () {
for (int k = nums.length – 1; k > 0; k--)
{
int randPos = (int) (Math.random() ∗ (k + 1));
int temp = nums[k];
nums[k] = nums[randPos];
nums[randPos] = temp;
} }
Suppose the initial state of nums is 8, 7, 6, 5, 4, and when the











































































   584   585   586   587   588