OCJP視頻課堂,具體講解:https://edu.csdn.net/course/detail/7811java
QUESTION 102 Given:數組
23. Object [] myObjects = {ide
24. new Integer(12),函數
25. new String("foo"),this
26. new Integer(5),spa
27. new Boolean(true).net
28. };code
29. Arrays.sort(myObjects);orm
30. for(int i=0;i<myObjects.length; i++) {視頻
31. System.out.print(myObjects[i].toString());
32. System.out.print("");
33. }
What is the result?
A. Compilation fails due to anerror in line 23.
B. Compilation fails due to anerror in line 29.
C. A ClassCastException occurs inline 29.
D. A ClassCastException occurs inline 31.
E. The value of all four objectsprints in natural order.
Answer: C
sort方法比較的必須是能夠轉化成相同的並且實現了Comparable接口的相同對象
QUESTION 103
Given:
1. public class Donkey {
2. public static void main(String[] args) {
3. boolean assertsOn = false;
4. assert (assertsOn) : assertsOn = true;
5. if(assertsOn) {
6. System.out.println("assert is on");
7. }
8. }
9. }
If class Donkey is invoked twice, the first time without assertions enabled, and the second time with
assertions enabled, what are the results?
A. no output
B. no output
assert is on
C. assert is on
D. no output
An AssertionError is thrown.
E. assert is on
An AssertionError is thrown.
Answer: D
考察assert,若是是斷言有效的狀況,即assert(false),就會拋出一個AssertionError
若是是true,則返回的是assert is on
QUESTION 105 Given:
11. public static voidmain(String[] args) {
12. try {
13. args = null;
14. args[0] = "test";
15. System.out.println(args[0]);
16. } catch (Exception ex) {
17. System.out.println("Exception");
18. } catch (NullPointerExceptionnpe) {
19. System.out.println("NullPointerException");
20. }
21. }
What is the result?
A. test
B. Exception
C. Compilation fails.
D. NullPointerException
Answer: C
首先是小異常處理,而後是大的異常處理!!!!若是返過來,就有問題了,致使編譯失敗!!!
QUESTION 106
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
考察break,當x=0,y=0,o=00;
當x=0,y=1,o=0001;
當x=1,y=0,break掉內循環,o=0001;
當x=2,y=0,o=000120;
當x=2,y=1,break掉斷點處的外循環,o=000120;
QUESTION 109
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
第五行5. x = i+y;,尚未爲i賦值(此時i爲null),沒法運算,注意看屬性Integer i,沒有給i賦值;
QUESTION 111
Given:
1. public class Venus {
2. public static void main(String[] args) {
3. int [] x = {1,2,3};
4. int y[] = {4,5,6};
5. new Venus().go(x,y);
6. }
7. void go(int[]... z) {
8. for(int[] a : z)
9. System.out.print(a[0]);
10. }
11. }
What is the result?
A. 1
B. 12
C. 14
D. 123
E. Compilation fails.
F. An exception is thrown at runtime.
Answer: C
int[]... z表示可變參數,而可變的是數組的個數。一共有兩個數組,輸出每一個數組的第一個元素
QUESTION 112
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
還沒new呢,就賦值。初始化錯誤。
QUESTION 113
Given:
11. class X { public void foo() { System.out.print("X "); } }
12.
13. public class SubB extends X {
14. public void foo() throws RuntimeException {
15. super.foo();
16. if (true) throw new RuntimeException();
17. System.out.print("B ");
18. }
19. public static void main(String[] args) {
20. new SubB().foo();
21. }
22. }
What is the result?
A. X, followed by an Exception.
B. No output, and an Exception is thrown.
C. Compilation fails due to an error on line 14.
D. Compilation fails due to an error on line 16.
E. Compilation fails due to an error on line 17.
F. X, followed by an Exception, followed by B.
Answer: A
多態性,首先是調用的SubB裏面的foo()函數,而執行SubB裏面的foo()函數首先就要super.foo();即調用父類的foo()函數輸出X,後來就會拋出一個異常,回到調用該拋出異常的方法的地方。不會輸出B
QUESTION 125
A team of programmers is reviewing a proposed API for a new utility class. After some discussion, they
realize that they can reduce the number of methods in the API without losing any functionality. If they
implement the new design, which two OO principles will they be promoting?
A. Looser coupling
B. Tighter coupling
C. Lower cohesion
D. Higher cohesion
E. Weaker encapsulation
F. Stronger encapsulation
Answer: A
內聚:一個模塊內各個元素彼此結合的緊密程度
耦合:一個軟件結構內不一樣模塊之間互連程度的度量
這裏減小API數量卻不丟失任何功能,意思是各個模塊之間的聯繫很小,互聯程度很低,屬於低耦合。