Java data types – 28

      在〈Java data types – 28〉中尚無留言

Q001

NO.11 Which two are successful examples of autoboxing? (Choose two.)
A. String a = "A";
B. Integer e = 5;
C. Float g = Float.valueOf(null);
D. Double d = 4;
E. Long c = 23L;
F. Float f = 6.0;


Answer: BE

Double d = 4 是錯誤的,因為 Double 可以自動封裝 double,但不會自動從 int → double → Double。
float a=3.14 是錯誤的,因為 Java 把浮點數預設為 double,所以必需寫成 float a=3.14f;
Long b=4;也是錯誤的,因為 4 預設是 int, 而 int 不能變成 Long。

Q002

NO.15 Given:
public class Person {
    private String name;
    public void setName(String name) {
        String title = "Dr. ";
        name = title + name;
    }
    public String toString() {
        return name;
    }
}

and

public class Test {
    public static void main(String args[]) {
        Person p = new Person();
        p.setName("Who");
        System.out.println(p);
    }
}
What is the result?
A. Dr. Who
B. Dr. Null
C. An exception is thrown at runtime.
D. null


Answer: D 

上述的 name = title + name 是 setName 的區域變數,並不是 class Person 的物件變數。

🔒 更多內容,請登入會員繼續閱讀。

立即登入

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *