AP-Style MC Practice QuestionsAP 风格选择题练习
Unit 4: Data Collections第 4 单元:数据集合CSA
Multiple Choice)Choose the best of the four options. Assume all referenced classes and methods are imported / available unless otherwise stated. "Consider the following code segment" implies the code compiles and runs without exception unless the question states otherwise.从四个选项中选择最合适的一个。除非另有说明,假定所有引用的类(class)与方法(method)均已导入且可用。"Consider the following code segment"("考虑以下代码段")默认代码可以正常编译并运行(不抛出异常),除非题目另有说明。
Consider the following code segment.考虑以下代码段。
int[] arr = {1, 2, 3, 4};
ArrayList<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
list.add(30);
System.out.println(arr.length + " " + list.size());
What is printed?输出是什么?(注意:数组用 length(字段),ArrayList 用 size()(方法调用)。)
4 33 34 4length field.编译错误:数组没有 length 字段。Consider the following code segment.考虑以下代码段。
int[] vals = {10, 20, 30};
System.out.println(vals[3]);
What is the result of executing the code segment?运行此代码段的结果是什么?(提示:数组有效索引为 0 到 length - 1。)
0 is printed (the default value).打印 0(默认值)。30 is printed (the last element).打印 30(最后一个元素)。ArrayIndexOutOfBoundsException is thrown.抛出 ArrayIndexOutOfBoundsException(数组越界 out-of-bounds)。Consider the following code segment.考虑以下代码段。
int[] vals = {1, 2, 3, 4};
for (int v : vals) {
v *= 10;
}
System.out.println(vals[0] + " " + vals[3]);
What is printed?输出是什么?(提示:增强 for 循环 enhanced for-loop 中的循环变量 v 是元素的副本,修改 v 不会影响原数组。)
10 401 41 40Consider the following code segment.考虑以下代码段。
int[] a = {1, 2, 3};
int[] b = a;
b[1] = 99;
int[] c = {1, 99, 3};
System.out.println(a[1] + " " + (a == b) + " " + (a == c));
What is printed?输出是什么?(注意:b = a; 让 a 与 b 成为别名 aliasing;== 比较引用地址,而不是数组内容。)
1 true false99 false false99 true true99 true falseConsider the following code segment.考虑以下代码段。
ArrayList<Integer> list = new ArrayList<>();
list.add(20); list.add(20); list.add(30); list.add(40);
for (int i = 0; i < list.size(); i++) {
if (list.get(i) % 20 == 0) {
list.remove(i);
}
}
System.out.println(list);
What is printed?输出是什么?(经典陷阱:在向前遍历时 list.remove(i) 会让后续元素左移一位,导致跳过元素。)
[30][20, 30][][20, 20, 30]Consider the following code segment.考虑以下代码段。
int[][] g = { {1, 2, 3},
{4, 5, 6},
{7, 8, 9} };
int sum = 0;
for (int r = 0; r < g.length; r++) {
for (int c = 0; c < g[r].length; c++) {
if (r + c == 2) sum += g[r][c];
}
}
System.out.println(sum);
What is printed?输出是什么?(条件 r + c == 2 选出反对角线 anti-diagonal 上的元素。)
9121518Consider the following code segment.考虑以下代码段。
public static void transpose(int[][] m) {
for (int r = 0; r < m.length; r++) {
for (int c = r + 1; c < m[r].length; c++) {
int tmp = m[r][c];
m[r][c] = m[c][r];
m[c][r] = tmp;
}
}
}
// in main:
int[][] g = { {1, 2, 3},
{4, 5, 6},
{7, 8, 9} };
transpose(g);
System.out.println(g[0][2] + " " + g[2][0] + " " + g[1][1]);
What is printed?输出是什么?(注意内层循环从 c = r + 1 开始,避免双重交换——这是原地转置 in-place transpose 的关键。)
3 7 57 3 53 3 56 8 5Consider the following code segment.考虑以下代码段。
ArrayList<Integer> a = new ArrayList<>();
a.add(1); a.add(2); a.add(3);
ArrayList<Integer> b = a;
b.set(0, 99);
b.add(4);
System.out.println(a.size() + " " + a.get(0) + " " + a.get(3));
What is printed?输出是什么?(b = a; 之后两者是别名;通过 b 的 set 与 add 都会影响同一个底层列表。)
3 1 ?3 99 44 1 44 99 4Consider the following code segment.考虑以下代码段。
int[] arr = {3, 7, 5, 7, 9, 7};
int idx = -1;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 7) {
idx = i;
}
}
System.out.println(idx);
What is printed?输出是什么?(提示:循环没有 break,会一直更新到最后一个匹配位置——线性"查找"的"最后一次出现 last occurrence"模式。)
-1135A selection sort (ascending) is applied to the array {5, 3, 8, 1, 9, 2}. What is the state of the array after the second complete pass of the outer loop (i.e., after the two smallest elements are placed at indices 0 and 1)?对数组 {5, 3, 8, 1, 9, 2} 进行选择排序(selection sort,升序)。完成外层循环的前两轮后(即最小的两个元素被放到下标 0 和 1 处之后),数组的状态是什么?
{1, 2, 3, 5, 8, 9}{1, 2, 8, 5, 9, 3}{1, 3, 8, 5, 9, 2}{3, 5, 8, 1, 9, 2}An insertion sort (ascending) is applied to the array {5, 2, 8, 1, 9, 3}. What is the state of the array after the second complete pass of the outer loop (i.e., after the elements at indices 1 and 2 have each been inserted into the sorted prefix)?对数组 {5, 2, 8, 1, 9, 3} 进行插入排序(insertion sort,升序)。完成外层循环的前两轮后(即下标 1 和 2 处的元素分别被插入到已排序的前缀里之后),数组的状态是什么?
{2, 5, 8, 1, 9, 3}{1, 2, 5, 8, 9, 3}{2, 5, 8, 9, 1, 3}{5, 2, 8, 1, 9, 3}A binary search is performed on the sorted array {2, 5, 7, 12, 18, 23, 31, 42, 56} searching for the value 19. Which element of the array is examined last before the search terminates? (Use mid = (lo + hi) / 2 with integer division.)对已排序数组 {2, 5, 7, 12, 18, 23, 31, 42, 56} 进行二分查找(binary search),目标值为 19。在查找终止之前,数组中最后被检查的元素是哪一个?(使用 mid = (lo + hi) / 2 整数除法。)
18233156Consider the following method.考虑以下方法。
public static String mystery(int n) {
if (n == 0) return "X";
return mystery(n - 1) + mystery(n - 1);
}
What is the value of mystery(4).length()?mystery(4).length() 的值是多少?(提示:每次递归调用两次 mystery(n - 1)——指数级分支 exponential branching,结果长度为 2^n。)
481632Consider the following method.考虑以下方法。
public static int g(int n) {
if (n < 2) return 0;
return 1 + g(n / 2);
}
What is the value of g(17)?g(17) 的值是多少?(提示:每次递归 n 整除以 2,本质是 ⌊log₂ n⌋ 的计数。)
34517Consider the following code segment.考虑以下代码段。
public static void modify(int[] a) {
a[0] = 99;
a = new int[]{100, 200, 300};
a[0] = 999;
}
// in main:
int[] vals = {1, 2, 3, 4, 5};
modify(vals);
System.out.println(vals[0] + " " + vals[2] + " " + vals.length);
What is printed?输出是什么?(关键:a[0] = 99 是通过引用就地修改 mutate,能被调用方看到;a = new int[]{...} 只是重新绑定 rebind 本地引用,对原数组无影响。)
1 3 599 3 5999 300 3100 300 3