您的位置:首页 > 其它

ocjp 考试题之六

2021-01-11 22:35 204 查看

OCJP视频课堂,具体讲解:https://edu.csdn.net/course/detail/7811

QUESTION 134

Given: 
11. class Snoochy { 
12. Boochy booch; 
13. public Snoochy() { booch = new Boochy(this); } 
14. } 
15. 
16. class Boochy { 
17. Snoochy snooch; 
18. public Boochy(Snoochy s) { snooch = s; } 
19. } And the statements: 
21. public static void main(String[] args) { 
22. Snoochy snoog = new Snoochy(); 
23. snoog = null; 
24. // more code here 
25. } 
Which statement is true about the objects referenced by snoog, snooch, and booch immediately after line 
23 executes? 
A. None of these objects are eligible for garbage collection. 
B. Only the object referenced by booch is eligible for garbage collection. 
C. Only the object referenced by snoog is eligible for garbage collection. 
D. Only the object referenced by snooch is eligible for garbage collection. 
E. The objects referenced by snooch and booch are eligible for garbage collection. 
Answer: E

23行结束,snooch 和booch都没引用任何对象,可以被垃圾回收器回收。

QUESTION 135
Given:
3. public class Batman {
4. int squares = 81;
5. public static void main(String[] args) {
6. new Batman().go();
7. }
8. void go() {
9. incr(++squares);
10. System.out.println(squares);
11. }
12. void incr(int squares) { squares += 10; }
13. }
What is the result?
A. 81
B. 82
C. 91
D. 92
E. Compilation fails.
F. An exception is thrown at runtime.
Answer: B

一定要记得Java的参数传递全部都是引用~~不变哦~~

QUESTION 136 Given classes defined intwo different files:

1. package util;

2. public class BitUtils {

3. private static voidprocess(byte[] b) {} //注意这里的访问修饰符是private,在类外不能访问,所以选择F

4. }

1. package app;

2. public class SomeApp {

3. public static voidmain(String[] args) {

4. byte[] bytes = new byte[256];

5. // insert code here

6. }

7. }

What is required at line 5 in classSomeApp to use the process method of BitUtils?

A.   process(bytes);

B.   BitUtils.process(bytes);

C.   app.BitUtils.process(bytes);

D.   util.BitUtils.process(bytes);

E.   import util.BitUtils.*;process(bytes);

F.    SomeApp cannot use the processmethod in BitUtils.

Answer: F

Section: (none)

QUESTION 139 

Given the following directory structure: 

bigProject

 |--source

 | |--Utils.java

 |

 |--classes

 |-- 

And the following

command line invocation: javac -d classes source/Utils.java Assume the current directory is bigProject, 
what is the result? 
A. If the compile is successful, Utils.class is added to the source directory. 
B. The compiler returns an invalid flag error. 
C. If the compile is successful, Utils.class is added to the classes directory. 
D. If the compile is successful, Utils.class is added to the bigProject directory. 
Answer: C 

-d classes 是把生成的Utils.class文件放到classes目录中的Unix命令~~

QUESTION 140 Given:

3.     interface Fish { }

4.     class Perch implements Fish { }

5.     class Walleye extends Perch { }

6.     class Bluegill { }

7.     public class Fisherman {

8.     public static voidmain(String[] args) {

9.     Fish f = new Walleye();

10. Walleye w = new Walleye();

11. Bluegill b = new Bluegill();

12. if(f instanceof Perch)System.out.print("f-p ");

13. if(w instanceof Fish)System.out.print("w-f ");

14. if(b instanceof Fish)System.out.print("b-f ");

15. }

16. }

What is the result?

A.   w-f

B.   f-p w-f

C.   w-f b-f

D.   f-p w-f b-f

E.   Compilation fails.

F.    An exception is thrown atruntime.

Answer: B

a instanceof  b,指的是a对象是否是b类或其子类的一个实例对象

或者说a对象是否是实现了b接口的一个实例对象。

w对象是Perch子类的实例对象,f对象实现了Fish接口,b对象没有实现Fish接口。

QUESTION 141 
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

QUESTION 142 Given:

11. public void testIfA() {

12. if (testIfB("True")){

13. System.out.println("True");

14. } else {

15. System.out.println("Nottrue");

16. }

17. }

18. public Boolean testIfB(Stringstr) {

19. return Boolean.valueOf(str);

20. }

What is the result when method testIfA isinvoked?

A.   True

B.   Not true

C.   An exception is thrown atruntime.

D.   Compilation fails because of anerror at line 12.E. Compilation fails because of an error at line 19.

Answer: A

public static Boolean valueOf(String s)返回一个用指定的 String 表示值的 Boolean 值。如果 String 参数不为 null 且在忽略大小写时等于 "true",则返回的 Boolean 表示 true 值。  

QUESTION 143 Given:

1. public class Donkey {

2. public static voidmain(String[] args) {

3. boolean assertsOn = false;

4. assert (assertsOn) : assertsOn= true;

5. if(assertsOn) {

6. System.out.println("assertis on");

7. }

8. }

9. }

If class Donkey is invoked twice, thefirst time without assertions enabled, and the second time with assertionsenabled, what are the results?

A. no output

B. no output

assert is on

C.   assert is on

D.   no output

An AssertionError is thrown.ss

E.   assert is on

An AssertionError is thrown.

QUESTION 144
Given:
31. // some code here
32. try {
33. // some code here
34. } catch (SomeException se) {
35. // some code here
36. } finally {
37. // some code here
38. }
Under which three circumstances will the code on line 37 be executed? (Choose three.)
A. The instance gets garbage collected.
B. The code on line 33 throws an exception.
C. The code on line 35 throws an exception.
D. The code on line 31 throws an exception.
E. The code on line 33 executes successfully.
Answer: BCE

finally有几种情况可以执行:正常执行,发生异常被catch块处理,当然,在catch块内发生的异常也行。

QUESTION 145 
Given: 
22. public void go() { 
23. String o = ""; 
24. z: 
25. for(int x = 0; x < 3; x++) { 
26. for(int y = 0; y < 2; y++) { 
27. if(x==1) break; 
28. if(x==2 && y==1) break z; 
29. o = o + x + y; 
30. } 
31. } 
32. System.out.println(o); 
33. } 
What is the result when the go() method is invoked? 
A. 00 
B. 0001 
C. 000120 
D. 00012021 
E. Compilation fails. 
F. An exception is thrown at runtime. 
Answer: C

x=0 y=0 -->o-->00

x=0 y=1 -->o-->00+01->0001

x=1 y=0 1 2--> break;

x=2 y=0 o-->0001+20-->000120

x=2 y=1 break;

最终结果是C

QUESTION 146
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

我写了一点测试了一下,发现如果字符串为空(null),那么toString()函数会抛出NullPointerException异常,而不去转换直接输出的时候不会。


[java]  view plain  copy

  1. package com.xujin;  
  2.   
  3. public class Test {  
  4.     public static void main(String[] args) {  
  5.   
  6.   
  7.         String s = null;  
  8.         System.out.println(s);//null  
  9.         //System.out.println(s.toString());//NullPointerException  
  10.         System.out.println(s + "hello~~");//nullhello~~  
  11.               
  12.     }     
  13. }  


QUESTION 147 Given:

10. interface Foo {}

11. class Alpha implements Foo {}

12. class Beta extends Alpha {}

13. class Delta extends Beta {

14. public static void main(String[] args ) {

15. Beta x = new Beta();

16. // insert code here

17. }

18. }

Which code, inserted at line 16, willcause a java.lang.ClassCastException?

A.   Alpha a = x;

B.   Foo f = (Delta)x;

C.   Foo f = (Alpha)x;

D.   Beta b = (Beta)(Alpha)x;

Answer: B

架设Foo是飞接口

//Alpha a=x; //将子类转化为父类对象允许,动物 a=狗		//Foo f=(Delta)x;//将鸽子抓换为fei接口对象		//Foo f=(Alpha)x; //鸟-->飞对象f实现了飞接口的鸟对象


QUESTION 148 
Given: 
33. try { 
34. // some code here 
35. } catch (NullPointerException e1) { 
36. System.out.print("a"); 
37. } catch (Exception e2) { 
38. System.out.print("b"); 
39. } finally { 
40. System.out.print("c"); 
41. } 
If some sort of exception is thrown at line 34, which output is possible? 
A. a 
B. b 
C. c 
D. ac 
E. abc 
Answer: D 


本处意思,如果在34行出现了异常,则最可能是ac或bc,但是没有bc,则选D


QUESTION 149 Given:

11. public class Test {

12. public enum Dogs {collie,harrier, shepherd};

13. public static void main(String[] args) {

14. Dogs myDog = Dogs.shepherd;

15. switch (myDog) {

16. case collie:

17. System.out.print("collie");

18. case default:

19. System.out.print("retriever");

20. case harrier:

21. System.out.print("harrier");

22. }

23. }

24. }

What is the result?

A.   harrier

B.   shepherd

C.   retriever

D.   Compilation fails.

E.   retriever harrier

F.    An exception is thrown atruntime.

Answer: D

d,错在了case default,有default,前面不用加case


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: