ocjp考試題之三

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.java

若是if後面沒有語句塊,就是緊跟的一句語句受if影響app

&與&&的區別,&&有短路功能當第一個語句是false時將不判斷第二個語句,&符號兩邊不是boolean值時,執行位運算。ide

注意第20行代碼處是b2=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
oop

若是拋出一個異常,就不會執行下面的內容,而是返回調用產生異常的方法那裏去。Error類和Exception類同繼承自Throwable類,main函數不能處理Error類異常,因此一個Thorwable被main拋出。ui

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
this

11. foo();和12. System.out.print(x + ", ");隨時均可以被打斷,因此他兩個必須有原子性,且要求x每加一就輸出一次,synchronized 修飾符修飾foo正好能夠知足。spa

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 
.net

子線程和主線程的速度不同,System.out.println("run.");和System.out.println("End of method.");哪個首先執行也不必定,可是必須是現輸出run再有一個異常。異常輸出後還要往下執行。線程



QUESTION 62 Given:

12. Date date = new Date();

13. df.setLocale(Locale.ITALY);

14. String s = df.format(date);

The variable df is an object of typeDateFormat that has been initialized in line 11. What is the result if thiscode is run on December 14, 2000?

A.   The value of s is 14-dic-2000.

B.   The value of s is Dec 14, 2000.

C.   An exception is thrown atruntime.

D.   Compilationfails because of an error in line 13.

DateFormat沒有setLocale()方法,setLocale是MessageFormat的方法 

QUESTION 63

Given:

1.     public class KungFu {

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

3.     Integer x = 400;

4.     Integer y = x;

5.     x++;

6.     StringBuilder sb1 = newStringBuilder("123");

7.     StringBuilder sb2 = sb1;

8.     sb1.append("5");

9.     System.out.println((x==y) +" " + (sb1==sb2));

10. }

11. }

What is the result?

A.   true true

B.   false true

C.   C. true false

D.   false false

E.   Compilation fails.

F.    An exception is thrown atruntime.

Answer: B  

sb1,sb2,指向的都是同一個地址;


QUESTION 64
Given that the current directory is empty, and that the user has read and write privileges to the current
directory, and the following:
1. import java.io.*;
2. public class Maker {
3. public static void main(String[] args) {
4. File dir = new File("dir");
5. File f = new File(dir, "f");
6. }
7. }
Which statement is true?
A. Compilation fails.
B. Nothing is added to the file system.
C. Only a new file is created on the file system.
D. Only a new directory is created on the file system.
E. Both a new file and a new directory are created on the file system.

沒有調用建立方法,只是申明瞭Fiel類


QUESTION 65
Given:
12. String csv = "Sue,5,true,3";
13. Scanner scanner = new Scanner( csv );
14. scanner.useDelimiter(",");
15. int age = scanner.nextInt();
What is the result?
A. Compilation fails.
B. After line 15, the value of age is 5.
C. After line 15, the value of age is 3.
D. An exception is thrown at runtime.

獲取的標記與指望類型的模式不匹配

useDelimiter 將此掃描器的分割模式設置爲從指定String構造的模式。

Delimiter英文意思爲分隔符;useDelimiter( )方法默認以空格做爲分隔符;固然也修改,如:

useDelimiter(",");   //以','爲分隔符

useDelimiter("\n"); //「\n」換行符(回車)做爲輸入的分隔符。

QUESTION 67 Given that Triangleimplements Runnable, and:

31. void go() throws Exception {

32. Thread t = new Thread(newTriangle());

33. t.start();

34. for(int x = 1; x < 100000;x++) {

35. //insert code here

36. if(x%100 == 0)System.out.print("g");

37. } }

38. public void run() {

39. try {

40. for(int x = 1; x < 100000;x++) {

41. // insert the same code here

42. if(x%100 == 0)System.out.print("t");

43. }

44. } catch (Exception e) { }

45. }

Which two statements, insertedindependently at both lines 35 and 41, tend to allow both threads totemporarily pause and allow the other thread to execute? (Choose two.)

A.   Thread.wait();

B.   Thread.join();

C.   Thread.yield();//線程捨棄

D.   Thread.sleep(1);// 線程休眠

E.   Thread.notify();

Answer: CD

QUESTION 71 
Given: 
10. interface A { void x(); } 
11. class B implements A { public void x() {} public void y() {} } 
12. class C extends B { public void x() {} } And: 
20. java.util.List<A> list = new java.util.ArrayList<A>(); 
21. list.add(new B()); 
22. list.add(new C()); 
23. for (A a : list) { 
24. a.x(); 
25. a.y(); 
26. } 
What is the result? 
A. The code runs with no output. 
B. An exception is thrown at runtime. 
C. Compilation fails because of an error in line 20. 
D. Compilation fails because of an error in line 21. 
E. Compilation fails because of an error in line 23. 
F. Compilation fails because of an error in line 25. 
Answer: F 

實現了A接口的類的對象a僅僅有x()方法,沒有y()方法!


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

相關文章
相關標籤/搜索