← All Units← 返回单元列表 ← Course Hub← 课程主页
A P  C O M P U T E R  S C I E N C E  A
Unit 2 · Selection and Iteration第 2 单元 · 选择与循环

Selection and Iteration选择与循环

AP-Style MC & Free-Response PracticeAP 风格选择题与自由回答题练习

MEDIUM HARD AP MC AP FRQ

Unit 2: Selection and Iteration第 2 单元:选择与循环CSA



Name:姓名:Date:日期:
MULTIPLE CHOICE15 questions · ~32 min at exam pace15 道题 · 考试节奏约 32 分钟

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 2.5 Short-Circuit && [1]

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

int x = 0;
if (x != 0 && 10 / x > 0) {
  System.out.println("yes");
} else {
  System.out.println("no");
}

What is the result of executing the code segment?运行此代码段的结果是什么?(注意 && 的短路求值 short-circuit evaluation。)

Q2MEDIUM AP MC 2.8 for Loop Count [1]

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

int count = 0;
for (int i = 5; i <= 20; i += 3) {
  count++;
}
System.out.println(count);

What is printed?输出是什么?

Q3MEDIUM AP MC 2.6 De Morgan's Law [1]

Which of the following expressions is logically equivalent to !(a < b || c >= d)?下列哪个表达式与 !(a < b || c >= d) 在逻辑上等价?(提示:使用德摩根定律 De Morgan's Law。)

Q4HARD AP MC 2.4 Nested if Trace [1]

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

int x = 15;
if (x > 10) {
  if (x % 2 == 0) {
    System.out.print("A");
  } else {
    System.out.print("B");
  }
} else if (x > 5) {
  System.out.print("C");
}
System.out.println("D");

What is printed?输出是什么?

Q5HARD AP MC 2.9 Digit Sum [1]

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

int n = 234;
int s = 0;
while (n > 0) {
  s += n % 10;
  n /= 10;
}
System.out.println(s);

What is printed?输出是什么?(提示:典型的"数字求和 digit sum"循环。)

Q6HARD AP MC 2.10 String Traverse — Case Trap [1]

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

String s = "ProgrAmming";
int count = 0;
for (int i = 0; i < s.length(); i++) {
  char c = s.charAt(i);
  if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
    count++;
  }
}
System.out.println(count);

What is printed?输出是什么?(注意:'a' 与 'A' 是不同字符,区分大小写 case-sensitive。)

Q7HARD AP MC 2.7 while — Flag-controlled exit + counter [1]

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

int n = 100;
int count = 0;
boolean stop = false;
while (n > 0 && !stop) {
  count++;
  if (count > 5) stop = true;
  n -= 10;
}
System.out.println(count + " " + n);

What is printed?输出是什么?(提示:标志控制退出 flag-controlled exit 与计数器一起更新。)

Q8HARD AP MC 2.11 Nested Loop Count [1]

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

int count = 0;
for (int i = 1; i <= 4; i++) {
  for (int j = 1; j <= i; j++) {
    count++;
  }
}
System.out.println(count);

What is printed?输出是什么?(嵌套循环 nested loop 的三角形计数。)

Q9HARD AP MC 2.9 Reverse Digits [1]

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

int n = 1234;
int rev = 0;
while (n > 0) {
  rev = rev * 10 + n % 10;
  n /= 10;
}
System.out.println(rev);

What is printed?输出是什么?(典型的"数字反转 reverse digits"循环。)

Q10HARD AP MC 2.7 while — OR + guarded decrements [1]

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

int a = 5;
int b = 3;
while (a > 0 || b > 0) {
  if (a > 0) a--;
  if (b > 0) b--;
}
System.out.println(a + " " + b);

What is printed?输出是什么?(注意条件 OR 与"带条件自减 guarded decrement"如何防止越界。)

Q11HARD AP MC 2.7 while Threshold [1]

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

int n = 0;
int p = 1;
while (p < 1000) {
  p *= 2;
  n++;
}
System.out.println(n);

What is printed?输出是什么?(找出使 2^n ≥ 1000 的最小 n,即典型的"阈值循环 threshold loop"。)

Q12HARD AP MC 2.8 for — Integer-division step (log₂ counter) [1]

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

int n = 100;
int count = 0;
for (int i = n; i > 1; i = i / 2) {
  count++;
}
System.out.println(count);

What is printed?输出是什么?(每次 i 整除以 2,相当于一个 log₂ 计数器。)

Q13HARD AP MC 2.4 if-else-if chain vs independent ifs [1]

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

int x = 7;
String result = "";
if (x > 0) result += "P";
if (x % 2 == 0) result += "E";
else if (x > 5) result += "B";
if (x < 10 && x > 3) result += "M";
System.out.println(result);

What is printed?输出是什么?(注意 if-else-if 链与多个独立 if 的区别。)

Q14HARD AP MC 2.11 Nested loop — modular pair counting [1]

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

int total = 0;
for (int i = 1; i <= 6; i++) {
  for (int j = i; j <= 6; j++) {
    if ((i + j) % 3 == 0) total++;
  }
}
System.out.println(total);

What is printed?输出是什么?(注意 j 从 i 开始,所以是"对模计数 modular pair counting",每对只计一次。)

Q15HARD AP MC 2.7 while — Conditional shrink (halve/decrement) [1]

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

int n = 13;
int count = 0;
while (n > 1) {
  if (n % 2 == 0) n /= 2;
  else n--;
  count++;
}
System.out.println(count);

What is printed?输出是什么?(偶则减半、奇则减一的"条件性缩减 conditional shrink"。)

FREE RESPONSE1 question · ~25 min at exam pace1 道题 · 考试节奏约 25 分钟

Free-Response Question自由回答题(Free Response)

Write all program segments in Java. You may call any accessible method of a class defined in this question, including a method you were asked to write yourself. Unless stated otherwise, assume that parameters are not null and that methods are called only when their preconditions are satisfied. On the exam this is Question 1: Methods and Control Structures, worth 7 points across two parts.所有程序段均用 Java 编写。你可以调用本题所定义的类中任何可访问的方法,包括题目要求你自己编写的方法。除非另有说明,假定各参数不为 null,且方法仅在其前置条件(precondition)满足时被调用。在正式考试中,本题对应第 1 题:方法与控制结构(Methods and Control Structures),分两小题,共 7 分。

FRQ 1HARD METHODS & CONTROL STRUCTURES 2.3 / 2.5 / 2.8 / 2.9 [7 pts]

This question involves a greenhouse vent controller. The Greenhouse class records the temperature inside a greenhouse once per hour, for hours 0 through 23, decides how far to open the vents during each hour, and labels each vent it controls. You will write two methods of the Greenhouse class.本题涉及一个温室通风控制器。Greenhouse 类每小时记录一次温室内的温度(对应第 0 至 23 小时),据此决定每小时通风口开启的档位,并为其所控制的每个通风口标注标签。你需要编写 Greenhouse 类中的两个方法。

public class Greenhouse
{
    /**
     * Returns the temperature, in degrees Celsius, recorded inside the
     * greenhouse during hour h of the day.
     * Precondition: 0 <= h <= 23
     */
    public static int tempAt(int h)
    { /* implementation not shown */ }

    /**
     * Returns the vent level to be used during hour h, as described
     * in part (a).
     * Precondition: 0 <= h <= 23
     */
    public static int ventLevel(int h)
    { /* to be implemented in part (a) */ }

    /**
     * Returns the display name contained in the vent label label,
     * as described in part (b).
     */
    public static String ventName(String label)
    { /* to be implemented in part (b) */ }

    /* There may be instance variables, constructors, and methods
       that are not shown. */
}
public class Greenhouse
{
    /**
     * 返回当天第 h 小时温室内记录到的温度(单位:摄氏度)。
     * 前置条件 Precondition: 0 <= h <= 23
     */
    public static int tempAt(int h)
    { /* 实现未给出 */ }

    /**
     * 返回第 h 小时应使用的通风档位,具体规则见 (a) 小题。
     * 前置条件 Precondition: 0 <= h <= 23
     */
    public static int ventLevel(int h)
    { /* 在 (a) 小题中实现 */ }

    /**
     * 返回通风口标签 label 中所含的显示名称,
     * 具体规则见 (b) 小题。
     */
    public static String ventName(String label)
    { /* 在 (b) 小题中实现 */ }

    /* 可能还存在未列出的实例变量、构造方法与方法。 */
}
(a)4 pts分 Write the ventLevel method. The method returns the vent level for hour h, determined only by the temperature recorded during that hour, as follows.编写 ventLevel 方法。该方法返回第 h 小时的通风档位,档位仅由该小时记录到的温度决定,规则如下。
  • • below 18 degrees → level 0低于 18 度 → 档位 0
  • • 18 through 23 degrees → level 118 至 23 度 → 档位 1
  • • 24 through 29 degrees → level 224 至 29 度 → 档位 2
  • • 30 degrees or above → level 330 度及以上 → 档位 3

For example, if tempAt(6) returns 23, then ventLevel(6) returns 1; if tempAt(6) returns 24, then ventLevel(6) returns 2. Temperatures may be negative.例如,若 tempAt(6) 返回 23,则 ventLevel(6) 返回 1;若 tempAt(6) 返回 24,则 ventLevel(6) 返回 2。温度可能为负数。

(b)3 pts分 Write the ventName method. Every vent is identified by a label, which is the vent’s display name followed by a mode suffix. The mode suffix always begins with the character #. The method returns the display name — the portion of label that comes before the first #. If label contains no # at all, the entire label is the display name and is returned unchanged.编写 ventName 方法。每个通风口都由一个标签(label)标识,标签由通风口的显示名称与其后的模式后缀组成。模式后缀总是以字符 # 开头。该方法返回其中的显示名称 — 即 label 中位于第一个 # 之前的那一部分。若 label 中完全不含 #,则整个标签即为显示名称,原样返回。
Call调用 Returns返回值 Why原因
ventName("north-bench#auto")"north-bench"the characters before the first #第一个 # 之前的字符
ventName("ridge#manual#2")"ridge"only the first # marks the suffix只有第一个 # 标志后缀的开始
ventName("ridge")"ridge"no #, so the whole label is the name不含 #,整个标签即为名称
ventName("#auto")""the # is first, so the name is empty# 位于开头,故名称为空字符串

A label may contain more than one #; only the first one marks the start of the mode suffix. The returned string may be empty, but it is never null.标签中可能含有多个 #,但只有第一个标志模式后缀的开始。返回的字符串可能为空字符串,但绝不会是 null。