OCP Java 自测
一個朋友準備去考OCP Java認證,即原來的SCJP。心血來潮也想測測自己什么水平。找了本McGraw.Hill.OCP.Java.SE.6.Programmer.Practice.Exams,開盤就是兩套自測題。14個題目,給了42分鐘,按書中說法是過了8個就可以去考了。掐上秒表,開工了。等我做完,一看時間才10分鐘,不由有些得意。也沒再檢查,直接對起答案,結果僅對了7個。雖然懊惱,但總得搞清楚自己錯在哪里。
1. 問下面這段程序的輸出結果?
public class Bunnies { 
 ??? static int count = 0;
??? Bunnies() { 
 ??????? while (count < 10) 
 ??????????? new Bunnies(++count); 
 ??? }
??? Bunnies(int x) { 
 ??????? super(); 
 ??? }
??? public static void main(String[] args) { 
 ??????? new Bunnies(); 
 ??????? new Bunnies(count); 
 ??????? System.out.println(count++); 
 ??? } 
 }
A. 9 
 B. 10 
 C. 11 
 d. 12 
 E. Compilation fails. 
 F. An exception is thrown at runtime.
再一細看,沒把我給氣死。明明算出來是10,卻選了C,又是粗心大意。這題就是考自增自減嘛,++放在變量前與變量后的區別。另外就是在構造方法里是可以用new關鍵字的。
2. 問下面這段程序的輸出結果?
public class Twine { 
 ??? public static void main(String[] args) { 
 ??????? String s = ""; 
 ??????? StringBuffer sb1 = new StringBuffer("hi"); 
 ??????? StringBuffer sb2 = new StringBuffer("hi"); 
 ??????? StringBuffer sb3 = new StringBuffer(sb2); 
 ??????? StringBuffer sb4 = sb3; 
 ??????? if (sb1.equals(sb2)) s += "1 "; 
 ??????? if (sb2.equals(sb3)) s += "2 "; 
 ??????? if (sb3.equals(sb4)) s += "3 "; 
 ??????? String s2 = "hi"; 
 ??????? String s3 = "hi"; 
 ??????? String s4 = s3;
??????? if (s2.equals(s3)) s += "4 "; 
 ??????? if (s3.equals(s4)) s += "5 "; 
 ??????? System.out.println(s); 
 ??? } 
 }
A. 1 3 
 B. 1 5 
 C. 1 2 3 
 D. 1 4 5 
 E. 3 4 5 
 F. 1 3 4 5 
 G. 1 2 3 4 5 
 H. Compilation fails.
StringBuffer 并沒有重載equals方法,不要想當然的以為比較的是字符串的值。正解:E。
3. 下面哪些是正確的?
A. All classes of Exception extend Error. 
 B. All classes of Error extend Exception. 
 C. All Errors must be handled or declared. 
 D. All classes of Exception extend Throwable. 
 E. All Throwables must be handled or declared. 
 F. All Exceptions must be handled or declared. 
 G. RuntimeExceptions need never be handled or declared.
這題錯的實在有些不應該。我咋就選成FG了呢?這兩個答案明顯是矛盾的啊。F說所有的異常都必須被處理,G卻講運行時異常是個例外。作者給42分鐘還是挺有道理的,仔細檢查是很有必要的。正解:DG。
4. 問運行結果:java Birthdays Draumur?
public class Birthdays { 
 ??? public static void main(String[] args) { 
 ??????? Map<Friends, String> hm = new HashMap<Friends, String>(); 
 ??????? hm.put(new Friends("Charis"), "Summer 2009"); 
 ??????? hm.put(new Friends("Draumur"), "Spring 2002"); 
 ??????? Friends f = new Friends(args[0]); 
 ??????? System.out.println(hm.get(f)); 
 ??? } 
 }
class Friends { 
 ??? String name;
??? Friends(String n) { 
 ??????? name = n; 
 ??? } 
 }
A. null 
 B. Draumur 
 C. Spring 2002 
 D. Compilation fails. 
 E. The output is unpredictable. 
 F. An exception is thrown at runtime. 
 G. Friends@XXXX (where XXXX is a representation of a hashcode)
再看此題時,猛然想起Think In Java里提到過,要作為HashMap的Key必須重載equals()和hashCode()方法,HashMap基于它們來判斷兩個對象是否相等。這個Friends 沒有重載equals()和hashCode(),因而直接以對象的引用作為Key,而不是Name,當然用hm.get(f)就什么也得不到啦。正解:A。
5. 下面哪些是正確的?
A. Compilation succeeds. 
 B. Compilation fails due to an error on line 6. 
 C. Compilation fails due to an error on line 7. 
 D. Compilation fails due to an error on line 8. 
 E. Compilation fails due to an error on line 9. 
 F. Compilation fails due to an error on line 10. 
 G. Compilation fails due to an error on line 11.
這題主要考泛型不支持多態。正解:BDEFG。
6. 下面哪行代碼插入打下圖42行處可以正常編譯運行?
37. boolean b = false; 
 38. int i = 7; 
 39. double d = 1.23; 
 40. float f = 4.56f; 
 41. 
 42. // insert code here
A. System.out.printf(" %b", b); 
 B. System.out.printf(" %i", i); 
 C. System.out.format(" %d", d); 
 D. System.out.format(" %d", i); 
 E. System.out.format(" %f", f);
考的是格式化參數,忘卻了就記憶一下http://hi.baidu.com/giml/blog/item/c6b1d0fa4a5bded9b48f31dd.html 。
正解:ADE。
7. 下面哪個是正確的?
public class MyPancake implements Pancake { 
 ??? public static void main(String[] args) { 
 ??????? List<String> x = new ArrayList<String>(); 
 ??????? x.add("3"); 
 ??????? x.add("7"); 
 ??????? x.add("5"); 
 ??????? List<String> y = new MyPancake().doStuff(x); 
 ??????? y.add("1"); 
 ??????? System.out.println(x); 
 ??? }
??? List<String> doStuff(List<String> z) { 
 ??????? z.add("9"); 
 ??????? return z; 
 ??? } 
 }
interface Pancake { 
 ??? List<String> doStuff(List<String> s); 
 }
A. [3, 7, 5] 
 B. [3, 7, 5, 9] 
 C. [3, 7, 5, 9, 1] 
 D. Compilation fails. 
 E. An exception is thrown at runtime.
這題設置了一個陷阱,接口里的方法默認都是Public的,子類實現后,訪問控制權限只能放大不能縮小,所以要在doStuff方法聲明處加上Public。正解:D。
看看自己犯的錯誤,除了第五個和第六個,確實不是很清楚,其他都是可以避免的。相關源代碼:
http://javaforge.com/svn/RoaringTiger/trunk/OCP 。
轉載于:https://blog.51cto.com/lovespss/534047
總結
以上是生活随笔為你收集整理的OCP Java 自测的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: redhat6 使用raid5的系统安装
- 下一篇: 深入Django(1): 通用视图 (g
