java前测题

1題---------------------------------------------------
class Atom {
    Atom() {
        System.out.print("atom ");
    }
}

class Rock extends Atom {
    Rock(String type) {
        System.out.print(type);
    }
}

public class Mountain extends Rock {
    Mountain() {
        super("granite ");
        new Rock("granite ");
    }

    public static void main(String[] a) {
        new Mountain();
    }
}

What is the result?

(A) atom granite
(B) granite granite
(C) atom granite granite
(D) atom granite atom granite

正確答案: D

解析:若父类有无参的构造函数且子类未显式调用父类构造函数,则java默认调用父类的无参构造函数。

 

 

第2題---------------------------------------------------

1. public class Blip{ 
2.     protected int blipvert(int x){return 0;} 
3. } 
4. class Vert extends Blip{ 
5.     //insert code here 
6. } 

Which one methods, inserted independently at line 5, will not compile?  

(A) public int blipvert(int x){return 0;} 
(B) private int blipvert(int x){return 0;} 
(C) private int blipvert(long x){return 0;} 
(D) protected long blipvert(int x, int y){return 0;}

正確答案: B

解析:访问修饰符的公开程度:public>protected>default>private

     子类继承方法时只能让方法的能见度变得更大

第3題---------------------------------------------------

1. class Super{ 
2.     private int a; 
3.     protected Super(int a){this.a = a;} 
4. } 
... 
11. class Sub extends Super{ 
12.     public Sub(int a){super(a);} 
13.     public Sub(){this.a = 5;} 
14. } 

Which one, independently, will allow Sub to compile? 

(A) Change line 2 to:  public int a; 
(B) Change line 2 to:  protected int a; 
(C) Change line 13 to: public Sub(){this(5);} 
(D) Change line 13 to: public Sub(){super(a);}

正確答案: C

解析:若父类只有有参构造函数,则子函数必须重写构造方法而且第一行要显式调用父类的有参构造函数

 

第4題---------------------------------------------------

Which Man class properly represents the relationship "Man has a best friend who is a Dog"? 

(A) class Man extends Dog{} 
(B) class Man implements Dog{} 
(C) class Man{private BestFriend dog;} 
(D) class Man{private Dog bestFriend;}

正確答案: D

解析:1is-a:類別間的繼承關係 

        2has-a:類別間的包含關係 

 

 

第5題---------------------------------------------------

 

class Batman { 
    int squares = 81; 

    public static void main(String[] args) { 
        new Batman().go(); 
    } 

    void go() { 
        incr(++squares); 
        System.out.println(squares); 
    } 

    void incr(int squares) { 
        squares += 10; 
    } 


What is the result? 

(A) 81 
(B) 82 
(C) 91 
(D) 92

正確答案: B

 

解析:incr函数中的squares是局部变量,所以没对全局的squares进行修改

 

 

第6題---------------------------------------------------

class Pass { 
    public static void main(String[] args) { 
        int x = 5; 
        Pass p = new Pass(); 
        p.doStuff(x); 
        System.out.print(" main x = " + x); 
    } 

    void doStuff(int x) { 
        System.out.println(" doStuff x = " + x++); 
    } 


What is the result? 

(A) doStuff x = 5 main x = 6 
(B) doStuff x = 6 main x = 5 
(C) doStuff x = 6 main x = 6 
(D) doStuff x = 5 main x = 5


正確答案: D

解析:dostuff 函数中的 x 是局部变量,所以没对全局的 x 进行修改

 

 

 

第7題---------------------------------------------------

class ClassA { 
    public int numberOfInstances; 

    protected ClassA(int numberOfInstances) { 
        this.numberOfInstances = numberOfInstances; 
    } 


class ExtendedA extends ClassA { 
    private ExtendedA(int numberOfInstances) { 
        super(numberOfInstances); 
    } 

    public static void main(String[] args) { 
        ExtendedA ext = new ExtendedA(420); 
        System.out.print(ext.numberOfInstances); 

    } 


What is the result? 

(A) 420 is the output 
(B) An exception is thrown at runtime. 
(C) All constructors must be declared public. 
(D) Constructors CANNOT use the private modifier.

正確答案: A

 

第8題---------------------------------------------------

class One { 
    void foo() { 
    } 


class Two extends One { 
    // insert method here 


Which one method, inserted individually at line 14, will not correctly complete class Two?  

(A) int foo(){/* more code here */} 
(B) void foo(){/* more code here */} 
(C) public void foo(){/* more code here */} 
(D) protected void foo(){/* more code here */}


正確答案: A

解析:重写的条件是形参不同

 

第9題---------------------------------------------------

11.interface IF { void set(); } 
12.abstract class Bar implements IF{ public abstract void set(); } 
13.class Baz extends Bar{ public void set(){};}  
14.public class Quz extends Baz{ 
15. public void set(){ System.out.println("Do it !!!"); } 
16. //insert code here 
17. } 
18.} 
下列何者插入第16行沒有使用到 "多型的特性? 
(A)void call ( IF i ){ i.set(); } 
(B)void call ( Bar br ){ br.set(); } 
(C)void call ( Baz bz ){ bz.set(); } 
(D)void call ( Quz q ){ q.set(); } 

正確答案: D

解析: 子类继承方法时只能让方法的能见度变得更大

第10題---------------------------------------------------

11.interface IF { void set(); } 

12.abstract class Bar implements IF{ public abstract void set(); } 
13.class Baz extends Bar{ public void set(){};}  
14.public class Quz extends Baz{ 
15. public void set(){ System.out.println("Do it !!!"); } 
16. //insert code here 
17. } 
18.} 
下列何者插入第16行沒有使用到 "多型的特性? 
(A)void call ( IF i ){ i.set(); } 
(B)void call ( Bar br ){ br.set(); } 
(C)void call ( Baz bz ){ bz.set(); } 
(D)void call ( Quz q ){ q.set(); } 

正確答案: D

解析: 變數宣告為Quz,所以沒有使用到多型的特性 

 

 

第11題---------------------------------------------------

public class Base { 
    public static final String FOO = "foo"; 

    public static void main(String[] args) { 
        Base b = new Base(); 
        Sub s = new Sub(); 
        System.out.print(Base.FOO); 
        System.out.print(Sub.FOO); 
        System.out.print(b.FOO); 
        System.out.print(s.FOO); 
        System.out.print(((Base) s).FOO); 
    } 


class Sub extends Base { 
    public static final String FOO = "bar"; 


What is the result? 

(A) foofoofoofoofoo 
(B) foobarfoobarbar 
(C) foobarfoofoofoo 
(D) foobarfoobarfoo

正確答案: D

解析: 標記為final的屬性雖然不可以修改數值,但是繼承時依然可以被複寫

 

 

 

第12題---------------------------------------------------

 

class Mammal { 


class Raccoon extends Mammal { 
    Mammal m = new Mammal(); 


class BabyRaccoon extends Mammal { 


Which one statment is false?  

(A) Raccoon is-a Mammal 
(B) Raccoon has-a Mammal 
(C) BabyRaccoon is-a Mammal 
(D) BabyRaccoon is-a Raccoon

正確答案: D

 

解析:1、is-a:類別間的繼承關係 

        2、has-a:類別間的包含關係 

 

 

第13題---------------------------------------------------

2. public class Hi{  
3.     void m1(){}  
4.     protected void m2(){}  
5. }  
6. class Lois extends Hi{  
7.     //insert code here  
8. }  

Which one code fragment, inserted independently at line 7, will compile?   

(A) protected void m2(){}  
(B) private void m2(){}  
(C) private void m1(){}  
(D) void m2(){}  

正確答案: A

解析:类的继承规则:能见度只能更大不能变小 

 

 

 

 

 

 

posted on 2016-12-15 16:18  xcshehe  阅读(590)  评论(0编辑  收藏  举报

导航