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

32.
(D) The method as given will throw an ArrayIndexOutOfBoundsException. F or t he m at rix in t he example, mat[0].length is 4. The call mat.alter(1) gives c a value of 1. Thus, in the inner for loop, j goes from 1 to 3. When j is 3, the line mat[i][j] = mat[i][j+1] becomes mat[i][3] = mat[i][4]. Sinc e c olum ns go f rom 0 t o 3, mat[i][4] is out of range. The changes in segments I and II both fix this problem. In each case, the correct replacements are made for each row i: mat[i][1] = mat[i][2] and mat[i] [2] = mat[i][3]. Segment III makes the following incorrect replacements as j goes from 3 to 2: mat[i][2] = mat[i][3] and mat[i][1] = mat[i][2]. This will cause both columns 1 and 2 to be overwritten. Before inserting zeros in the last column, mat will be
2999 1333 0222
This does not achieve the intended postcondition of the method.































































































   1034   1035   1036   1037   1038