java第五章练习题(继承)

本文是一篇关于Java编程中继承特性的练习题集,涵盖了继承的优点、子类调用父类方法、方法覆盖、不可继承类的声明、对象转换等核心概念。通过解答这些问题,读者可以深入理解Java继承的本质和应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第五章练习题(继承)

 

    1.下列关于继承优点的叙述正确的是哪几项?(选三项)

    A.可以创建更为特殊的类型

    B.消除重复代码

    C.执行效率高

    D.便于维护

    

2.在子类中调用父类中被覆盖的方法时需要使用哪项关键字?

    A.this

    B.super

    C.new

    D.以上都不是

    

3.现有

    public class Parentt

    public void change (int x){)

    )

    public class Child extends Parent{

    //覆盖父类change方法

    }

    下列哪个声明是正确的覆盖了父类的change方法?

    A.  protected void change (int x){}

    B.  public void change(int x,  int y){}

    C.  public void change (int x){}

    D.  public void change (String s){}

    

4.如果想要一个类不能被任何类继承的话,需要使用哪个关键字来修饰该类?

    A.  abstract

    B.  new

    C.  static

    D.  final

 

    5.为了使得System.out.println()输出对象引用的时候得到有意义的信

    我们应该覆盖Object t{-的哪个方法?

    A.equals

    B.toString

    C.hashCode

    D.notify

    

6.现有:

    public class Pet()

    public class Cat extends Pet{)

    执行代码

    Cat c- new Cat();

    Pet p=  (Pet)c;

    后下列哪项是正确的?

    A. Pet p=(Pet)c运行错误

    B. Pet p=(Pet)c编译错误

    C. Pet p= (Pet)c止常执行

    D.以上都不对

    

7.程序:

    public class Pet{

    public void speak(){

    System.out.print("pet");

    }

    }

    public class Cat extends Pet{

    pulolic void  speak(){

    System.out.print("Cat");

    }

    }

    public class Dog extends Pet(

    public  void  speak(){

    System.out.print("Dog");

    }

    }

    执行代码

    Pet[]  p=  {new Cat (),new Dog(),nex Pet()};

    for (int i=O;i<p.length;i++)

    p[i] .speak();

 

    后输出的内容是哪项?

    B. Cat Cat Cat

    C. Cat Dog Dog

    D. Cat Dog Pet

    

8.现有:

    1.  class Dog{  )

    2.  class Harrier extends Dog  {  )

    3.

    4.  class DogTest{

    5.public  static void main (String  []  args)  (

    6.    Dog dl=new Dog();

    7.Harrier hl=new Harrier();

    8.    Dog d2=hl;

    9.Harrier h2=  (Harrier) d2;

    10.    Harrier h3=d2;

    11.    }

    12.  }

    下面哪一项是正确的?

    A.2个Dog对象被创建

    B.2个Harrier对象被创建

    C.3个Harrier对象被创建

    D.编译失败

    

9.现有:

    2.  class Cat  {

    3.Cat (int c)  {System.out.print {"cat"+c+" ");  }

    4.    }

    5.  class SubCat extends Cat  {

    6.SubCat (int c){super (5); System.out.print ("cable");}

    7.    SubCat()  {  this (4);  }

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

    9.SubCat s= new SubCat();

    10.    }

    11.  }

    结果为:

    A. cat5

    B. cable

    C. cat5 cable

    D. cable cat5

 

 

10.现有:

1. class Guy {String greet()    {return "hi";  }  }

2.  class Cowboy extends Guy  (  String greet()    (  return "howdy  ¨;    )  )

3.  class Surfer extends Guy  (String greet()    (return "dude! "; }  }

4.

5.  class Greetings  {

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

7.Guy  []  guys =  ( new Guy(), new Cowboy(), new Surfer()  };

8.    for (Guy g:  guys)

9.System.out.print (g.greet()};

10.    }

11.  }

    结果为:

    A. hi howdy dude!

    B.运行时异常被抛出。

    c.第7行出现一个错误,编译失败。

    D.第8行出现一个错误,编译失败。

   

 11.现有:

    1.  class Over  {

    2.int dolt (long x)  {  return 3;  }

    3.  }

    4.

    5. class Under extends Over  {

    6.    //insert code here

    7.  }

    和四个方法:

    short dolt (int y)  {  return 4;  }

    int dolt(long Xr long y)  {  return 4;  }

    private int dolt(short y)  {  return 4;  }

    protected int dolt (long x)  {  return 4;  }

    分别插入到第6行,有几个可以通过编译?

    A.  1

    B.  2

    C.  3

    D.  4

 

    12现有

      1.  class Beverage {

      2.     Beverage ()  {  System.out.print ("beverage ");  }

      3.  }

      4.  class Beer extends Beverage {

      5.   public static void main{string [] args) {

      6.        Beer b = new Beer (14) ;

      7.   }

      8.   public int Beer(int x) {

      9.       this () ;

      10.      System.out.print ("beerl") ;

      11.   }

      12.  public Beer() { System.out.print("beer2 "); }

       13. }

    结果是什么?

     A. beerl beverage

     B. beer2 beverage

     C. beverage beer2 beerl

     D.编译失败

      

     13.现有:

       1.  class Bird {

       2.   void talk() { System.out.print("chirp "); }

       3.  }

       4.  class Parrot2 extends Bird {

       5.   protected void talk() { System.out.print("hello ");

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

       7.      Bird [] birds = {new Bird(), new Parrot2 () };

       8.     for( Bird b : birds)

        9.          b.talk () ;

        10.   }

        11. }

    结果是什么 ?

     A. chirp chirp

     B. hello hello

     C. chirp hello

     D:编译错误

 

  14.现有:

    1.class SuperFoo(

    2.SuperFoo doStuff (int x)  {

    3.return new SuperFoo();

    4.    }

    5.  }

    6.

    7. class Foo extends SuperFoo  {

    8.    //insert code here

    9.  }

  和四个声明:

    Foo doStuff (int x)  {  return new Foo();  }

    Foo doStuff (int x)  {  return new SuperFoo();  }

    SuperFoo doStuff(int x)  {  return new Foo();  }

    SuperFoo doStuff(int y)  {  return new SuperFoo();  }

  分别插入到第8行,有几个可以通过编泽?

   A.  1

   B.  2

   C.  3

   D. 4

  

15.现有:

    1.  class HorseRadish  {

    2.    //insert code here

    3.protected HorseRadish (int x)    {

    4.System.out.println ("bok choy");

    5.    }

    6.    }

    7. class Wasabi extends HorseRadish  {

    8.public static void main (String  []  args)    (

    9.Wasabi w- new Wasabi();

    10,    }

    11.  }

    分别插入到第2行,哪两项允许代码编译并产生”bok choy”输出结果?(选

    两项)

    A. protected HorseRadish()  {this (42);}

    B.  protected HorseRadish()  {}

    C.  //just a comment

    D.  protected  HorseRadish()  {  new HorseRadish (42);}

 

参考答案

                                                                      .

   1ABD

   2B

   3C

   4D

   SB

   6A

   7D

   8D

   9C

   10 A

   11 D

   12 D

   13 C

   14 D

   15 AD

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值