A P  C O M P U T E R  S C I E N C E  A
Unit 4 · Data Collections第 4 单元 · 数据集合

Data Collections数据集合

AP-Style MC Practice QuestionsAP 风格选择题练习

MEDIUM HARD AP MC

Unit 4: Data Collections4 单元:数据集合CSA



Name:姓名:Date:日期:

Draft v2 — 15 AP-style MCQs at exam level (3 MED + 12 HARD); array/ArrayList/2D/search/sort/recursion trace heavy with realistic distractors. HARDs upgraded: array aliasing + ==, 2D anti-diagonal sum, in-place transpose, ArrayList aliasing, exponential-branching recursion, array rebind-vs-mutate. Assumes top-score target. No calculator. Java Quick Reference assumed available.v2 草稿 —— 15 道 AP 风格选择题(MC),考试难度(3 道 MED + 12 道 HARD);偏重数组/ArrayList/二维/查找/排序/递归追踪,干扰项贴近真题。HARD 题目升级方向:数组别名(aliasing)+ ==、二维反对角线(anti-diagonal)求和、原地转置(in-place transpose)、ArrayList 别名、指数级分支(exponential branching)递归、数组"重新绑定 vs 原地修改"。面向冲击 5 分的考生。不允许使用计算器。假定考生可使用 Java Quick Reference。

MULTIPLE CHOICE15 questions · ~34 min at exam pace15 道题 · 考试节奏约 34 分钟

Multiple Choice选择题(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"("考虑以下代码段")默认代码可以正常编译并运行(不抛出异常),除非题目另有说明。

Q1MEDIUM AP MC 4.3 / 4.8 length vs size [1]

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(字段),ArrayListsize()(方法调用)。)

Q2MEDIUM AP MC 4.3 Out-of-Bounds [1]

Consider the following code segment.考虑以下代码段。

int[] vals = {10, 20, 30};
System.out.println(vals[3]);

What is the result of executing the code segment?运行此代码段的结果是什么?(提示:数组有效索引为 0length - 1。)

Q3MEDIUM AP MC 4.4 Enhanced for-loop [1]

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 不会影响原数组。)

Q4HARD AP MC 4.4 Array aliasing + == on references [1]

Consider 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;ab 成为别名 aliasing== 比较引用地址,而不是数组内容。)

Q5HARD AP MC 4.9 ArrayList Remove + Shift [1]

Consider 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) 会让后续元素左移一位,导致跳过元素。)

Q6HARD AP MC 4.12 2D anti-diagonal sum [1]

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 上的元素。)

Q7HARD AP MC 4.12 In-place 2D transpose [1]

Consider 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 的关键。)

Q8HARD AP MC 4.8 ArrayList aliasing through set + add [1]

Consider 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; 之后两者是别名;通过 bsetadd 都会影响同一个底层列表。)

Q9HARD AP MC 4.14 Linear "Find" — No Break [1]

Consider 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"模式。)

Q10HARD AP MC 4.15 Selection Sort — 2 Passes [1]

A 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 处之后),数组的状态是什么?

Q11HARD AP MC 4.15 Insertion Sort — 2 Passes [1]

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 处的元素分别被插入到已排序的前缀里之后),数组的状态是什么?

Q12HARD AP MC 4.17 Binary Search Trace [1]

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 整数除法。)

Q13HARD AP MC 4.16 Recursion — exponential branching [1]

Consider 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。)

Q14HARD AP MC 4.16 Recursion — Halving [1]

Consider 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⌋ 的计数。)

Q15HARD AP MC 4.4 Array — rebind vs mutate in method [1]

Consider 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 本地引用,对原数组无影响。)