AP-Style MC Practice QuestionsAP 风格选择题练习
Unit 3: Class Creation第 3 单元:类的创建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 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; 的作用是什么?
size to the instance variable size.将参数 size 的值赋给实例变量(instance variable) size。size to itself; the instance variable is unaffected.把参数 size 赋给它自己;实例变量不受影响。size that is discarded.创建一个名为 size 的新局部变量然后被丢弃。Which of the following statements about a static variable declared in a Java class is true?下列关于 Java 类中 static 变量的描述,哪一项是正确的?
null regardless of its declared type.无论声明类型为何,都会被自动初始化为 null。class-level variable)。Consider a Java class that declares no constructors of its own. Which of the following is true?假设某个 Java 类没有声明任何自定义的构造方法(constructor)。下列哪一项是正确的?
default constructor)。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; 之后 a 与 b 是别名 aliasing,指向同一个对象。)
5 1015 1020 1020 20Consider 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 中如何维持总和。)
30507080Consider 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.。)
07-1Consider 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 reference,mutate 通过引用就地修改对象 mutate via reference。)
1 21 9999 9999 2Consider 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 时才裁剪。)
15203035Consider 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,所有方法调用作用于同一个对象。)
0 22 02 24 4Consider 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,而不是字段值。)
true truefalse truetrue falsefalse falseConsider 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 同名实例变量。)
90100-100Consider 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?尝试编译并运行此类的结果是什么?
getCount() always returns 0.能编译,但 getCount() 永远返回 0。static context)中访问。NullPointerException on the call to getCount().运行时错误:调用 getCount() 时抛出 NullPointerException。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。)
p.name is now "Bob".能通过编译;p.name 现在为 "Bob"。name has private access in Person.编译错误:name 在 Person 中是私有访问(private access)。IllegalAccessException is thrown.运行时抛出 IllegalAccessException。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,对象引用传的是地址副本——可以通过它修改对象,但重新赋值给参数本身不影响调用方。)
5 5999 9995 999999 5Consider 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 仍然指向哪个对象。)
#1:alpha #2:beta #1:alpha#3:alpha #2:beta #3:alpha#3:alpha #2:beta #1:alpha#1:alpha #2:beta #3:alpha