A P  C O M P U T E R  S C I E N C E  A
Unit 1 · Using Objects and Methods第 1 单元 · 使用对象与方法

Using Objects and Methods使用对象与方法

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

MEDIUM HARD AP MC

Unit 1: Using Objects and Methods1 单元:使用对象与方法CSA



Name:姓名:Date:日期:

Draft v3 — 15 AP-style MCQs at exam level (3 MED + 12 HARD); code-trace heavy with realistic distractors. HARDs upgraded: multi-step chains, aliased reassignment timing, swap-without-temp, Math.pow edge inputs, compareTo case/lex. Assumes top-score target. No calculator. Java Quick Reference assumed available.v3 草稿 —— 15 道 AP 风格选择题(MC),考试难度(3 道 MED + 12 道 HARD);偏重代码追踪(code trace),干扰项贴近真题。HARD 题目升级方向:多步链式调用、别名重新赋值的时序(aliased reassignment)、不使用临时变量的交换(swap without temp)、Math.pow 边界输入、compareTo 大小写与字典序。面向冲击 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 1.3 / 1.4 Integer Ops [1]

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

int x = 17;
int y = 4;
System.out.println(x / y + x % y);

What is printed as a result of executing the code segment?运行此代码段后输出的结果是什么?

Q2MEDIUM AP MC 1.x Math.random() [1]

Which of the following expressions returns a random even integer between 2 and 10 inclusive (i.e., one of 2, 4, 6, 8, or 10)?下列哪个表达式返回 2 到 10(含端点,inclusive)之间的一个随机偶数(即 2、4、6、8 或 10 之一)?

Q3MEDIUM AP MC 1.x String — Identity vs Equality [1]

Consider the following declarations.考虑以下变量声明(declarations)。

String a = "Java";
String b = "Ja" + "va";
String c = new String("Java");

Which row in the table correctly evaluates the listed expressions?下面哪一行正确给出了所列表达式的求值结果?

Q4HARD AP MC 1.5 Casting Placement [1]

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

int a = 10;
int b = 4;
double r = (double) a / b + a / b;
System.out.println(r);

What is printed?输出是什么?

Q5HARD AP MC 1.x String — 4-step Chain [1]

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

String s = "ABCDEFG";
String t = s.substring(2);
String u = t.substring(t.indexOf("E"));
String v = u.substring(0, u.length() - 1);
System.out.println(v + u.substring(u.length() - 1));

What is printed?输出是什么?

Q6HARD AP MC 1.x String — Aliased Reassignment Timing [1]

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

String a = "go";
String b = a;
a = a + a;
b += a;
System.out.println(b);

What is printed?输出是什么?

Q7HARD AP MC 1.5 Integer Overflow [1]

Consider the following expression.考虑以下表达式。

(Integer.MAX_VALUE + 1) / 2

What is the value of this expression in Java?在 Java 中,此表达式的值是多少?(提示:考虑整数溢出 integer overflow。)

Q8HARD AP MC 1.5 Type Promotion + Cast [1]

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

int x = 7;
double y = 2.0;
double z = (int) (x / y * 2);
System.out.println(z);

What is printed?输出是什么?

Q9HARD AP MC 1.x String — Self-referential length arithmetic [1]

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

String s = "BAOBAB";
int i = s.indexOf("OB");
String a = s.substring(i);
String b = s.substring(i, i + a.length() - 2);
System.out.println(a.length() + b.length());

What is printed?输出是什么?

Q10HARD AP MC 1.3 Negative Division & Modulus [1]

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

int x = -17;
int y = 5;
System.out.println((x / y) + " " + (x % y));

What is printed?输出是什么?(注意 Java 中负数的整除与取模 modulus 的方向。)

Q11HARD AP MC 1.x String — Swap without a temp [1]

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

String a = "AB";
String b = "CD";
a = a + b;
b = a.substring(0, a.length() - b.length());
a = a.substring(b.length());
System.out.println(a + b);

What is printed?输出是什么?

Q12HARD AP MC 1.6 Math.pow — Edge inputs + Cast [1]

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

int x = (int) Math.pow(-3, 3)
      + (int) Math.pow(-3, -1)
      + (int) Math.pow(0, 0);
System.out.println(x);

What is printed?输出是什么?(注意:(int) 强制类型转换会向零截断 truncate toward zero。)

Q13HARD AP MC 1.x String — Concatenation Order [1]

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

String s = "" + 1 + 2 + 3;
String t = 1 + 2 + 3 + "";
System.out.println(s.length() + t.length());

What is printed?输出是什么?(注意 + 操作符的左结合 left-associative 求值顺序。)

Q14HARD AP MC 1.x Wrapper — Autoboxing [1]

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

Integer a = 5;
Integer b = 7;
int c = a + b;
System.out.println(c);

What is printed (or what happens)?输出是什么(或者会发生什么)?

Q15HARD AP MC 1.x String — compareTo, case & lex [1]

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

String a = "apple";
String b = "Apple";
String c = "apricot";
int p = a.compareTo(b);
int q = a.compareTo(c);
System.out.println((p > 0) + " " + (q > 0));

What is printed?输出是什么?(提示:compareTo 比较的是字典序 lexicographic order,且区分大小写 case-sensitive。)