ocjp 考試題之六

OCJP視頻課堂,具體講解:https://edu.csdn.net/course/detail/7811
java

QUESTION 134app

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
ide

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的參數傳遞所有都是引用~~不變哦~~ui

QUESTION 136 Given classes defined intwo different files:this

1. package util;url

2. public class BitUtils {spa

3. private static voidprocess(byte[] b) {} //注意這裏的訪問修飾符是private,在類外不能訪問,因此選擇F.net

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

相關文章
相關標籤/搜索