Page 587 - AP Computer Science A, 7th edition
P. 587
method is executed the values generated for randPos are 3, 2, 0, 0, in that order. What element will be contained in nums[2] after execution?
(A) 8 (B) 7 (C) 6 (D) 5 (E) 4
6. Consider the following instance variables and method assignValues in the same class:
private int numRows; private int numCols; private int[][] mat;
/∗ ∗ arr has numCols elements ∗ /
private void assignValues(int[] arr, int value) {
for (int k = 0; k < arr.length; k++) arr[k] = value;
}
Which of the following code segments will correctly assign mat to have the value 100 in each slot? You may assume that the instance variables have all been correctly initialized.
for (int row = 0; row < numRows; row++) assignValues(mat[row], 100);
for (int col = 0; col < numCols; col++) assignValues(mat[col], 100);
for (int[] row: mat) III for (int num: row)
I
II
(A) I only (B) II only (C) III only
num = 100;