Java基础练习题 考试题 笔试题 面试题 (3)

来源:互联网 发布:js判断数字为整数 编辑:程序博客网 时间:2024/05/01 23:41

八、继承与多态

1)以下程序调试结果为:

class Base{

   Base(){

      int  i = 100;    

      System.out.print (i);

   }

 }

 public class Pri extends Base{

    static int i = 200;

    public static void main(String argv[]){

         Pri p = new Pri();

         System.out.print(i);

    }

 }

 A.编译错误    B200   C100200    D100

2 以下程序调试结果为:

 public class Test {

    int m=5;

    public  void some(int x) {

        m=x;

    }

    public static void main(String args []) {

        new Demo().some(7);

    }

}

class Demo extends Test {

    int m=8;

    public  void some(int x) {

        super.some(x);

       System.out.println(m);

    }

}

A5    B8   C7   D.无任何输出  E.编译错误

3)  试完成下述程序片段:

public class Point()

{  int x,y;

    public  Point(int x,int y)

    {      =x;      =y;

     }

     ......

 }

A Point.x   Point.y     B.无解   C x1    y1   Dthis.x    this.y

4)考虑如下类:

1. class Test(int i) {

2.    void test(int i) {

3.       System.out.println("I am an int.");

4.     }

5.    void test(String s) {

6.       System.out.println("I am a string.");

7.    }

8.

9.    public static void main(String args[]) {

10.     Test t=new Test();

11.     char ch="y";

12.     t.test(ch);

13.   }

14. }

以下哪条为真?

A.行 5 不能通过编译,方法不能被覆盖.

B.行 12 不能通过编译, 因为没有一个test()方法含字符参数.

C.代码可以编译但在12行将出现异常.

D.代码可以编译且产生如下输出: I am an int.

E.代码可以编译且产生如下输出: I am a String.

(5) Test1定义如下:

1public  class  Test1{

2        public  float  aMethodfloat  afloat  b{   }

3       

4}

将以下哪种方法插入行3是不合法的。(          

Apublic  float  aMethodfloat  a float  bfloat  c{  }

Bpublic  float  aMethodfloat  cfloat d{  }

Cpublic  int  aMethodint  a int b{  }

Dprivate float  aMethodint aint bint c{  }

6)考虑如下代码:

class Tree{}

class Pine extends Tree{}

class Oak extends Tree{}

public class Forest {

public static void main( String[] args ) {

      Tree tree = new Pine();

      if( tree instanceof Pine )

          System.out.println( "Pine" );

      if( tree instanceof Tree )

          System.out.println( "Tree" );

      if( tree instanceof Oak )

       System.out.println( "Oak" );

      else

System.out.println( "Oops" );

   }

}

则输出结果中有哪些?

APine   BTree   CForest   DOops   E.无输出

7)以下程序的编译和运行结果为?

abstract class Base{

    abstract public void myfunc();

    public void another(){

        System.out.println("Another method");

    }

}

public class Abs extends Base{

    public static void main(String argv[]){

        Abs a = new Abs();

        a.amethod();

     }

    public void myfunc(){

         System.out.println("My Func");

     }

    public void amethod(){

        myfunc();     

     }

}

A.输出结果为 My Func

B.编译指示 Base 类中无抽象方法

C.编译通过,但运行时指示Base 类中无抽象方法

D.编译指示Base 类中的myfunc方法无方法体,没谁会喜欢该方法。

8) 以下程序的调试结果为?

class Base{

public final void amethod(){

        System.out.println("amethod");

        }

}

public class Fin extends Base{

   public static void main(String argv[]){

        Base b = new Base();

        b.amethod();

   }

}

A.编译指示带有final 方法的类自己必须定义为final

B.编译指示不能继承含有final 方法的类

C.运行错误,原因是Base类没有定义为final

D.运行输出 amethod

9) 在同一目录编译和运行以下两文件结果如何?

//文件 P1.java

package MyPackage;

class P1{

    void afancymethod(){

        System.out.println("What a fancy method");

    }

}

//文件 P2.java

public class P2 extends P1{

    public static void main(String argv[]){

        P2 p2 = new P2();

        p2.afancymethod();

    }

}

A.两个均通过编译,P2运行时输出 What a fancy method

B.没一个通过编译

C.两个均通过编译,但P2运行时出错

DP1 通过编译,但P2出现编译错误

10)以下程序的调试结果为?

public class Outer{

public String name = "Outer";

public static void main(String argv[]){

     Inner i = new Inner();

     i.showName();

}

private class Inner{

    String name =new String("Inner");

    void showName(){

          System.out.println(name);

    }

  }

}

A.输出结果 Outer

B.输出结果 Inner

C.编译错误,因Inner类定义为私有访问

D.在创建Inner类实例的行出现编译错误

11) 设有如下代码:

class Base{}

public class MyCast extends Base{

    static boolean b1=false;

    static int i = -1;

    static double d = 10.1;

    public static void main(String argv[]){

        MyCast m = new MyCast();

        Base b = new Base();

        //Here

    }

}

则在 //Here处插入哪个代码将不出现编译和运行错误。

Ab=m;   Bm=b;   Cd =i;   Db1 =i;

12)  设有如下代码:

interface IFace{}

class CFace implements IFace{}

class Base{}

public class ObRef extends Base{

    public static void main(String argv[]){

        ObRef obj = new ObRef();

        Base b = new Base();

        Object obj1 = new Object();

        IFace obj2 = new CFace();

//Here

    }

}

则在 //Here处插入哪个代码将不出现编译和运行错误。

Aobj1=obj2;   Bb=obj;   Cobj=b;  Dobj1=b;

13) 设有类定义如下:

class Base{

public Base(int i){}

}

public class MyOver extends Base{

public static void main(String arg[]){

     MyOver m = new MyOver(10);

}

MyOver(int i){

     super(i);

}

MyOver(String s, int i){

      this(i);

      //Here

}

}

以下哪条语句可以安排在//Here ?

AMyOver m = new MyOver();

Bsuper();

Cthis("Hello",10);

DBase b = new Base(10);

14) 设有类定义如下:

class InOut{

String s= new String("Between");

public void amethod(final int iArgs){

    int iam;

    class Bicycle{

        public void sayHello(){

            //Here

        }

     }

 }

 public void another(){

     int iOther;

 }

}

以下哪些语句可以安排在//Here ?

A. System.out.println(s);

BSystem.out.println(iOther);

C. System.out.println(iam);

D.     System.out.println(iArgs);

九、常用系统类

1) 关于以下程序段,正确的说法是

1  String  s1="Hello";

2  String  s2="Hello";

3  ifs1= =s2

4      System.out.println("s1= =s2");

5  if (s1.equals(s2))

6  System.out.println("s1.equals(s2) ");

A.  4与行6都将执行

B.  4执行,行6不执行

C.  6执行,行4不执行

D.  4、行6都不执行

2) 要产生[20999]之间的随机整数使用哪个表达式?

A(int)(20+Math.random()*979)

B. 20+(int)(Math.random()*980)

C. (int)Math.random()*999

D. 20+(int)Math.random()*980

3) 下列程序运行的结果为:

public class Example{

  String str=new String("good");

  char[] ch={'a','b','c'};

  public static void main(String args[]){

    Example ex=new Example();

    ex.change(ex.str,ex.ch);

    System.out.print(ex.str+" and ");

    Sytem.out.print(ex.ch);

  }

  public void change(String str,char ch[]){

    str="test ok";

    ch[0]='g';

  }

}

A.  good and abc

B.  good and gbc

C.  test ok and abc

D.  test ok and gbc

4) 设有如下程序

public class test {

   public static void main(String args[]) {

      Integer intObj=Integer.valueOf(args[args.length-1]);

      int i = intObj.intValue();

      if(args.length > 1)

         System.out.println(i);

      if(args.length > 0)

         System.out.println(i - 1);

      else

         System.out.println(i - 2);

   }

}

运行程序,输入如下命令:

   java test 2

则输出为:

A. test      B. test -1     C. 0

D. 1         E. 2

5) 下列程序运行的结果为:

public class test {

  public static void main(String args[]) {

    int i;

    float  f = 2.3f;

    double d = 2.7;

    i = ((int)Math.ceil(f)) * ((int)Math.round(d));

    System.out.println(i);

  }

}

A. 4       B. 5       C.  6

D. 6.1     E. 9

6)如果以下条件成立,则用到java.lang.Math 类中哪个方法?

method( -4.4 ) == -4;

A. round()    B. min()   C. trunc()    D. abs()

E. floor()    F. ceil()

7) set集合如何处理重复元素

A.如果加入一个重复元素将抛出异常

B.如果加入一个重复元素add方法将返回false

C. 集合通过调用equals方法可以返回包含重复值的元素。

D. 重复值将导致编译出错。

8) 以下哪个方法是Vector类中增加一个新元素的方法。

AaddElement    B. insert    C. append   D. addItem

9) 以下哪些方法是Collection 接口的方法?

A. iterator    B. isEmpty    C. toArray    D. setText

原创粉丝点击