呵呵呵~忙錄、忙忙忙,但我又有新的問題啦!這次的分別是commarTo()、equals(), hashCode(),及Class Cast的問題嘍!
而且值得慶幸的是:我不會的題目有比較少唷XD所以這次才一次有那麼多不同類型的題目啊!
第23題:
Given:1. public class Drink implements Comparable {2. public String name;
3. public int compareTo(Object o) {
4. return 0;
5. }
6. }
and:20. Drink one = new Drink();21. Drink two = new Drink();
22. one.name= "Coffee";
23. two.name= "Tea";
24. TreeSet set = new TreeSet();
25. set.add(one);
26. set.add(two);
A programmer iterates over the TreeSet and prints the name of each Drink object.
What is the result?
A. Tea
B. Coffee
C. Coffee Tea
D. Compilation fails
E. The code runs with no output.
F. An exception is thrown at runtime.
為何答案只有Coffee呢?為何不是Coffee Tea哩?(十萬個為什麼@@)
第191題:
Given:11. public class Person {12. private String name, comment;
13. private int age;
14. public Person(String n, int a, String c) {
15. name = n; age = a; comment = c;
16. }
17. public boolean equals(Object o) {
18. if (! (o instanceof Person)) return false;
19, Person p = (Person)o;
20. return age == p.age && name.equals(p.name);
21. }
22. }
What is the appropriate definition of the hashCode method in class Person?
A. return super.hashCode();
B. return name.hashCode() + age * 7;
C. return name.hashCode() + comment.hashCode() / 2;
D. return name.hashCode() + comment.hashCode() / 2 - age * 3;
為何答案是B而不是A呢?我搞不太懂說Orz.
第9題:
Given:1. class Super {2. private int a;
3. protected Super(int a) { this.a = a; }
4. }
...
11. class Sub extends Super {
12. public Sub(int a) { super(a); }
13. public Sub() { this.a = 5; }
14. }
Which two, independently, will allow Sub to compile? (Choose two.)
A. Change line 2 to:
public int a;
B. Change line 2 to:
protected int a;
C. Change line 13 to:
public Sub() { this(5); }
D. Change line 13 to:
public Sub() { super(5); }
E. Change line 13 to:
public Sub() { super(a); }
Why?why?CD對嗎?WHY???
第109題:
What two must the programmer do to correct the compilation errors? (Choose two.)
A. insert a call to this() in the Car constructor
B. insert a call to this() in the MeGo constructor
C. insert a call to super() in the MeGo constructor
D. insert a call to super(vin) in the MeGo constructor
E. change the wheelCount variable in Car to protected
F. change line 3 in the MeGo class to super.wheelCount = 3;
這題我陣亡了Orz
第126題:
What is the result?
A. 4321
B. 0000
C. An exception is thrown at runtime
D. Compilation fails because of an error in line 18.
為何答案是D不是A哩?
第324題:
Given: 11. class Cup { }
12. class PoisonCup extends Cup {
}
... 21. public void takeCup(Cup c) {
22. if (c instanceof PoisonCup) {
23. System.out.println("Inconceivable!");
24. } else if (c instanceof Cup) {
25. System.out.println("Dizzying intellect!");
26. } else {
27. System.exit(0);
28. }
29. }
And the execution of the statements: Cup cup = new PoisonCup(); takeCup(cup);
What is the output?
A. Inconceivable!
B. Dizzying intellect!
C. The code runs with no output.
D. An exception is thrown at runtime.
E. Compilation fails because of an error in line 22.
請救救我吧Orz...

加油~
Thank you. ^_^
我陪妳~ 不過妳得自己慢慢研究....
呵呵 謝謝^^"
完全不負責任之亂猜 23 TreeSet 在書上是這樣寫 add(Object o): Adds the specified element to this set if it is not already present. 意思如果 TreeSet 裡面沒有這個 Object 才加進去 因為 compareTo 都是 0, 意即每個 Drink Object 在比較時都視為一樣 所以只有 "Coffee" 被加進 TreeSet 囉 191 因為沒更好答案啦 Person 的 Super 是 Object。 Object 的 hashCode 也沒意義啊 age * 7 的 7 是無釐頭 放 7 或放 100 都沒差 9 是 CD 沒錯 a 是 private 在 Sub 裡面看不到 a 109 跟上題同 wheelCount 是 private 在 MeGo 裡面看不到 選 E 改成 protected 126 因為 Person 沒有 Person(){} constructor 所以 line 18 會爆 可以加 Person(){} 或在 line 18 加 super(""); 324 B 啊 cup 雖然是 PoisonCup class 但已被 downcast 成 Cup 了
呵呵~ 不過感覺你這次的「不負責任」感覺比上次清楚多嘍XD
上次... 上次那個超多題的 眼睛都花了啊...
上次真的有那麼多嗎@@"? 那真的是API的題目太難了Orz...
看不懂 @@ 呵~ 天天做這個題目 會掉頭髮吧 :p
沒天天啊! 這幾天只要有去學校都在幫老師忙認證的東西 明天中午有預評,所以我早上想提早去學校幫老師處理嘍!(因為我上午第二節開始有課) 所以希望在那之前可以趕完... 但我還在想那麼早就要出門我起床後還要不要先寫日記XD 不過我打算準備要去睡了,看早上如果醒來的早的話還是會先上線的吧XD 至於,這些題目我如果是連續很多題都解不出來的話,我到是真的會想抓頭髮@@"
啊... 我想改答案... (汗汗~ 324 應該是 A 吧 雖然 cast 成 Cup 但是 instance 不會變啊 想的頭有點痛起來 (也許等一下又來改... )
sorry 讓你頭疼了/___\ 真的很謝謝你(感動)^_^
有時候都會想...JavaScript幹嘛不像PHP那樣簡單明瞭 不過想一想...JS的執行環境本來就不單純(客戶端),複雜些也是應該的~XD
呵~或許...有些也是習慣問題吧 有的人可能會覺得都很簡單 但也有些人覺得兩種都很難@@"
說真的,PHP的CLASS我當初也花了快一個月的時間才搞懂七八分 相較之下JS的一堆類別如果搞懂並能靈活運用了,你會是個"能者"~呵呵
我是弱者...
跟上次一樣不懂Orz! 不能幫上雲雲的忙真的抱歉, 我只好在精神上支持雲雲了ˊˋ, 雲雲加油, 我與妳同在> <"!
呵呵~ 謝謝你^^