码迷,mamicode.com
首页 > 其他好文 > 详细

OCJP(1Z0-851) 模拟题分析(二)

时间:2014-07-31 13:05:06      阅读:444      评论:0      收藏:0      [点我收藏+]

标签:des   http   java   使用   os   strong   io   for   

Exam : 1Z0-851

Java Standard Edition 6 Programmer Certified Professional Exam

以下分析全都是我自己分析或者参考网上的,定有疏漏,还请大家对我的分析提出质疑。

QUESTION 31

Given:
1. interface A { public void aMethod(); }
2. interface B { public void bMethod(); }
3. interface C extends A,B { public void cMethod(); }
4. class D implements B {
5. public void bMethod(){}
6. }
7. class E extends D implements C {
8. public void aMethod(){}
9. public void bMethod(){}
10. public void cMethod(){}
11. }
What is the result?
A. Compilation fails because of an error in line 3.
B. Compilation fails because of an error in line 7.
C. Compilation fails because of an error in line 9.
D. If you define D e = new E(), then e.bMethod() invokes the version of bMethod() defined in Line 5.
E. If you define D e = (D)(new E()), then e.bMethod() invokes the version of bMethod() defined in Line 5.
F. If you define D e = (D)(new E()), then e.bMethod() invokes the version of bMethod() defined in Line 9.
Answer: F

考察多态性,new E()产生一个E类型的对象,是D的子类,D e = new E()和D e = (D)(new E())完全一样,e引用E类型的对象,那么e.bMethod()引用的就是第9行的bMethod()

 

QUESTION 32
Given that: Gadget has-a Sprocket and Gadget has-a Spring and Gadget is-a Widget and Widget has-a
Sprocket Which two code fragments represent these relationships? (Choose two.)
A. class Widget { Sprocket s; }
class Gadget extends Widget { Spring s; }
B. class Widget { }
class Gadget extends Widget { Spring s1; Sprocket s2; }
C. class Widget { Sprocket s1; Spring s2; }
class Gadget extends Widget { }
D. class Gadget { Spring s; }
class Widget extends Gadget{ Sprocket s; }
E. class Gadget { }
class Widget extends Gadget{ Sprocket s1; Spring s2; }
F. class Gadget { Spring s1; Sprocket s2; }
class Widget extends Gadget{ }
Answer: AC

has-a 关系:包含关系

is-a 关系:继承关系

use-a关系:关联关系

Gadget has-a Sprocket and Gadget has-a Spring 指的是Gadget 类中包含Sprocket 和Spring 对象;

Gadget is-a Widget指的是Gadget 继承自 Widget;

Widget has-a Sprocket 指的是Widget类包含Sprocket对象。

 

QUESTION 33
A company that makes Computer Assisted Design (CAD) software has, within its application, some utility
classes that are used to perform 3D rendering tasks. The company‘s chief scientist has just improved the
performance of one of the utility classes‘ key rendering algorithms, and has assigned a programmer to
replace the old algorithm with the new algorithm. When the programmer begins researching the utility
classes, she is happy to discover that the algorithm to be replaced exists in only one class. The
programmer reviews that class‘s API, and replaces the old algorithm with the new algorithm, being careful
that her changes adhere strictly to the class‘s API. Once testing has begun, the programmer discovers that
other classes that use the class she changed are no longer working properly. What design flaw is most
likely the cause of these new bugs?
A. Inheritance
B. Tight coupling
C. Low cohesion
D. High cohesion
E. Loose coupling
F. Object immutability
Answer: B

软件工程要求高内聚,低耦合。即High/Strong/Tight cohesion,Low/Weak/Loose coupling。但是这里说the programmer discovers that
other classes that use the class she changed are no longer working properly. 所以肯定是没有遵循这个原则,改变了一小部分,其他没动过的不能用了,说明耦合性太高了。

 

QUESTION 34
Which Man class properly represents the relationship "Man has a best friend who is a Dog"?
A. class Man extends Dog { }
B. class Man implements Dog { }
C. class Man { private BestFriend dog; }
D. class Man { private Dog bestFriend; }
E. class Man { private Dog<bestFriend>; }
F. class Man { private BestFriend<dog>; }
Answer: D

 

 

QUESTION 35
Given:
31. class Foo {
32. public int a = 3;
33. public void addFive() { a += 5; System.out.print("f "); }
34. }
35. class Bar extends Foo {
36. public int a = 8;
37. public void addFive() { this.a += 5; System.out.print("b " ); }
38. } Invoked with: Foo f = new Bar(); f.addFive(); System.out.println(f.a);
What is the result?
A. b 3
B. b 8
C. b 13
D. f 3
E. f 8
F. f 13
G. Compilation fails.
H. An exception is thrown at runtime.
Answer: A

多态性,f引用的是Bar类型的, f.addFive(); 调用的是Bar类中的。方法具有多态性,但是域没有多态性,f是Foo类型的,所以f.a是Foo中的。

 

QUESTION 36
Given:
11. class Animal { public String noise() { return "peep"; } }
12. class Dog extends Animal {
13. public String noise() { return "bark"; }
14. }
15. class Cat extends Animal {
16. public String noise() { return "meow"; }
17. } ...
30. Animal animal = new Dog();
31. Cat cat = (Cat)animal;
32. System.out.println(cat.noise());
What is the result?
A. peep
B. bark
C. meow
D. Compilation fails.
E. An exception is thrown at runtime.
Answer: E

类型转换错误,ClassCastException。

 

QUESTION 37
Given:
1. class Super {
2. private int a;
3. protected Super(int a) { this.a = a; }
4. } ...
11. class Sub extends Super {
12. public Sub(int a) { super(a); }
13. public Sub() { this.a = 5; }
14. }
Which two, independently, will allow Sub to compile? (Choose two.)
A. Change line 2 to:
public int a;
B. Change line 2 to:
protected int a;
C. Change line 13 to:
public Sub() { this(5); }
D. Change line 13 to:
public Sub() { super(5); }
E. Change line 13 to:
public Sub() { super(a); }
Answer: CD

问题出在public Sub() { this.a = 5; },Sub类没有域a。所以可以改成public Sub() { this(5); }调用Sub类的其它的构造函数,也可以public Sub() { super(5); }调用父类的构造函数。

 

QUESTION 38
Given:
1. public class Base {
2. public static final String FOO = "foo";
3. public static void main(String[] args) {
4. Base b = new Base();
5. Sub s = new Sub();
6. System.out.print(Base.FOO);
7. System.out.print(Sub.FOO);
8. System.out.print(b.FOO);
9. System.out.print(s.FOO);
10. System.out.print(((Base)s).FOO);
11. } }
12. class Sub extends Base {public static final String FOO="bar";} What is the result?
A. foofoofoofoofoo
B. foobarfoobarbar
C. foobarfoofoofoo
D. foobarfoobarfoo
E. barbarbarbarbar
F. foofoofoobarbar
G. foofoofoobarfoo
Answer: D

域不具有多态性!对域来说,对象是什么类型就是什么。

 

QUESTION 39
Given:
1. package geometry;
2. public class Hypotenuse {
3. public InnerTriangle it = new InnerTriangle();
4. class InnerTriangle {
5. public int base;
6. public int height;
7. }
8. }
Which statement is true about the class of an object that can reference the variable base?
A. It can be any class.
B. No class has access to base.
C. The class must belong to the geometry package.
D. The class must be a subclass of the class Hypotenuse.
Answer: C

考察修饰符的作用域,base变量在类InnerTriangle里面,而类InnerTriangle的作用域是包可见性。

 

QUESTION 40
Given:
2. public class Hi {
3. void m1() { }
4. protected void m2 () { }
5. }
6. class Lois extends Hi {
7. // insert code here
8. }
Which four code fragments, inserted independently at line 7, will compile? (Choose four.)
A. public void m1() { }
B. protected void m1() { }
C. private void m1() { }
D. void m2() { }
E. public void m2() { }
F. protected void m2() { }
G. private void m2() { }
Answer: ABEF

考察重写,子类中的重写方法的作用域不可以reduce(减小)。public>protected>default>private

 

QUESTION 41
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 42
Given:
11. class A {
12. public void process() { System.out.print("A,"); }
13. class B extends A {
14. public void process() throws IOException {
15. super.process();
16. System.out.print("B,");
17. throw new IOException();
18. }
19. public static void main(String[] args) {
20. try { new B().process(); }
21. catch (IOException e) { System.out.println("Exception"); }
22. }
What is the result?
A. Exception
B. A,B,Exception
C. Compilation fails because of an error in line 20.
D. Compilation fails because of an error in line 14.
E. A NullPointerException is thrown at runtime.
Answer: D

第十四行抛出了一个父类没有的异常,错误。子类不可以抛出父类没有的异常。

 

QUESTION 43
Given:
11. public void go(int x) {
12. assert (x > 0);
13. switch(x) {
14. case 2: ;
15. default: assert false;
16. }
17. }
18. private void go2(int x) { assert (x < 0); }
Which statement is true?
A. All of the assert statements are used appropriately.
B. Only the assert statement on line 12 is used appropriately.
C. Only the assert statement on line 15 is used appropriately.
D. Only the assert statement on line 18 is used appropriately.
E. Only the assert statements on lines 12 and 15 are used appropriately.
F. Only the assert statements on lines 12 and 18 are used appropriately.
G. Only the assert statements on lines 15 and 18 are used appropriately.

Answer: D

 

使用断言的规则:

1.不要使用断言验证公共方法的参数。
2.可以使用断言验证私有方法的参数。
3.不要使用断言验证命令行参数
4.在公共方法内,可以使用断言检查从不会发生的情况

5.不要使用可能产生副作用的断言,也就是断言表达式应该使程序保持在进入它之前的状态。

 

 

 

对于第12行,assert不能检测公共方法的参数,违反了原则4

15行,assert肯定会发生,不会回到之前的状态,违反了原则5

18行符合原则2。

 

 

QUESTION 44
Given:
1. public class Breaker2 {
2. static String o = "";
3. public static void main(String[] args) {
4. z:
5. for(int x = 2; x < 7; x++) {
6. if(x==3) continue;
7. if(x==5) break z;
8. o = o + x;
9. }
10. System.out.println(o);
11. }
12. }
What is the result?
A. 2
B. 24
C. 234
D. 246
E. 2346
F. Compilation fails.
Answer: B

考察break。

 

QUESTION 45
Given:
11. public static void main(String[] args) {
12. String str = "null";
13. if (str == null) {
14. System.out.println("null");
15. } else (str.length() == 0) {
16. System.out.println("zero");
17. } else {
18. System.out.println("some");
19. }
20. }
What is the result?
A. null
B. zero
C. some
D. Compilation fails.
E. An exception is thrown at runtime.
Answer: D

第十五行应该是else if

 

QUESTION 46
Given:
11. public class Test {
12. public static void main(String [] args) {
13. int x = 5;
14. boolean b1 = true;
15. boolean b2 = false;
16.
17. if ((x == 4) && !b2 )
18. System.out.print("1 ");
19. System.out.print("2 ");
20. if ((b2 = true) && b1 )
21. System.out.print("3 ");
22. }
23. }
What is the result?
A. 2
B. 3
C. 1 2
D. 2 3
E. 1 2 3
F. Compilation fails.
G. An exception is thrown at runtime.
Answer: D

b2 = true返回一个boolean类型的true

 

QUESTION 47
Given:
11. static void test() throws Error {
12. if (true) throw new AssertionError();
13. System.out.print("test ");
14. }
15. public static void main(String[] args) {
16. try { test(); }
17. catch (Exception ex) { System.out.print("exception "); }
18. System.out.print("end ");
19. }
What is the result?
A. end
B. Compilation fails.
C. exception end
D. exception test end
E. A Throwable is thrown by main.
F. An Exception is thrown by main.
Answer: E

如果抛出一个异常,就不会执行下面的内容,而是返回调用产生异常的方法那里去。Error类和Exception类同继承自Throwable类,main函数不能处理Error类异常,所以一个Thorwable被main抛出。

 

QUESTION 48
Given:
10. public class Foo {
11. static int[] a;
12. static { a[0]=2; }
13. public static void main( String[] args ) {}
14. }
Which exception or error will be thrown when a programmer attempts to run this code?
A. java.lang.StackOverflowError
B. java.lang.IllegalStateException
C. java.lang.ExceptionInInitializerError
D. java.lang.ArrayIndexOutOfBoundsException
Answer: C

a还没有new呢,初始化错误。

 

QUESTION 49

Click the Exhibit button. Given:

bubuko.com,布布扣

25. try {
26. A a = new A();
27. a.method1();
28. } catch (Exception e) {
29. System.out.print("an error occurred");
30. }
Which two statements are true if a NullPointerException is thrown on line 3 of class C? (Choose two.)
A. The application will crash.
B. The code on line 29 will be executed.
C. The code on line 5 of class A will execute.
D. The code on line 5 of class B will execute.
E. The exception will be propagated back to line 27.
Answer: BE

如果一个方法内部出现异常,那么不会执行该方法余下的部分,将会返回调用该方法的那里(27line)。

 

QUESTION 50
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 的作用域仅在for循环内部。

 

QUESTION 51
Given:
11. static class A {
12. void process() throws Exception { throw new Exception(); }
13. }
14. static class B extends A {
15. void process() { System.out.println("B"); }
16. }
17. public static void main(String[] args) {
18. new B().process();
19. }
What is the result?
A. B
B. The code runs with no output.
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 18.
Answer: A

B类重写了A类的process方法,并没有抛出A类没有的异常。

 

QUESTION 52
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 53
Given:
1. public class TestOne implements Runnable {
2. public static void main (String[] args) throws Exception {
3. Thread t = new Thread(new TestOne());
4. t.start();
5. System.out.print("Started");
6. t.join();
7. System.out.print("Complete");
8. }
9. public void run() {
10. for (int i = 0; i < 4; i++) {
11. System.out.print(i);
12. }
13. }
14. }
What can be a result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. The code executes and prints "StartedComplete".
D. The code executes and prints "StartedComplete0123".
E. The code executes and prints "Started0123Complete".
Answer: E

t.join()函数是等待进程 t 结束,Started和0123的出现先后是不一定的,Complete一定在最后出现。

 

QUESTION 54

Click the Exhibit button. What is the output if the main() method is run?

bubuko.com,布布扣

A. 4
B. 5
C. 8
D. 9
E. Compilation fails.
F. An exception is thrown at runtime.
G. It is impossible to determine for certain.
Answer: D

关键还是join,start()运行run完毕之后才执行x = x - 1

 

QUESTION 55
Given:
1. public class TestFive {
2. private int x;
3. public void foo() {
4. int current = x;
5. x = current + 1;
6. }
7. public void go() {
8. for(int i = 0; i < 5; i++) {
9. new Thread() {
10. public void run() {
11. foo();
12. System.out.print(x + ", ");
13. } }.start();
14. } }
Which two changes, taken together, would guarantee the output: 1, 2, 3, 4, 5, ? (Choose two.)
A. move the line 12 print statement into the foo() method
B. change line 7 to public synchronized void go() {
C. change the variable declaration on line 2 to private volatile int x;
D. wrap the code inside the foo() method with a synchronized( this ) block
E. wrap the for loop code inside the go() method with a synchronized block synchronized(this) { // for loop
code here }
Answer: AD

11. foo();和12. System.out.print(x + ", ");随时都可以被打断,所以他两个必须有原子性,且要求x每加一就输出一次,synchronized 修饰符修饰foo正好可以满足。

 

QUESTION 56
Given:
1. public class Threads2 implements Runnable {
2.
3. public void run() {
4. System.out.println("run.");
5. throw new RuntimeException("Problem");
6. }
7. public static void main(String[] args) {
8. Thread t = new Thread(new Threads2());
9. t.start();
10. System.out.println("End of method.");
11. }
12. }
Which two can be results? (Choose two.)
A. java.lang.RuntimeException: Problem
B. run.
java.lang.RuntimeException: Problem
C. End of method.
java.lang.RuntimeException: Problem
D. End of method.
run.
java.lang.RuntimeException: Problem
E. run.
java.lang.RuntimeException: Problem
End of method.
Answer: DE

子线程和主线程的速度不一样,System.out.println("run.");和System.out.println("End of method.");哪一个首先执行也不一定,但是必须是现输出run再有一个异常。异常输出后还要往下执行。

 

QUESTION 57
DRAG DROP
Click the Task button.
bubuko.com,布布扣
Answer:

Pi是浮点数%f,3.141593,而E是boolean类型的,只要不是false都是true。

OCJP(1Z0-851) 模拟题分析(二),布布扣,bubuko.com

OCJP(1Z0-851) 模拟题分析(二)

标签:des   http   java   使用   os   strong   io   for   

原文地址:http://www.cnblogs.com/husam/p/3880481.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!