A P  C O M P U T E R  S C I E N C E  A
Unit 3 · Class Creation第 3 单元 · 类的创建

Class Creation类的创建

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

MEDIUM HARD AP MC

Unit 3: Class Creation3 单元:类的创建CSA



Name:姓名:Date:日期:

Draft v2 — 15 AP-style MCQs at exam level (3 MED + 12 HARD); class/constructor/method trace heavy with realistic distractors. HARDs upgraded: aliasing + compound assignment, static aggregate w/ mutator, reassign-vs-mutate ref param, mutator order w/ conditional cap, fluent interface, static seq + reassignment-through-aliasing. Assumes top-score target. No calculator. Java Quick Reference assumed available.v2 草稿 —— 15 道 AP 风格选择题(MC),考试难度(3 道 MED + 12 道 HARD);偏重类/构造方法/方法追踪(class / constructor / method),干扰项贴近真题。HARD 题目升级方向:别名(aliasing)+ 复合赋值(compound assignment)、static 聚合配合实例修改器(mutator)、引用参数的"重新赋值 vs 就地修改"、带条件上限的修改器调用顺序、链式接口(fluent interface)、static 自增序列 + 别名重新赋值。面向冲击 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 3.9 this Keyword [1]

Consider the following constructor.考虑以下构造方法(constructor)。

public class Box {
  private int size;
  public Box(int size) {
    this.size = size;
  }
}

What does the statement this.size = size; do?语句 this.size = size; 的作用是什么?

Q2MEDIUM AP MC 3.7 static Variables [1]

Which of the following statements about a static variable declared in a Java class is true?下列关于 Java 类中 static 变量的描述,哪一项是正确的?

Q3MEDIUM AP MC 3.4 Default Constructor [1]

Consider a Java class that declares no constructors of its own. Which of the following is true?假设某个 Java 类没有声明任何自定义的构造方法(constructor)。下列哪一项是正确的?

Q4HARD AP MC 3.6 Aliasing + compound assignment [1]

Consider the following class and code.考虑以下类与代码。

public class Box {
  public int n;
  public Box(int n) { this.n = n; }
}

// in main:
Box a = new Box(5);
Box b = a;
b.n = 10;
a.n += b.n;
System.out.println(a.n + " " + b.n);

What is printed?输出是什么?(注意 b = a; 之后 ab 是别名 aliasing,指向同一个对象。)

Q5HARD AP MC 3.7 static aggregate + instance mutator [1]

Consider the following class.考虑以下类。

public class Sensor {
  private static int total = 0;
  private int reading;
  public Sensor(int r) { reading = r; total += r; }
  public void update(int r) {
    total -= reading;
    reading = r;
    total += r;
  }
  public static int total() { return total; }
}

// in main:
Sensor s1 = new Sensor(10);
Sensor s2 = new Sensor(20);
s1.update(50);
s2.update(0);
System.out.println(Sensor.total());

What is printed?输出是什么?(注意 static 聚合变量与实例修改器 mutator 在每次 update 中如何维持总和。)

Q6HARD AP MC 3.8 Missing this. Trap [1]

Consider the following class.考虑以下类。

public class Point {
  private int x;
  public Point(int x) {
    x = x;        // note: no this.
  }
  public int getX() { return x; }
}

// in main:
Point p = new Point(7);
System.out.println(p.getX());

What is printed (or what happens)?输出是什么(或会发生什么)?(注意构造方法中没有写 this.。)

Q7HARD AP MC 3.6 Reassigning a ref param vs mutating [1]

Consider the following class and methods.考虑以下类与方法。

public class Box {
  public int n;
  public Box(int n) { this.n = n; }
}

public static void replace(Box b) {
  b = new Box(99);
}

public static void mutate(Box b) {
  b.n = 99;
}

// in main:
Box one = new Box(1);
Box two = new Box(2);
replace(one);
mutate(two);
System.out.println(one.n + " " + two.n);

What is printed?输出是什么?(关键区别:replace 重新赋值本地引用 reassign referencemutate 通过引用就地修改对象 mutate via reference。)

Q8HARD AP MC 3.5 Mutator order with conditional cap [1]

Consider the following class.考虑以下类。

public class Score {
  private int v;
  public Score(int v) { this.v = v; }
  public void add(int n) { v += n; }
  public void cap(int max) { if (v > max) v = max; }
  public int get() { return v; }
}

Score s = new Score(10);
s.add(20);
s.cap(15);
s.add(5);
s.cap(50);
System.out.println(s.get());

What is printed?输出是什么?(按顺序逐步执行每个修改器调用,注意 cap 仅当超过 max 时才裁剪。)

Q9HARD AP MC 3.5 Fluent interface (return this) [1]

Consider the following class.考虑以下类。

public class Counter {
  private int n;
  public Counter()       { n = 0; }
  public Counter inc()   { n++; return this; }
  public Counter dec()   { n--; return this; }
  public int peek()      { return n; }
}

Counter c = new Counter();
int x = c.inc().inc().inc().dec().peek();
System.out.println(x + " " + c.peek());

What is printed?输出是什么?(每个修改器 return this;,构成链式接口 fluent interface,所有方法调用作用于同一个对象。)

Q10HARD AP MC 3.6 Object Equality (==) [1]

Consider the following class and code.考虑以下类与代码。

public class Pt {
  public int x, y;
  public Pt(int x, int y) { this.x = x; this.y = y; }
}

Pt p = new Pt(1, 2);
Pt q = new Pt(1, 2);
Pt r = p;
System.out.println((p == q) + " " + (p == r));

What is printed?输出是什么?(提示:== 比较对象的引用地址 reference equality,而不是字段值。)

Q11HARD AP MC 3.8 Local Variable Shadows [1]

Consider the following class.考虑以下类。

public class Tank {
  private int level = 100;
  public void drain() {
    int level = 0;     // local declaration
    level -= 10;
  }
  public int getLevel() { return level; }
}

Tank t = new Tank();
t.drain();
System.out.println(t.getLevel());

What is printed?输出是什么?(注意 drain 中重新声明了局部 level,会"遮蔽" shadow 同名实例变量。)

Q12HARD AP MC 3.7 static Accessing Instance [1]

Consider the following class declaration.考虑以下类的声明。

public class Counter {
  private int count = 0;
  public static int getCount() {
    return count;
  }
}

What is the result of attempting to compile and run this class?尝试编译并运行此类的结果是什么?

Q13HARD AP MC 3.3 Encapsulation Access [1]

Consider the following class.考虑以下类。

public class Person {
  private String name;
  public Person(String name) { this.name = name; }
}

From a different class in the same package, a programmer writes:在同一个包中的另一个类里,某程序员写下:

Person p = new Person("Alice");
p.name = "Bob";   // line X

What happens at line X?在 line X 处会发生什么?(提示:private 字段的可见性 visibility。)

Q14HARD AP MC 3.6 Primitive vs Object Params [1]

Consider the following class and code.考虑以下类与代码。

public class Wrap {
  public int n;
  public Wrap(int n) { this.n = n; }
}

public static void modify(int x, Wrap w) {
  x   = 999;
  w.n = 999;
}

// in main:
int x = 5;
Wrap w = new Wrap(5);
modify(x, w);
System.out.println(x + " " + w.n);

What is printed?输出是什么?(提示:基本类型按值传递 pass by value,对象引用传的是地址副本——可以通过它修改对象,但重新赋值给参数本身不影响调用方。)

Q15HARD AP MC 3.7 static seq + aliasing + toString [1]

Consider the following class and code.考虑以下类与代码。

public class Tag {
  private static int seq = 0;
  private String label;
  private int id;
  public Tag(String label) {
    this.label = label;
    this.id = ++seq;
  }
  public String toString() { return "#" + id + ":" + label; }
}

// in main:
Tag a = new Tag("alpha");
Tag b = new Tag("beta");
Tag c = a;
a = new Tag("alpha");
System.out.println(a + " " + b + " " + c);

What is printed?输出是什么?(注意每个构造方法都会预增 ++seq,然后注意 c = a; 此时 a 仍然指向哪个对象。)