Exam : 1Z0-851
Java Standard Edition 6 Programmer Certified Professional Exam
以下分析全都是我自己分析或者参考网上的,定有疏漏,还请大家对我的分析提出质疑。
QUESTION 167
Given:
1. import java.util.*;
2. public class WrappedString {
3. private String s;
4. public WrappedString(String s) { this.s = s; }
5. public static void main(String[] args) {
6. HashSet<Object> hs = new HashSet<Object>();
7. WrappedString ws1 = new WrappedString("aardvark");
8. WrappedString ws2 = new WrappedString("aardvark");
9. String s1 = new String("aardvark");
10. String s2 = new String("aardvark");
11. hs.add(ws1); hs.add(ws2); hs.add(s1); hs.add(s2);
12. System.out.println(hs.size()); } }
What is the result?
A. 0
B. 1
C. 2
D. 3
E. 4
F. Compilation fails.
G. An exception is thrown at runtime.
Answer: D
WrappedString 的hashCode()是继承自Object类,而String的hashCode()时重写过的仅与字符串的内容有关。ws1 和ws2存储地址不同,所以hashCode()的返回值不同,而s1和s2字符串的内容相同,哈希值相同。
Given:
1. import java.util.*;
2. public class WrappedString {
3. private String s;
4. public WrappedString(String s) { this.s = s; }
5. public static void main(String[] args) {
6. HashSet<Object> hs = new HashSet<Object>();
7. WrappedString ws1 = new WrappedString("aardvark");
8. WrappedString ws2 = new WrappedString("aardvark");
9. String s1 = new String("aardvark");
10. String s2 = new String("aardvark");
11. hs.add(ws1); hs.add(ws2); hs.add(s1); hs.add(s2);
12. System.out.println(hs.size()); } }
What is the result?
A. 0
B. 1
C. 2
D. 3
E. 4
F. Compilation fails.
G. An exception is thrown at runtime.
Answer: D
WrappedString 的hashCode()是继承自Object类,而String的hashCode()时重写过的仅与字符串的内容有关。ws1 和ws2存储地址不同,所以hashCode()的返回值不同,而s1和s2字符串的内容相同,哈希值相同。
QUESTION 168
Given a class whose instances, when found in a collection of objects, are sorted by using the compareTo()
method, which two statements are true? (Choose two.)
A. The class implements java.lang.Comparable.
B. The class implements java.util.Comparator.
C. The interface used to implement sorting allows this class to define only one sort sequence.
D. The interface used to implement sorting allows this class to define many different sort sequences.
Answer: AC
Comparable接口仅有一个compareTo()函数~~谨记
QUESTION 169
Given:
1. import java.util.*;
2. public class Example {
3. public static void main(String[] args) {
4. // insert code here
5. set.add(new Integer(2));
6. set.add(new Integer(1));
7. System.out.println(set);
8. }
9. }
Which code, inserted at line 4, guarantees that this program will output [1, 2]?
A. Set set = new TreeSet();
B. Set set = new HashSet();
C. Set set = new SortedSet();
D. List set = new SortedList();
E. Set set = new LinkedHashSet();
Answer: A
必须是排好序的集合,所以TreeSet合适。
QUESTION 169
Given:
1. import java.util.*;
2. public class Example {
3. public static void main(String[] args) {
4. // insert code here
5. set.add(new Integer(2));
6. set.add(new Integer(1));
7. System.out.println(set);
8. }
9. }
Which code, inserted at line 4, guarantees that this program will output [1, 2]?
A. Set set = new TreeSet();
B. Set set = new HashSet();
C. Set set = new SortedSet();
D. List set = new SortedList();
E. Set set = new LinkedHashSet();
Answer: A
必须是排好序的集合,所以TreeSet合适。
而HashSet应该也可以的,Integer类型的hashCode()函数返回的是相应的int类型的值。如果就只有一个答案的话TreeSet是最好的选项。
SortedSet是一个interface,java里面没有SortedList类。
QUESTION 171
DRAG DROP
Click the Task button.
Answer:
多态性~~a和b的getName()都是A类的方法,对这个方法来说看不到B中的name变量。
QUESTION 177
Given:
1. class TestException extends Exception { }
2. class A {
3. public String sayHello(String name) throws TestException {
4. if(name == null) throw new TestException();
5. return "Hello " + name;
6. }
7. }
8. public class TestA {
9. public static void main(String[] args) {
10. new A().sayHello("Aiko");
11. }
12. }
Which statement is true?
A. Compilation succeeds.
B. Class A does not compile.
C. The method declared on line 9 cannot be modified to throw TestException.
D. TestA compiles if line 10 is enclosed in a try/catch block that catches TestException.
Answer: D
第十行 new A().sayHello("Aiko");会抛出一个TestException(),所以要么throws TestException。,要么try{} catch(TestException e){}
QUESTION 178
Given:
11. public static void main(String[] args) {
12. for (int i = 0; i <= 10; i++) {
13. if (i > 6) break;
14. }
15. System.out.println(i);
16. }
What is the result?
A. 6
B. 7
C. 10
D. 11
E. Compilation fails.
F. An exception is thrown at runtime.
Answer: E
i的作用域
QUESTION 179
Given:
3. public class Breaker {
4. static String o = "";
5. public static void main(String[] args) {
6. z:
7. o = o + 2;
8. for(int x = 3; x < 8; x++) {
9. if(x==4) break;
10. if(x==6) break z;
11. o = o + x;
12. }
13. System.out.println(o);
14. }
15. }
What is the result?
A. 23
B. 234
C. 235
D. 2345
E. 2357
F. 23457
G. Compilation fails.
Answer: G
断点的label只能出现在for循环的前一句或者while循环的前一句~~
QUESTION 180
Given:
5. class A {
6. void foo() throws Exception { throw new Exception(); }
7. }
8. class SubB2 extends A {
9. void foo() { System.out.println("B "); }
10. }
11. class Tester {
12. public static void main(String[] args) {
13. A a = new SubB2();
14. a.foo();
15. }
16. }
What is the result?
A. B
B. B, followed by an Exception.
C. Compilation fails due to an error on line 9.
D. Compilation fails due to an error on line 14.
E. An Exception is thrown with no other output.
Answer: D
编译器会报错:Unhandled exception type Exception
必须按照以下格式来:
try {
a.foo();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
或者throws一个Exception。这种多态性在编译的时候是完全按照 父类的方法来的,即在compiler的时候检查foo()方法是否在 A类中,如果在那么就按照父类的要求抛出异常或者捕获异常,在runtime的时候再调用SubB2 的foo()方法。如果子类的方法允许抛出一个父类没有的异常,那么就没有异常处理器来处理此异常,所以,不允许子类方法抛出父类没有的异常。
QUESTION 182
Given:
1. public class Mule {
2. public static void main(String[] args) {
3. boolean assert = true;
4. if(assert) {
5. System.out.println("assert is true");
6. }
7. }
8. }
Which command-line invocations will compile?
A. javac Mule.java
B. javac -source 1.3 Mule.java
C. javac -source 1.4 Mule.java
D. javac -source 1.5 Mule.java
Answer: B
断言引入是在JDK SE 1.4中,所以在之前assert不被当作保留字。
QUESTION 183
Given:
11. static void test() {
12. try {
13. String x = null;
14. System.out.print(x.toString() + " ");
15. }
16. finally { System.out.print("finally "); }
17. }
18. public static void main(String[] args) {
19. try { test(); }
20. catch (Exception ex) { System.out.print("exception "); }
21. }
What is the result?
A. null
B. finally
C. null finally
D. Compilation fails.
E. finally exception
Answer: E
x.toString() 会抛出一个NulPointerException
QUESTION 184
Given:
1. public class Boxer1{
2. Integer i;
3. int x;
4. public Boxer1(int y) {
5. x = i+y;
6. System.out.println(x);
7. }
8. public static void main(String[] args) {
9. new Boxer1(new Integer(4));
10. }
11. }
What is the result?
A. The value "4" is printed at the command line.
B. Compilation fails because of an error in line 5.
C. Compilation fails because of an error in line 9.
D. A NullPointerException occurs at runtime.
E. A NumberFormatException occurs at runtime.
F. An IllegalStateException occurs at runtime.
Answer: D
问题出在执行第五行的时候。
QUESTION 185
Which two code fragments are most likely to cause a StackOverflowError? (Choose two.)
A. int []x = {1,2,3,4,5};
for(int y = 0; y < 6; y++)
System.out.println(x[y]);
B. static int[] x = {7,6,5,4};
static { x[1] = 8;
x[4] = 3; }
C. for(int y = 10; y < 10; y++)
doStuff(y);
D. void doOne(int x) { doTwo(x); }
void doTwo(int y) { doThree(y); }
void doThree(int z) { doTwo(z); }
E. for(int x = 0; x < 1000000000; x++)
doStuff(x);
F. void counter(int i) { counter(++i); }
Answer: DF
不断迭代就能StackOverflowError
QUESTION 186
Given:
11. static void test() throws RuntimeException {
12. try {
13. System.out.print("test ");
14. throw new RuntimeException();
15. }
16. catch (Exception ex) { System.out.print("exception "); }
17. }
18. public static void main(String[] args) {
19. try { test(); }
20. catch (RuntimeException ex) { System.out.print("runtime "); }
21. System.out.print("end ");
22. }
What is the result?
A. test end
B. Compilation fails.
C. test runtime end
D. test exception end
E. A Throwable is thrown by main at runtime.
Answer: D
第14行抛出的RuntimeException被16行处理了,就没20行什么事了。
QUESTION 187
Given:
11. public static void main(String[] args) {
12. Integer i = new Integer(1) + new Integer(2);
13. switch(i) {
14. case 3: System.out.println("three"); break;
15. default: System.out.println("other"); break;
16. }
17. }
What is the result?
A. three
B. other
C. An exception is thrown at runtime.
D. Compilation fails because of an error on line 12.
E. Compilation fails because of an error on line 13.
F. Compilation fails because of an error on line 15.
Answer: A
QUESTION 188
Given:
21. class Money {
22. private String country = "Canada";
23. public String getC() { return country; }
24. }
25. class Yen extends Money {
26. public String getC() { return super.country; }
27. }
28. public class Euro extends Money {
29. public String getC(int x) { return super.getC(); }
30. public static void main(String[] args) {
31. System.out.print(new Yen().getC() + " " + new Euro().getC());
32. }
33. }
What is the result?
A. Canada
B. null Canada
C. Canada null
D. Canada Canada
E. Compilation fails due to an error on line 26.
F. Compilation fails due to an error on line 29.
Answer: E
super加点只能调用父类的非私有方法~~
QUESTION 189
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
QUESTION 190
Which three statements are true? (Choose three.)
A. A final method in class X can be abstract if and only if X is abstract.
B. A protected method in class X can be overridden by any subclass of X.
C. A private static method can be called only within other static methods in class X.
D. A non-static public final method in class X can be overridden in any subclass of X.
E. A public static method in class X can be called by a subclass of X without explicitly referencing the
class X.
F. A method with the same signature as a private final method in class X can be implemented in a
subclass of X.
G. A protected method in class X can be overridden by a subclass of X only if the subclass is in the same
package as X.
Answer: BEF
A:final方法不可是抽象方法。
C:Class X中的非静态的方法也可以哦
D:final方法不可重写哦
F:正确,因为private final方法不会被子类继承,所以在子类中可以有一个相同签名的方法。 注意这里不同于重写override
G:proteted的话不是一个包照样可以
QUESTION 191
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接口没有y()方法。
QUESTION 192
Given:
1. package test;
2.
3. class Target {
4. public String name = "hello";
5. }
What can directly access and change the value of the variable name?
A. any class
B. only the Target class
C. any class in the test package
D. any class that extends Target
Answer: C
Target 是默认修饰符修饰,包级可见性~~
QUESTION 193
Click the Exhibit button. What two must the programmer do to correct the compilation errors? (Choose
two.)
QUESTION 191
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接口没有y()方法。
QUESTION 192
Given:
1. package test;
2.
3. class Target {
4. public String name = "hello";
5. }
What can directly access and change the value of the variable name?
A. any class
B. only the Target class
C. any class in the test package
D. any class that extends Target
Answer: C
Target 是默认修饰符修饰,包级可见性~~
QUESTION 193
Click the Exhibit button. What two must the programmer do to correct the compilation errors? (Choose
two.)
A. insert a call to this() in the Car constructor
B. insert a call to this() in the MeGo constructor
C. insert a call to super() in the MeGo constructor
D. insert a call to super(vin) in the MeGo constructor
E. change the wheelCount variable in Car to protected
F. change line 3 in the MeGo class to super.wheelCount = 3;
Answer: DE
MeGo会调用Car无参数构造函数~~又是这个问题
wheelCount必须被MeGo类可见才是~~
QUESTION 194
A team of programmers is involved in reviewing a proposed design for a new utility class. After some
discussion, they realize that the current design allows other classes to access methods in the utility class
that should be accessible only to methods within the utility class itself. What design issue has the team
discovered?
A. Tight coupling
B. Low cohesion
C. High cohesion
D. Loose coupling
E. Weak encapsulation
F. Strong encapsulation
Answer: E
弱封装,没有好好把这个类封装起来~~
QUESTION 194
A team of programmers is involved in reviewing a proposed design for a new utility class. After some
discussion, they realize that the current design allows other classes to access methods in the utility class
that should be accessible only to methods within the utility class itself. What design issue has the team
discovered?
A. Tight coupling
B. Low cohesion
C. High cohesion
D. Loose coupling
E. Weak encapsulation
F. Strong encapsulation
Answer: E
弱封装,没有好好把这个类封装起来~~
QUESTION 195
Given:
5. class Thingy { Meter m = new Meter(); }
6. class Component { void go() { System.out.print("c"); } }
7. class Meter extends Component { void go() { System.out.print("m"); } }
8.
9. class DeluxeThingy extends Thingy {
10. public static void main(String[] args) {
11. DeluxeThingy dt = new DeluxeThingy();
12. dt.m.go();
13. Thingy t = new DeluxeThingy();
14. t.m.go();
15. }
16. }
Which two are true? (Choose two.)
A. The output is mm.
B. The output is mc.
C. Component is-a Meter.
D. Component has-a Meter.
E. DeluxeThingy is-a Component.
F. DeluxeThingy has-a Component.
Answer: AF
注意dt.m还有t.m都是Meter类的实例~~
QUESTION 196
Given:
10. interface Jumper { public void jump(); } ...
20. class Animal {} ...
30. class Dog extends Animal {
31. Tail tail; 32. } ...
40. class Beagle extends Dog implements Jumper{
41. public void jump() {}
42. } ...
50. class Cat implements Jumper{
51. public void jump() {}
52. }
Which three are true? (Choose three.)
A. Cat is-a Animal
B. Cat is-a Jumper
C. Dog is-a Animal
D. Dog is-a Jumper
E. Cat has-a Animal
F. Beagle has-a Tail
G. Beagle has-a Jumper
Answer: BCF
QUESTION 197
Click the Exhibit button. What is the result?
A. Value is: 8
B. Compilation fails.
C. Value is: 12
D. Value is: -12
E. The code runs with no output.
F. An exception is thrown at runtime.
Answer: A
:
QUESTION 198
Given a valid DateFormat object named df, and
16. Date d = new Date(0L);
17. String ds = "December 15, 2004";
18. // insert code here What updates d's value with the date represented by ds?
A. 18. d = df.parse(ds);
B. 18. d = df.getDate(ds);
C.
QUESTION 198
Given a valid DateFormat object named df, and
16. Date d = new Date(0L);
17. String ds = "December 15, 2004";
18. // insert code here What updates d's value with the date represented by ds?
A. 18. d = df.parse(ds);
B. 18. d = df.getDate(ds);
C.
18. try {
19. d = df.parse(ds);
20. } catch(ParseException e) { };
D.
19. d = df.parse(ds);
20. } catch(ParseException e) { };
D.
18. try {
19. d = df.getDate(ds);
20. } catch(ParseException e) { };
Answer: C
parse函数用来Parses text from the beginning of the given string to produce a date.
19. d = df.getDate(ds);
20. } catch(ParseException e) { };
Answer: C
parse函数用来Parses text from the beginning of the given string to produce a date.
没有getDate函数~~
QUESTION 199
Which two scenarios are NOT safe to replace a StringBuffer object with a StringBuilder object? (Choose
two.)
A. When using versions of Java technology earlier than 5.0.
B. When sharing a StringBuffer among multiple threads.
C. When using the java.io class StringBufferInputStream.
D. When you plan to reuse the StringBuffer to build more than one string.
Answer: AB
StringBuffer 是线程安全的,而Stringbuilder不是。
StringBuffer是在JDK1.0加入的,而StringBuilder是在JDK 1.5时候加入的~~