SCJP/OCJP(1z0-851) 模拟题分析(二)

来源:互联网 发布:矩阵分析 袁永新尚答案 编辑:程序博客网 时间:2024/04/28 16:31

Exam : 1z0-851

Sun/Oracle Certificated Java Programmer

Java Standard Edition 6 Programmer Certified Professional Exam

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


QUESTION NO: 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
Explanation:考察java对象的多态性,首先清楚接口可以继承接口,379行是没有编译错误的,ABC错误,

对于DEF:D e = new E()D e = (D)(new E()) 一样,都是对象父类e实例成子类 E,所以其引用的方法为第9行的bMethod().

注意:子类一定属于父类,就好比苹果一定属于水果, 由子类向父类转换,不需要强制类型转换(不写强制转化不会错),

父类不一定只有一个子类,就好比水果包括 苹果,梨,橘子..., 所以由父类向子类需要强制类型转换。


QUESTION NO: 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.)
24
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: A,C
Explanation: has-a 关系:包含关系(有一个),is-a 关系:继承关系(是一个),use-a关系:关联关系(用一个) ,此关系不仅试用JAVA语言

子类继承父类后,拥有父类的对象(has-a),所以答案选AC

追记:关于子类父类的继承请参照另一篇博文

LinkJava:子类是否只继承父类的非私有变量和方法?


QUESTION NO: 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
Explanation:软件工程要求高内聚,低耦合。即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 NO: 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
Explanation:

QUESTION NO: 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
Explanation:声明为父类,实例为子类,父类调用的addFive()方法为子类(Bar)的方法(该方法重写了父类的addFive方法)

关于调用的变量a,由于是向上转型,父类只可以看到自己及其自己可见属性,即a为父类(Foo)的a的值3


QUESTION NO: 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
Explanation:引用类型进行强转时,必须存在继承关系。第31行进行了强制类型转换(向下转型),animal实例成Dog,声明Cat时候将annimal强转,Dog和Cat不存在继承关系,出现类型转换错误


QUESTION NO: 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: C,D
Explanation:参照: http://blog.csdn.net/dietime1943/article/details/52882855


QUESTION NO: 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
Explanation: main函数内先打印foobar(6,7行),第8行source打印Base的常量FOO值为foo,第9行打印Sub内的FOO值为bar,第10行向下转型为打印父类FOO的值即为foo


QUESTION NO: 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
Explanation:关于引用变量base的类 那个陈述是正确的? 考察作用域修饰符,变量base所属class InnerTriangle中,修饰符为默认defalt类型,也就是早期的frendly类型,作用域为包可见,所以答案C正确。


QUESTION NO: 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: A,B,E,F
Explanation: 子类继承父类不能降低重写方法的访问权限,public > protected > default > private


QUESTION NO: 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: D,F
Explanation:会引起内存溢出的选项(死循环)


QUESTION NO: 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
Explanation:父类没有处理异常,子类在第14行抛出异常。


QUESTION NO: 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
Explanation:

使用断言的规则:
1、不要使用断言验证公共方法的参数。
2可以使用断言验证私有方法的参数。
3不要使用断言验证命令行参数
4在公共方法内,可以使用断言检查从不会发生的情况
5不要使用可能产生副作用的断言,也就是断言表达式应该使程序保持在进入它之前的状态。

对于第12行,assert不能检测公共方法的参数,违反了原则4
15行,assert肯定会发生,不会回到之前的状态,违反了原则5
18行符合原则2。


QUESTION NO: 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
Explanation:"continue"跳出本次循环,“break”中断当前循环,「o = o + x;」该表达式为拼接字符串。结果为24


QUESTION NO: 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
Explanation:第15行正确编译错误,正确写法为「else if


QUESTION NO: 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
Explanation:在第17行的if判断中没有大括号,所以if的作用域仅为18行,第19行的“2”一定打印,第20行「(b2 = true)」该赋值表达式返回true,所以第21行的“3”也打印,答案D


QUESTION NO: 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
Explanation:参看下面文章 Link:Error和Exception的区别


QUESTION NO: 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
Explanation:数组a在11行进行了声明,但没有进行初始化操作,答案C。其他异常A为堆栈(内存)溢出异常;B为错误调用异常;D为数组越界异常。


QUESTION NO: 49
Click the Exhibit button. Given:


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: B,E
Explanation:在出现异常的所在行之后的source不会继续执行,直接返回到调用方法所在行(line27),答案BE


QUESTION NO: 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
Explanation:在第15行出现编译错误,局部变量“i”的作用域仅为在for循环内。


QUESTION NO: 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
Explanation:JAVA继承中的原则之一,重写方法的异常不能大于(少于)父类的被重写方法的异常。(注意该规则与重写方法的修饰符刚好相反)。


QUESTION NO: 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
Explanation:第3行实例新线程,并且实现了Runnalbe接口,实现该接口必须重写run方法(第4-6行),且第6行start方法启动了该线程。考察线程实现的方法,没有错误选C。


QUESTION NO: 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
Explanation:第6行「t.join();」是等待线程 t 结束,Started和0123的出现先后是不一定的,Complete一定在最后出现。


QUESTION NO: 54
Click the Exhibit button. What is the output if the main() method is run?


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

Explanation:同Q53,Exhibit中的第20行join();方法等待线程结束,最后才执行x = x - 1;运行最后结果为9。


QUESTION NO: 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: A,D
Explanation:表达式要避免对x的存取竞争,破坏掉x的递增逻辑,解答观点:所有对x的存取叙述要放在一个同步区块内,也就是先将第12行对x的输出操作放进foo()方法中(答案A),再把foo()方法内部叙述(对x的所有存取叙述)以synchronized(this) 同步化(答案D)。


QUESTION NO: 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: D,E
Explanation:线程启动后,输出线程内部的“run”和main函数中的"End of method."前后不一定,线程内部的“run”一定是先打印,再抛出"Problem"异常。


QUESTION NO: 57 DRAG DROP
Click the Task button.

Answer:


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


QUESTION NO: 58 DRAG DROP
Click the Task button.


Answer:

Explanation:


QUESTION NO: 59 DRAG DROP
Click the Task button.


Answer:


Explanation:


QUESTION NO: 60 DRAG DROP
Click the Task button.


Answer:




Flag Counter

0 0