SCJP-02 流程控制 11题.docVIP

  • 12
  • 0
  • 约6.32千字
  • 约 8页
  • 2017-06-24 发布于河南
  • 举报
SCJP-02 流程控制 11题

8二 流程控制11题 (1) Given the following Java code: 11. 12. 13. 14. 15. public static void main (String[] args ) { Object obj = new int [] {1, 2, 3 } int [] someArray = ( int [] ) obj; for ( int x : someArray ) System.out.print(x+ ); } What is the result? (結果為何?) A. 1 2 3 B. Compilation fails because of an error in line 12. (因為在第12列的錯誤而編譯失敗) C. Compilation fails because of an error in line 13. (因為在第13列的錯誤而編譯失敗) D. Compilation fails because of an error in line 14. (因為在第14列的錯誤而編譯失敗) E. A ClassCaseException is thrown at runtime. (執行時期會丟出 ClassCaseException ) A 陣列 is-a Object, 所以第12列沒有問題 再將 obj 強制轉型回 int[], 也沒有問題 最後以 foreach 將該陣列值印出, 將會是 1 2 3 () Given the following Java code: 25. 26. 27. 28. 29. int x = 12; while ( x10 ) { x--; } System.out.print( x ); What is the result? A. 0 B. 10 C. 12 D. Line 29 will never be reached. (第29列不可能執行到) C 一開始準備執行到 while 迴圈 12 10 並不成立, 所以將直接印出 12 (3) Given the following Java code: public static Iterator reverse ( List list ) { Collections.reverse( list ); return list.iterator(); } public static void main(String[] args ) { List list = new ArrayList(); list.add(1); list.add(2); list.add(3); for ( Object obj: reverse(list)) System.out.print(obj + , ); } What is the result? A. 3, 2, 1 B. 1, 2, 3 C. Compilation fails D. The code runs with no output. (本程式碼執行, 不會有任何輸出) E. An exception is thrown at runtime C 題目中的 for 迴圈是寫成 foreach 語法. 但是 reverse() 其傳回執型態為 Iterator, 並非陣列型態或是 java.util.Collection 型別 其實, 若要探究根源, 就是只要有 implements java.lang.Iterable 的介面, 就可以使用 foreach 語法將其元素取回. java.util.Collection 介面之父介面就是 java.lang.Iterable 而 java.util.Iterator 介面並非 java.lang.Iterable 所以將會產生編譯時期的錯誤 (4) Given the following Java code: 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. public static void main( string[] args ) { String str = null; if( str == null ) { System.out.println(null); } else ( str.length() == 0 ) { System.out.println(zero); } else { System.out.println(some); } } What is the result? (結果為何?) A. B. C. D. E. null zero some Co

文档评论(0)

1亿VIP精品文档

相关文档