经典易错的笔试题目集锦

QUESTION NO: 1 

1. class A{

2.protected int method1(int a, int b) { return 0; }

3. }

Which twoare valid in a class that extends class A? (Choose two)

A. publicint method1(int a, int b) { return 0; }

B. privateint method1(int a, int b) { return 0; }

C. privateint method1(int a, long b) { return 0; }

D. publicshort method1(int a, int b) { return 0; }

E. staticprotected int method1(int a, int b) { return 0; }

publicclass Bextends A{

    /** *@paramargs

     */   

    //can not reduce the visibility of the inherited methodfrom A

    //即不能够使从类A中继续来的方法的可见性降低  

    //private int method1(int a, int b) { return 0; }

   

    //This static method cannot hide the instance method fromA

    //静态方法不能够隐藏继承于A的实例

    //static protected int method1(int a, int b) { return 0;}   

    //返回类型与A中的该方法不一致

    //public short method1(int a, int b) { return 0; }   

    /***总结:类的继承中,如果要想重载父类的方法,必须要和父类中的返回类型、可见性等等都要操作一致

     *否则,程序就会报错。一定遵守子类要遵从于父类的原则

     *而我选择的答案居然是privateintmethod1staticprotectedint

     *我选择第一个的错误理由是:因为原来为保护的,如果我这里设为public,那么就扩展了其原来的可见性

     *本来原来就是对包外不可见的,现在变成对包外可见的了,所以就选择的是private

     *选择第二个的错误理由是:都是保护的,这里只是变成了静态的而已 */   

    //这里是写了一个重载方法,因为参数类型不一致,不会报错

    Private int method1(int a, long b) {return 0;}   

    //可见性可以增大,但是不能够缩小,正确

    Public int method1(int a, int b) {return 0; }   

    Public static void main(String[] args){

       // TODO Auto-generated method stub 

    }

} 

QUESTION NO: 2

1. publicclass Outer{

2. publicvoid someOuterMethod() {

3. // Line3

4. }

5. publicclass Inner{}

6. publicstatic void main( String[]argv ) {

7. Outer o= new Outer();

8. // Line8

9. }

10.} 

Whichinstantiates an instance of Inner?

A. newInner(); // At line 3

B. newInner(); // At line 8

C. newo.Inner(); // At line 8

D. newOuter.Inner(); // At line 8//new Outer().new Inner()

答案如下:

publicclass Outer {

    publicvoid someOuterMethod() {

       // Line 3

       new Inner();//放在这里不出错

    }

    publicclass Inner {  }

 

    publicstaticvoid main(String[] argv){

       Outer o= new Outer();

       // Line 8

       //o不能够被解释成为一种类型,出错

       //new o.Inner();

       /**

        *下面两种用法,都报下面的错误:

        *NoenclosinginstanceoftypeOuterisaccessible.

        *Mustqualifytheallocationwithanenclosinginstance

        *oftypeOuter(e.g.x.newA()wherexisaninstanceofOuter)

        */   

       //new Outer.Inner();//如果是静态内部类则可以。

       //newInner();      

    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值