尾部都有答案
第五章練習(xí)題(繼承)
(1)下列關(guān)于繼承優(yōu)點(diǎn)的敘述正確的是哪幾項(xiàng)沟突?(選三項(xiàng))
A.可以創(chuàng)建更為特殊的類(lèi)型
B.消除重復(fù)代碼
C.執(zhí)行效率高
D.便于維護(hù)
(2)在子類(lèi)中調(diào)用父類(lèi)中被覆蓋的方法時(shí)需要使用哪項(xiàng)關(guān)鍵字花颗?
A.this
B.super
C.new
D.以上都不是
(3)現(xiàn)有
public class Parent{
public void change (int x){)
)
public class Child extends Parent{
//覆蓋父類(lèi)change方法
}
下列哪個(gè)聲明是正確的覆蓋了父類(lèi)的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)如果想要一個(gè)類(lèi)不能被任何類(lèi)繼承的話惠拭,需要使用哪個(gè)關(guān)鍵字來(lái)修飾該類(lèi)扩劝?
A. abstract
B. new
C. static
D. final
(5)為了使得System.out.println()輸出對(duì)象引用的時(shí)候得到有意義的,我們應(yīng)該覆蓋Object t{-的哪個(gè)方法片橡?
A.equals
B.toString
C.hashCode
D.notify
(6)現(xiàn)有
public class Pet()
public class Cat extends Pet{)
//執(zhí)行代碼
Cat c- new Cat()奏司;
Pet p= (Pet)c;
后下列哪項(xiàng)是正確的?
A. Pet p=(Pet)c運(yùn)行錯(cuò)誤
B. Pet p=(Pet)c編譯錯(cuò)誤
C. Pet p= (Pet)c止常執(zhí)行
D. 以上都不對(duì)
(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");
}
}
//執(zhí)行代碼
Pet[] p= {new Cat (),new Dog(),nex Pet()};
for (int i=O;i<p.length;i++)
p[i] .speak()幸缕;
后輸出的內(nèi)容是哪項(xiàng)悔详?
B. Cat Cat Cat
C. Cat Dog Dog
D. Cat Dog Pet
(8)現(xiàn)有
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;
}
}
下面哪一項(xiàng)是正確的蒲凶?
A. 2個(gè)Dog對(duì)象被創(chuàng)建
B. 2個(gè)Harrier對(duì)象被創(chuàng)建
C. 3個(gè)Harrier對(duì)象被創(chuàng)建
D. 編譯失敗
(9)現(xiàn)有
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();
}
}
結(jié)果為
A. cat5
B. cable
C. cat5 cable
D. cable cat5
(10)現(xiàn)有
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()};
}
}
結(jié)果為
A. hi howdy dude!
B. 運(yùn)行時(shí)異常被拋出拆内。
C. 第7行出現(xiàn)一個(gè)錯(cuò)誤旋圆,編譯失敗。
D. 第8行出現(xiàn)一個(gè)錯(cuò)誤麸恍,編譯失敗灵巧。
(11)現(xiàn)有
class Over {
int dolt (long x) { return 3; }
}
class Under extends Over {
//insert code here
}
//四個(gè)方法
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行,有幾個(gè)可以通過(guò)編譯抹沪?
A. 1
B. 2
C. 3
D. 4
(12)現(xiàn)有
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 "); }
}
結(jié)果是什么?
A. beerl beverage
B. beer2 beverage
C. beverage beer2 beerl
D.編譯失敗
(13)現(xiàn)有
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();
}
}
結(jié)果是什么 ?
A. chirp chirp
B. hello hello
C. chirp hello
D. 編譯錯(cuò)誤
(14)現(xiàn)有
class SuperFoo(
SuperFoo doStuff (int x) {
return new SuperFoo();
}
}
class Foo extends SuperFoo {
//insert code here
}
//四個(gè)聲明:
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行融欧,有幾個(gè)可以通過(guò)編澤敏弃?
A. 1
B. 2
C. 3
D. 4
(15)現(xiàn)有
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行,哪兩項(xiàng)允許代碼編譯并產(chǎn)生”bok choy”輸出結(jié)果噪馏?(選兩項(xiàng))
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