首页 > 代码库 > OCJP(1Z0-851) 模拟题分析(三)
OCJP(1Z0-851) 模拟题分析(三)
Exam : 1Z0-851
Java Standard Edition 6 Programmer Certified Professional Exam
以下分析全都是我自己分析或者参考网上的,定有疏漏,还请大家对我的分析提出质疑。
QUESTION 61
Given:
1. public class TestString1 {
2. public static void main(String[] args) {
3. String str = "420";
4. str += 42;
5. System.out.print(str);
6. }
7. }
What is the output?
A. 42
B. 420
C. 462
D. 42042
E. Compilation fails.
F. An exception is thrown at runtime.
Answer: D
42会自动类型转换成String类型。
QUESTION 62
Given:
12. Date date = new Date();
13. df.setLocale(Locale.ITALY);
14. String s = df.format(date);
The variable df is an object of type DateFormat that has been initialized in line 11. What is the result if this
code is run on December 14, 2000?
A. The value of s is 14-dic-2000.
B. The value of s is Dec 14, 2000.
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 13.
Answer: D
设置地点不是这样设置,如下:
Locale.setDefault(Locale.US);//设置为美国地区
QUESTION 63
Given:
1. public class KungFu {
2. public static void main(String[] args) {
3. Integer x = 400;
4. Integer y = x;
5. x++;
6. StringBuilder sb1 = new StringBuilder("123");
7. StringBuilder sb2 = sb1;
8. sb1.append("5");
9. System.out.println((x==y) + " " + (sb1==sb2));
10. }
11. }
What is the result?
A. true true
B. false true
C. true false
D. false false
E. Compilation fails.
F. An exception is thrown at runtime.
Answer: B
x与y引用相同的对象400,但是x++即x = x + 1; 使x引用401,而y依然引用400,输出false。
QUESTION 64
Given that the current directory is empty, and that the user has read and write privileges to the current
directory, and the following:
1. import java.io.*;
2. public class Maker {
3. public static void main(String[] args) {
4. File dir = new File("dir");
5. File f = new File(dir, "f");
6. }
7. }
Which statement is true?
A. Compilation fails.
B. Nothing is added to the file system.
C. Only a new file is created on the file system.
D. Only a new directory is created on the file system.
E. Both a new file and a new directory are created on the file system.
Answer: B
没调用creat函数,不会是文件系统产生新的文件。
QUESTION 65
Given:
12. String csv = "Sue,5,true,3";
13. Scanner scanner = new Scanner( csv );
14. scanner.useDelimiter(",");
15. int age = scanner.nextInt();
What is the result?
A. Compilation fails.
B. After line 15, the value of age is 5.
C. After line 15, the value of age is 3.
D. An exception is thrown at runtime.
Answer: D
读取的第一个是String类型的,不是int。
QUESTION 66
Given that t1 is a reference to a live thread, which is true?
A. The Thread.sleep() method can take t1 as an argument.
B. The Object.notify() method can take t1 as an argument.
C. The Thread.yield() method can take t1 as an argument.
D. The Thread.setPriority() method can take t1 as an argument.
E. The Object.notify() method arbitrarily chooses which thread to notify.
Answer: E
notify() ,yield() 函数没有参数,setPriority()有一个参数int newPriority,sleep()有一个参数long millis,notify()函数的功能是Wakes up a single thread that is waiting on this object‘s monitor.
QUESTION 67
Given that Triangle implements Runnable, and:
31. void go() throws Exception {
32. Thread t = new Thread(new Triangle());
33. t.start();
34. for(int x = 1; x < 100000; x++) {
35. //insert code here
36. if(x%100 == 0) System.out.print("g");
37. } }
38. public void run() {
39. try {
40. for(int x = 1; x < 100000; x++) {
41. // insert the same code here
42. if(x%100 == 0) System.out.print("t");
43. }
44. } catch (Exception e) { }
45. }
Which two statements, inserted independently at both lines 35 and 41, tend to allow both threads to
temporarily pause and allow the other thread to execute? (Choose two.)
A. Thread.wait();
B. Thread.join();
C. Thread.yield();
D. Thread.sleep(1);
E. Thread.notify();
Answer: CD
Thread.yield(); 以及Thread.sleep(1);是静态方法,可以让现在正在运行的进程暂停,运行其他进程。
QUESTION 68
Given:
1. public class Threads3 implements Runnable {
2. public void run() {
3. System.out.print("running");
4. }
5. public static void main(String[] args) {
6. Thread t = new Thread(new Threads3());
7. t.run();
8. t.run();
9. t.start();
10. }
11. }
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. The code executes and prints "running".
D. The code executes and prints "runningrunning".
E. The code executes and prints "runningrunningrunning".
Answer: E
run()函数仅在主线程中运行,而start()函数是开辟一个新的线程。running出现三次。
QUESTION 69
Given:
1. public class Threads5 {
2. public static void main (String[] args) {
3. new Thread(new Runnable() {
4. public void run() {
5. System.out.print("bar");
6. }}).start();
7. }
8. }
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. The code executes normally and prints "bar".
D. The code executes normally, but nothing prints.
Answer: C
QUESTION 70
Given:
11. public class PingPong implements Runnable {
12. synchronized void hit(long n) {
13. for(int i = 1; i < 3; i++)
14. System.out.print(n + "-" + i + " ");
15. }
16. public static void main(String[] args) {
17. new Thread(new PingPong()).start();
18. new Thread(new PingPong()).start();
19. }
20. public void run() {
21. hit(Thread.currentThread().getId());
22. }
23. }
Which two statements are true? (Choose two.)
A. The output could be 8-1 7-2 8-2 7-1
B. The output could be 7-1 7-2 8-1 6-1
C. The output could be 8-1 7-1 7-2 8-2
D. The output could be 8-1 8-2 7-1 7-2
Answer: CD
synchronized 仅仅是同一对象等等hit函数互斥,第17,18行是对两个不同的PingPong对象开辟的新线程。
QUESTION 71
Given:
10. interface A { void x(); }
11. class B implements A { public void x() {} public void y() {} }
12. class C extends B { public void x() {} } And:
20. java.util.List<A> list = new java.util.ArrayList<A>();
21. list.add(new B());
22. list.add(new C());
23. for (A a : list) {
24. a.x();
25. a.y();
26. }
What is the result?
A. The code runs with no output.
B. An exception is thrown at runtime.
C. Compilation fails because of an error in line 20.
D. Compilation fails because of an error in line 21.
E. Compilation fails because of an error in line 23.
F. Compilation fails because of an error in line 25.
Answer: F
实现了A接口的类的对象a仅仅有x()方法,没有y()方法!
QUESTION 72
Given:
11. class Mammal { }
12.
13. class Raccoon extends Mammal {
14. Mammal m = new Mammal();
15. }
16.
17. class BabyRaccoon extends Mammal { }
Which four statements are true? (Choose four.)
A. Raccoon is-a Mammal.
B. Raccoon has-a Mammal.
C. BabyRaccoon is-a Mammal.
D. BabyRaccoon is-a Raccoon.
E. BabyRaccoon has-a Mammal.
F. BabyRaccoon is-a BabyRaccoon.
Answer: ABCF
三种关系,is-a,has-a,use-a
QUESTION 73
Given:
10: public class Hello {
11: String title;
12: int value;
13: public Hello() {
14: title += " World";
15: }
16: public Hello(int value) {
17: this.value = http://www.mamicode.com/value;
18: title = "Hello";
19: Hello();
20: }
21: } and:
30: Hello c = new Hello(5);
31: System.out.println(c.title);
What is the result?
A. Hello
B. Hello World
C. Compilation fails.
D. Hello World 5
E. The code runs with no output.
F. An exception is thrown at runtime.
Answer: C
第19行,调用本类的其他构造函数应该是:this();
QUESTION 74
Given:
1. class ClassA {
2. public int numberOfInstances;
3. protected ClassA(int numberOfInstances) {
4. this.numberOfInstances = numberOfInstances;
5. }
6. }
7. public class ExtendedA extends ClassA {
8. private ExtendedA(int numberOfInstances) {
9. super(numberOfInstances);
10. }
11. public static void main(String[] args) {
12. ExtendedA ext = new ExtendedA(420);
13. System.out.print(ext.numberOfInstances);
14. }
15. }
Which statement is true?
A. 420 is the output.
B. An exception is thrown at runtime.
C. All constructors must be declared public.
D. Constructors CANNOT use the private modifier.
E. Constructors CANNOT use the protected modifier.
Answer: A
QUESTION 75
Given:
1. public class Target {
2. private int i = 0;
3. public int addOne(){
4. return ++i;
5. }
6. } And:
1. public class Client {
2. public static void main(String[] args){
3. System.out.println(new Target().addOne());
4. }
5. }
Which change can you make to Target without affecting Client?
A. Line 4 of class Target can be changed to return i++;
B. Line 2 of class Target can be changed to private int i = 1;
C. Line 3 of class Target can be changed to private int addOne(){
D. Line 2 of class Target can be changed to private Integer i = 0;
Answer: D
要更改Target类而不影响Client类,只有D
QUESTION 76
Given:
1. public class Blip {
2. protected int blipvert(int x) { return 0; }
3. }
4. class Vert extends Blip {
5. // insert code here
6. }
Which five methods, inserted independently at line 5, will compile? (Choose five.)
A. public int blipvert(int x) { return 0; }
B. private int blipvert(int x) { return 0; }
C. private int blipvert(long x) { return 0; }
D. protected long blipvert(int x) { return 0; }
E. protected int blipvert(long x) { return 0; }
F. protected long blipvert(long x) { return 0; }
G. protected long blipvert(int x, int y) { return 0; }
Answer: A,C,E,F,G
重写和重载。A是重写,C,E,F,G是重载。
QUESTION 77
Given:
1. class Pizza {
2. java.util.ArrayList toppings;
3. public final void addTopping(String topping) {
4. toppings.add(topping);
5. }
6. }
7. public class PepperoniPizza extends Pizza {
8. public void addTopping(String topping) {
9. System.out.println("Cannot add Toppings");
10. }
11. public static void main(String[] args) {
12. Pizza pizza = new PepperoniPizza();
13. pizza.addTopping("Mushrooms");
14. }
15. }
What is the result?
A. Compilation fails.
B. Cannot add Toppings
C. The code runs with no output.
D. A NullPointerException is thrown in Line 4.
Answer: A
public final void addTopping(String topping) 不允许重写。
QUESTION 78
Given:
11. class ClassA {}
12. class ClassB extends ClassA {}
13. class ClassC extends ClassA {} and:
21. ClassA p0 = new ClassA();
22. ClassB p1 = new ClassB();
23. ClassC p2 = new ClassC();
24. ClassA p3 = new ClassB();
25. ClassA p4 = new ClassC();
Which three are valid? (Choose three.)
A. p0 = p1;
B. p1 = p2;
C. p2 = p4;
D. p2 = (ClassC)p1;
E. p1 = (ClassB)p3;
F. p2 = (ClassC)p4;
Answer: AEF
p2/p4引用的是C类型的对象,p1/p3引用的是B类型的对象,p0引用的是A类型的对象,而B和C是A的子类,根据语言规范,可以把子类对象赋给父类,A正确,B错误。
p4在编译器看来是A类型的,不能再没有类型转换的情况下将p2 = p4;,C错误。
EF类型转换正确!
QUESTION 79
Given two files, GrizzlyBear.java and Salmon.java:
1. package animals.mammals;
2.
3. public class GrizzlyBear extends Bear {
4. void hunt() {
5. Salmon s = findSalmon();
6. s.consume();
7. }
8. }
1. package animals.fish;
2.
3. public class Salmon extends Fish {
4. public void consume() { /* do stuff */ }
5. }
If both classes are in the correct directories for their packages, and the Mammal class correctly defines the
findSalmon() method, which change allows this code to compile?
A. add import animals.mammals.*; at line 2 in Salmon.java
B. add import animals.fish.*; at line 2 in GrizzlyBear.java
C. add import animals.fish.Salmon.*; at line 2 in GrizzlyBear.java
D. add import animals.mammals.GrizzlyBear.*; at line 2 in Salmon.java
Answer: B
GrizzlyBear类利用了Salmon类,所以必须在GrizzlyBear类所在的文件中把Salmon类包含进来。 import animals.fish.*或者 import animals.fish.Samlon;
QUESTION 80
Given:
1. package com.company.application;
2.
3. public class MainClass {
4. public static void main(String[] args) {}
5. }
And MainClass exists in the /apps/com/company/application directory. Assume the CLASSPATH
environment variable is set to "." (current directory). Which two java commands entered at the command
line will run MainClass? (Choose two.)
A. java MainClass if run from the /apps directory
B. java com.company.application.MainClass if run from the /apps directory
C. java -classpath /apps com.company.application.MainClass if run from any directory
D. java -classpath . MainClass if run from the /apps/com/company/application directory
E. java -classpath /apps/com/company/application:. MainClass if run from the /apps directory
F. java com.company.application.MainClass if run from the /apps/com/company/application directory
Answer: BC
QUESTION 81
Click the Exhibit button. Which three code fragments, added individually at line 29, produce the output
100? (Choose three.)
A. n = 100;
B. i.setX( 100 );
C. o.getY().setX( 100 );
D. i = new Inner(); i.setX( 100 );
E. o.setY( i ); i = new Inner(); i.setX( 100 );
F. i = new Inner(); i.setX( 100 ); o.setY( i );
Answer: BCF
o.getY()返回的对象和i引用的对象一样~~
QUESTION 82
A developer is creating a class Book, that needs to access class Paper. The Paper class is deployed in a
JAR named myLib.jar. Which three, taken independently, will allow the developer to use the Paper class
while compiling the Book class? (Choose three.)
A. The JAR file is located at $JAVA_HOME/jre/classes/myLib.jar.
B. The JAR file is located at $JAVA_HOME/jre/lib/ext/myLib.jar..
C. The JAR file is located at /foo/myLib.jar and a classpath environment variable is set that includes /foo/
myLib.jar/Paper.class.
D. The JAR file is located at /foo/myLib.jar and a classpath environment variable is set that includes /foo/
myLib.jar.
E. The JAR file is located at /foo/myLib.jar and the Book class is compiled using javac -cp /foo/myLib.jar/
Paper Book.java.
F. The JAR file is located at /foo/myLib.jar and the Book class is compiled using javac -d /foo/myLib.jar
Book.java
G. The JAR file is located at /foo/myLib.jar and the Book class is compiled using javac -classpath /foo/
myLib.jar Book.java
Answer: BDG
QUESTION 83
Given:
11. interface DeclareStuff {
12. public static final int EASY = 3;
13. void doStuff(int t); }
14. public class TestDeclare implements DeclareStuff {
15. public static void main(String [] args) {
16. int x = 5;
17. new TestDeclare().doStuff(++x);
18. }
19. void doStuff(int s) {
20. s += EASY + ++s;
21. System.out.println("s " + s);
22. }
23. }
What is the result?
A. s 14
B. s 16
C. s 10
D. Compilation fails.
E. An exception is thrown at runtime.
Answer: D
在interface中的void doStuff(int t);一定是public,而TestDeclare中实现的时候就变成了包可见性,reduce了可见性,所以编译要出错的。
QUESTION 84
Given:
11. public class Commander {
12. public static void main(String[] args) {
13. String myProp = /* insert code here */
14. System.out.println(myProp);
15. }
16. }
and the command line: java -Dprop.custom=gobstopper Commander Which two, placed on line 13, will
produce the output gobstopper? (Choose two.)
A. System.load("prop.custom");
B. System.getenv("prop.custom");
C. System.property("prop.custom");
D. System.getProperty("prop.custom");
E. System.getProperties().getProperty("prop.custom");
Answer: DE
命令行参数 -D<propertyName>=value
在虚拟机的系统属性中设置属性名/值对,运行在此虚拟机之上的应用程序可用System.getProperty(“propertyName”)得到value的值。
如果value中有空格,则需要用双引号将该值括起来,如-Dname=”space string”。
该参数通常用于设置系统级全局变量值,如配置文件路径,应为该属性在程序中任何地方都可访问。
static Properties getProperties() 功能:Determines the current system properties.
String getProperty(String key) 功能:Searches for the property with the specified key in this property list.
QUESTION 85
Given:
3. public class Spock {
4. public static void main(String[] args) {
5. Long tail = 2000L;
6. Long distance = 1999L;
7. Long story = 1000L;
8. if((tail > distance) ^ ((story * 2) == tail))
9. System.out.print("1");
10. if((distance + 1 != tail) ^ ((story * 2) == distance))
11. System.out.print("2");
12. }
13. }
What is the result?
A. 1
B. 2
C. 12
D. Compilation fails.
E. No output is produced.
F. An exception is thrown at runtime.
Answer: E
^ 是异或,第一次两边都是true,不会输出1,第二次两边都是false,不会输出2。
QUESTION 86
Given:
1. public class GC {
2. private Object o;
3. private void doSomethingElse(Object obj) { o = obj; }
4. public void doSomething() {
5. Object o = new Object();
6. doSomethingElse(o);
7. o = new Object();
8. doSomethingElse(null);
9. o = null;
10. }
11. }
When the doSomething method is called, after which line does the Object created in line 5 become
available for garbage collection?
A. Line 5
B. Line 6
C. Line 7
D. Line 8
E. Line 9
F. Line 10
Answer: D
第7行时,局部变量o引用了一个新的对象,但是全局变量o还在饮用者第五行创建的对象。
QUESTION 87
Click the Exhibit button. What is the result?
A. go in Goban
go in Sente
B. go in Sente
go in Goban
C. go in Sente
go in Goban
go in Goban
D. go in Goban
go in Sente
E. Compilation fails because of an error in line 17.
Answer: C
QUESTION 88
Given:
1. public class Plant {
2. private String name;
3. public Plant(String name) { this.name = name; }
4. public String getName() { return name; }
5. }
1. public class Tree extends Plant {
2. public void growFruit() { }
3. public void dropLeaves() { }
4. }
Which statement is true?
A. The code will compile without changes.
B. The code will compile if public Tree() { Plant(); } is added to the Tree class.
C. The code will compile if public Plant() { Tree(); } is added to the Plant class.
D. The code will compile if public Plant() { this("fern"); } is added to the Plant class.
E. The code will compile if public Plant() { Plant("fern"); } is added to the Plant class.
Answer: D
Tree 的构造器只有一个默认构造器,而这个默认构造器必须调用父类的无参数构造器,故Plant缺少一个无参数的构造器。
QUESTION 89
Click the Exhibit button.
Given:
25. A a = new A();
26. System.out.println(a.doit(4, 5));
What is the result?
A. Line 26 prints "a" to System.out.
B. Line 26 prints "b" to System.out.
C. An exception is thrown at line 26 at runtime.
D. Compilation of class A will fail due to an error in line 6.
Answer: A
可变长参数类型和另外的不可变长参数类型都符合要求,这种情况下,虚拟机会先抛开可变长参数的方法,查找有没有能直接匹配的方法,如果找到再运行,否则去查找可变长参数的方法。
QUESTION 90
Given:
11. public enum Title {
12. MR("Mr."), MRS("Mrs."), MS("Ms.");
13. private final String title;
14. private Title(String t) { title = t; }
15. public String format(String last, String first) {
16. return title + " " + first + " " + last;
17. }
18. }
19. public static void main(String[] args) {
20. System.out.println(Title.MR.format("Doe", "John"));
21. }
What is the result?
A. Mr. John Doe
B. An exception is thrown at runtime.
C. Compilation fails because of an error in line 12.
D. Compilation fails because of an error in line 15.
E. Compilation fails because of an error in line 20.
Answer: A
枚举常量,定义了一个构造函数Title(String t) 。
QUESTION 91
Given:
11. public interface A111 {
12. String s = "yo";
13. public void method1();
14. }
17. interface B { }
20. interface C extends A111, B {
21. public void method1();
22. public void method1(int x);
23. }
What is the result?
A. Compilation succeeds.
B. Compilation fails due to multiple errors.
C. Compilation fails due to an error only on line 20.
D. Compilation fails due to an error only on line 21.
E. Compilation fails due to an error only on line 22.
F. Compilation fails due to an error only on line 12.
Answer: A
第21行相当于重写了,没多大意义,但是不报错。
QUESTION 92
Given:
1. interface TestA { String toString(); }
2. public class Test {
3. public static void main(String[] args) {
4. System.out.println(new TestA() {
5. public String toString() { return "test"; }
6. });
7. }
8. }
What is the result?
A. test
B. null
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 1.
E. Compilation fails because of an error in line 4.
F. Compilation fails because of an error in line 5.
Answer: A
匿名内部类。返回一个TestA类型的对象,然后再调用toString函数输出。
QUESTION 93
Given:
11. class Alpha {
12. public void foo() { System.out.print("Afoo "); }
13. }
14. public class Beta extends Alpha {
15. public void foo() { System.out.print("Bfoo "); }
16. public static void main(String[] args) {
17. Alpha a = new Beta();
18. Beta b = (Beta)a;
19. a.foo();
20. b.foo();
21. }
22. }
What is the result?
A. Afoo Afoo
B. Afoo Bfoo
C. Bfoo Afoo
D. Bfoo Bfoo
E. Compilation fails.
F. An exception is thrown at runtime.
Answer: D
ab都是引用的Beta类的同一个对象。
QUESTION 94
Given:
10. abstract public class Employee {
11. protected abstract double getSalesAmount();
12. public double getCommision() {
13. return getSalesAmount() * 0.15;
14. }
15. }
16. class Sales extends Employee {
17. // insert method here
18. }
Which two methods, inserted independently at line 17, correctly complete the Sales class? (Choose two.)
A. double getSalesAmount() { return 1230.45; }
B. public double getSalesAmount() { return 1230.45; }
C. private double getSalesAmount() { return 1230.45; }
D. protected double getSalesAmount() { return 1230.45; }
Answer: BD
子类中实现abstract方法,父类中是protected,所以子类的 getSalesAmount() 访问修饰符必须是protected或是public。
QUESTION 95
Click the Exhibit button. What is the result?
A. 4321
B. 0000
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 18.
Answer: D
Person中没有默认的无参数的构造函数,所以Employee中的构造函数必须显式调用super(id);
QUESTION 96
Given:
3. import java.util.*;
4. public class Mapit {
5. public static void main(String[] args) {
6. Set<Integer> set = new HashSet<Integer>();
7. Integer i1 = 45;
8. Integer i2 = 46;
9. set.add(i1);
10. set.add(i1);
11. set.add(i2); System.out.print(set.size() + " ");
12. set.remove(i1); System.out.print(set.size() + " ");
13. i2 = 47;
14. set.remove(i2); System.out.print(set.size() + " ");
15. }
16. }
What is the result?
A. 2 1 0
B. 2 1 1
C. 3 2 1
D. 3 2 2
E. Compilation fails.
F. An exception is thrown at runtime.
Answer: B
HashSet不允许有重复,加两次和加一次一样。第14行remove会失败,因为哈希表中没有47。
QUESTION 97
Given:
1. public class Score implements Comparable<Score> {
2. private int wins, losses;
3. public Score(int w, int l) { wins = w; losses = l; }
4. public int getWins() { return wins; }
5. public int getLosses() { return losses; }
6. public String toString() {
7. return "<" + wins + "," + losses + ">";
8. }
9. // insert code here
10. }
Which method will complete this class?
A. public int compareTo(Object o){/*more code here*/}
B. public int compareTo(Score other){/*more code here*/}
C. public int compare(Score s1,Score s2){/*more code here*/}
D. public int compare(Object o1,Object o2){/*more code here*/}
Answer: B
Comparable接口只有一个方法,public int CompareTo(T other)
QUESTION 98
A programmer has an algorithm that requires a java.util.List that provides an efficient implementation of
add(0, object), but does NOT need to support quick random access. What supports these requirements?
A. java.util.Queue
B. java.util.ArrayList
C. java.util.LinearList
D. java.util.LinkedList
Answer: D
QUESTION 99
Given:
12. import java.util.*;
13. public class Explorer3 {
14. public static void main(String[] args) {
15. TreeSet<Integer> s = new TreeSet<Integer>();
16. TreeSet<Integer> subs = new TreeSet<Integer>();
17. for(int i = 606; i < 613; i++)
18. if(i%2 == 0) s.add(i);
19. subs = (TreeSet)s.subSet(608, true, 611, true);
20. subs.add(629);
21. System.out.println(s + " " + subs);
22. }
23. }
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. [608, 610, 612, 629] [608, 610]
D. [608, 610, 612, 629] [608, 610, 629]
E. [606, 608, 610, 612, 629] [608, 610]
F. [606, 608, 610, 612, 629] [608, 610, 629]
Answer: B
由于subs被限制为s的视图view,只能是在608~611之间。如果超出了,会抛出一个java.lang.IllegalArgumentException异常。
如果是下列程序:
- import java.util.TreeSet;
- public class Explorer3 {
- public static void main(String[] args) {
- TreeSet<Integer> s = new TreeSet<Integer>();
- TreeSet<Integer> subs = new TreeSet<Integer>();
- for(int i = 606; i < 613; i++)
- if(i%2 == 0) s.add(i);
- subs = (TreeSet)s.subSet(608, true, 611, true);
- subs.add(609);
- System.out.println(s + " " + subs);
- }
- }
则输出是:[606, 608, 609, 610, 612] [608, 609, 610]
而按照以下更改:
- import java.util.TreeSet;
- public class Explorer3 {
- public static void main(String[] args) {
- TreeSet<Integer> s = new TreeSet<Integer>();
- TreeSet<Integer> subs = new TreeSet<Integer>();
- for(int i = 606; i < 613; i++)
- if(i%2 == 0) s.add(i);
- subs = (TreeSet)s.subSet(608, true, 611, true);
- s.add(609);
- System.out.println(s + " " + subs);
- }
- }
输出依然是:[606, 608, 609, 610, 612] [608, 609, 610]
可以说subs是s的一部分,谁更改都会影响到另一个。
QUESTION 100
Given:
11. // insert code here
12. private N min, max;
13. public N getMin() { return min; }
14. public N getMax() { return max; }
15. public void add(N added) {
16. if (min == null || added.doubleValue() < min.doubleValue())
17. min = added;
18. if (max == null || added.doubleValue() > max.doubleValue()) 19. max = added;
20. }
21. }
Which two, inserted at line 11, will allow the code to compile? (Choose two.)
A. public class MinMax<?> {
B. public class MinMax<? extends Number> {
C. public class MinMax<N extends Object> {
D. public class MinMax<N extends Number> {
E. public class MinMax<? extends Object> {
F. public class MinMax<N extends Integer> {
Answer: DF