- 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 3B. 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. 0B. 10C. 12D. 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, 1B. 1, 2, 3C. Compilation failsD. 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. nullzerosomeCo
您可能关注的文档
最近下载
- 克劳锐:2022年社交媒体美妆内容发展洞察.pdf VIP
- 人大社《社会工作综合能力(初级)》第四章 社会工作服务专业价值观与道德规范 社会工作综合能力(初级).pptx VIP
- 激活个体与团队-利用PK机制推动业务变革.pptx VIP
- 人大社《社会工作综合能力(初级)》第十二章 社会工作服务相关法规与政策 社会工作综合能力(初级).pptx VIP
- 从物、符号构成的世界全域看文艺——赵毅衡教授的“艺术符号论”述评.pdf VIP
- 新世纪大学英语综合教程 (第3版)第1册 PPT课件U1.pptx
- 高效能管理系列工作之PK机制导入.pdf VIP
- 病历丢失应急预案流程.docx VIP
- 毕业设计(论文)-中型货车驱动桥主减速器设计.doc VIP
- 人大社《社会工作综合能力(初级)》第六章 个案社会工作服务方法 社会工作综合能力(初级).pptx VIP
原创力文档

文档评论(0)