尾部都有答案
第五章练习题(继承)
(1)下列关于继承优点的叙述正确的是哪几项?(选三项)
A.可以创建更为特殊的类型
B.消除重复代码
C.执行效率高
D.便于维护
(2)在子类中调用父类中被覆盖的方法时需要使用哪项关键字?
A.this
B.super
C.new
D.以上都不是
(3)现有
public class Parent{
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)现有
class Dog{ )
class Harrier extends Dog { )
class DogTest{
public static void main (String [] args) (
Dog dl=new Dog();
Harrier hl=new Harrier();
Dog d2=hl;
Harrier h2= (Harrier) d2;
Harrier h3=d2;
}
}
下面哪一项是正确的?
A. 2个Dog对象被创建
B. 2个Harrier对象被创建
C. 3个Harrier对象被创建
D. 编译失败
(9)现有
class Cat {
Cat (int c) {System.out.print {"cat"+c+" "); }
}
class SubCat extends Cat {
SubCat (int c){super (5); System.out.print ("cable");}
SubCat() { this (4); }
public static void main (String [] args) {
SubCat s= new SubCat();
}
}
结果为
A. cat5
B. cable
C. cat5 cable
D. cable cat5
(10)现有
class Guy {String greet() {return "hi"; } }
class Cowboy extends Guy ( String greet() ( return "howdy ¨; ) )
class Surfer extends Guy (String greet() (return "dude! "; } }
class Greetings {
public static void main (String [] args) {
Guy [] guys = ( new Guy(), new Cowboy(), new Surfer() };
for (Guy g: guys)
System.out.print (g.greet()};
}
}
结果为
A. hi howdy dude!
B. 运行时异常被抛出。
C. 第7行出现一个错误,编译失败。
D. 第8行出现一个错误,编译失败。
(11)现有
class Over {
int dolt (long x) { return 3; }
}
class Under extends Over {
//insert code here
}
//四个方法
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)现有
class Beverage {
Beverage () { System.out.print ("beverage "); }
}
class Beer extends Beverage {
public static void main{string [] args) {
Beer b = new Beer (14) ;
}
public int Beer(int x) {
this () ;
System.out.print ("beerl") ;
}
public Beer() { System.out.print("beer2 "); }
}
结果是什么?
A. beerl beverage
B. beer2 beverage
C. beverage beer2 beerl
D.编译失败
(13)现有
class Bird {
void talk() { System.out.print("chirp "); }
}
class Parrot2 extends Bird {
protected void talk() { System.out.print("hello ");
public static void main(String [] args) {
Bird [] birds = {new Bird(), new Parrot2 () };
for( Bird b : birds)
B.talk();
}
}
结果是什么 ?
A. chirp chirp
B. hello hello
C. chirp hello
D. 编译错误
(14)现有
class SuperFoo(
SuperFoo doStuff (int x) {
return new SuperFoo();
}
}
class Foo extends SuperFoo {
//insert code here
}
//四个声明:
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)现有
class HorseRadish {
//insert code here
protected HorseRadish (int x) {
System.out.println ("bok choy");
}
}
class Wasabi extends HorseRadish {
public static void main (String [] args) (
Wasabi w- new Wasabi();
}
}
分别插入到第2行,哪两项允许代码编译并产生”bok choy”输出结果?(选两项)
A. protected HorseRadish() {this (42);}
B. protected HorseRadish() {}
C. //just a comment
D. protected HorseRadish() { new HorseRadish (42);}
参考答案
1、ABD
2、B
3、C
4、D
5、B
6、A
7、D
8、D
9、C
10、A
11、D
12、D
13、C
14、D
15、AD