SCJP 1.6版考題 161

出自 陳富國維基館
於 2013年3月30日 (六) 02:35 由 Ikk (對話 | 貢獻) 所做的修訂 (新頁面: Given: 1. public class Boxer1{ 2. Integer i 3. int x; 4. public Boxer1(int y){ 5. x = i + y; 6. System.out.println(x); 7. } 8. public static void main(String...)
(差異) ←上個修訂 | 最新修訂 (差異) | 下個修訂→ (差異)
前往: 導覽搜尋
Given:
1. public class Boxer1{
2.   Integer i
3.   int x;
4.   public Boxer1(int y){
5.     x = i + y;
6.     System.out.println(x);
7.   }
8.   public static void main(String[] args){
9.     new Boxer1(new Integer(4));
10. }
11.}

What is the result?
 A. The value "4" is printed at the command line.
 B. Compilation fails because of an error in line 5.
 C. Compilation fails because of an error in line 9.
 D. A NullPointerException occurs at runtime.
 E. A NumberFormatException occurs at runtime.
 F. An IllegalStateException occurs at runtime.


解答


Ans: D

解說: 執行建構子時,i是一個null參考,未指向一個有效的Integer物件