scjp jdk1.5 ——4.19(6)

Question: 21
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

解:

主要在join()join()的调用者必须是个线程。此处t调用join()t作为主线程外的一个子线程运行。Join()告诉主线程:“必须等到子线程结束才能够做继续主线程”。所以就出现了E的答案

 



Question: 22
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 at runtime.

Answer: D

 

不知道这题有没有出错,case default明显是不对的。编译失败理所当然

然后如果原本是没有case,那么就是E答案了

 



Question: 23
Given:
8. public class test {
9. public static void main(String [] a) {
10. assert a.length == 1;
11. }
12.}
Which two will produce an AssertionError? (Choose two.)
A. java test
B. java -ea test
C. java test file1
D. java -ea test file1
E. java -ea test file1 file2
F. java -ea:test test file1

Answer: B, E

java -ea

 

    -ea[:<packagename>...|:<classname>]

    -enableassertions[:<packagename>...|:<classname>]

                  enable assertions

 

A. java test // 没有 enable Assertion

 

B. java -ea test // a.length = 0, AssertionError

 

C. java test file1 // 没有 enable Assertion

 

D. java -ea test file1 // a.length = 1, OK

 

E. java -ea test file1 file2 // a.length = 2, AssertionError

 

F. java -ea:test test file1 // a.length = 1, OK

 



Question: 24
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, will cause 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

Xbeta,但是b要将他转化为子类类型,不可能啦

 



Question: 25
Given:
11. public static Collection get() {
12. Collection sorted = new LinkedList();
13. sorted.add("B"); sorted.add("C"); sorted.add("A");
14. return sorted;
15. }
16. public static void main(String[] args) {
17. for (Object obj: get()) {
18. System.out.print(obj + ", ");
19. }
20. }
What is the result?
A. A, B, C,
B. B, C, A,
C. Compilation fails.
D. The code runs with no output.
E. An exception is thrown at runtime.

Answer: B

Linkedlist()是插入顺序的。

 

 

posted @ 2014-04-19 08:27  Fresher_Z  阅读(127)  评论(0编辑  收藏  举报