第四十一題:

Given:

public class KungFu {
    public static void main(String[] args) {
        Integer x = 400;
        Integer y = x;
        x++;
        StringBuilder sb1 = new StringBuilder("123");
        StringBuilder sb2 = sb1;
        sb1.append("5");
        System.out.println((x == y) + " " + (sb1 == sb2));
    }
}

What is the result?

A. true true
B. false true
C. true false
D. false false
E. Compilation fails.
F. An exception is thrown at runtime.

答案:

執行結果:無

題目範圍:基本觀念 WapperClass

解析:

IntegerintWapperClass,雖然以物件的形式來表現,不過在做等於運算的時候要視為基本資料型態,以傳值的作法來做
一般物件的話當然做等於運算就是傳指標了

 

第四十二題:

class A {
    public String doit(int x, int y) {
        return "a";
    }

    public String doit(int... vals) {
        return "b";
    }
}

Given:

25. A a = new A();
26. System.out.println(a.doit(4, 5));

What is the result?

A. Line 26 prints "a" to System.out.
B. Line 26 prints "b" to System.out.
C. An exception is thrown at runtime.
D. Compilation of class A will fail due to an error in line 6.

答案:

執行結果:答案中

題目範圍:方法多載

解析:

這是方法多載比較特別的情況
當多重參數方法跟一般的多變數方法有類似的傳入值時,會優先採用多變數方法

 

第四十三題:

Given:

class Plant {
    private String name;

    public Plant(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

public class Tree extends Plant {
    public void growFruit() {
    }

    public void dropLeaves() {
    }
}

What statement is true?

A. The code will compile without changes.
B. The code will compile if public Tree(){Plant();} is added to the Tree class.
C. The code will compile if public Plant(){Tree();} is added to the Plant class.
D. The code will compile if public Plant(){this("fern");} is added to the Plant class.
E. The code will compile if public Plant(){Plant("fern");} is added to the Plant class.

答案:

執行結果:無

題目範圍:物件觀念

解析:

繼承類別時,建構子的呼叫
父代如果只有需要參數的建構子,子代在建構子中,就必須寫出super() 來傳遞參數
否則就必須在父代增加一個無參數的建構子

 

第四十四題:

Given:

class Employee {
    String name;
    double baseSalary;

    public Employee(String name, double baseSalary) {
        this.name = name;
        this.baseSalary = baseSalary;
    }
}

public class SalesPerson extends Employee {
    double commission;

    public SalesPerson(String name, double baseSalary, double commission) {
        // insert code here
    }

}

Which two code fragments, inserted independently at line 12, will compile? (Choose two.)

A. super(name, baseSalary);

B. this.commission = commission;

C. super();

 this.commission = commission;

D. this.commission = commission;

 super();

E. super(name, baseSalary);

 this.commission = commission;

F. this.commission = commission;

 super(name, baseSalary);

G. super(name, baseSalary, commission);

答案:AE

執行結果:無

題目範圍:物件觀念

解析:

與四十三題觀念類似

 

第四十五題:

Given that:

Gadget has-a Sprocket and
Gadget has-a Spring and
Gadget is-a Widget and
Widget has-a Sprocket
Which two code fragments represent these relationships?
(Choose two.)

A. class Widget{Sprocket s;}
 class Gadget extends Widget{Spring s;}

B. class Widget{}
 class Gadget extends Widget{Spring s1; Sprocket s2;}

C. class Widget{Sprocket s1; Spring s2;}

 class Gadget extends Widget{}

D. class Gadget{Spring s;}

 class Widget extends Gadget{Sprocket s;}

E. class Gadget{}

 class Widget extends Gadget{Sprocket s1; Spring s2;}

F. class Gadget{Spring s1; Sprocket s2;}

 class Widget extends Gadget{}

答案:AC

執行結果:無

題目範圍:物件觀念

解析:

英文解釋:Gadget 機件,Widget 部件,Sprocket 鍊條,Spring 彈簧

A完全符合題目

B的Widget 沒有容物,直到Gadget繼承了之後才加入,因此不正確

C的Widget 直接加入了兩個內容物,然後Gadget 繼承之後也會有父代的內容物,正確。題目沒有規定Widget 不能有其他的內容物

DEF的繼承關係反了,也就沒甚麼好說的

arrow
arrow

    yaya741228 發表在 痞客邦 留言(1) 人氣()