面向对象编程题目.docx

上传人:b****6 文档编号:7037143 上传时间:2023-01-16 格式:DOCX 页数:14 大小:17.89KB
下载 相关 举报
面向对象编程题目.docx_第1页
第1页 / 共14页
面向对象编程题目.docx_第2页
第2页 / 共14页
面向对象编程题目.docx_第3页
第3页 / 共14页
面向对象编程题目.docx_第4页
第4页 / 共14页
面向对象编程题目.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

面向对象编程题目.docx

《面向对象编程题目.docx》由会员分享,可在线阅读,更多相关《面向对象编程题目.docx(14页珍藏版)》请在冰豆网上搜索。

面向对象编程题目.docx

面向对象编程题目

面向对象编程

(1)

GiventhefollowingJavacode:

classAnimal{publicStringnoise(){return"peep"}}

classDogextendsAnimal{

publicStringnoise(){return"back";}

}

classCatextendsAnimal{

publicStringnoise(){return"move";}

}

...

Animalanimal=newDog();

Catcat=(Cat)animal;

System.out.printIn(cat.noise());

Whatistheresult?

A.peep

B.back

C.move

D.Compilationfails.

E.Anexceptionisthrownatruntime

(2)

GiventhefollowingJavacode:

10.

11.

12.

13.

14.

15.

16.

interfaceA{publicintgetValue();}

classBimplementsA{

publicintgetValue(){return1;}

}

classCextendsB{

//insertcodehere

}

Whatthreecodefragmentsinsertedindividuallyatline15,makeusedofpolymorphism?

(choosethree)

A.publicvoidadd(Cc){c.getValue();}

B.publicvoidadd(Bb){b.getValue();}

C.publicvoidadd(Aa){a.getValue();}

D.publicvoidadd(Aa,Bb){a.getValue();}

E.publicvoidadd(Cc1Cc2){c1.getValue();}

(3)

AddmethodstotheBetaclasstomakeitcompilecorrectly.

classAlpha{

publicvoidbar(int…x){}

publicvoidbar(intx){}

}

publicclassBetaextendsAlpha{

placehere

placehere

placehere

}

Methods

privatevoidbar(intx){}

publicvoidbar(intx){}

publicintbar(Stringx){return1;}

publicAlphabar(intx){}

publicvoidbar(intx,inty){}

publicintbar(intx){returnx;}

(4)

GiventhefollowingJavacode:

classTest1{

publicTest1foo(){returnthis;}

}

classTest2extendsTest1{

publicTest1foo(){returnthis;}

}

classTest3extendsTest2{

//insertmethodhere

}

Whichtwomethods,insertedindividually,correctlycompletetheTest3class?

(choosetwo)

A.publicvoidfoo(){}

B.publicintfoo(){return3;}

C.publicTest2foo(){returnthis;}

D.publicTest1foo(){returnthis;}

(5)

GiventhefollowingJavacode:

publicclassBootchy{

intbootch;

Stringsnootch;

publicBootchy(){

this("snootchy");

System.out.print("first");

}

publicBoothchy(Stringsnootch){

this(420,"snootchy");

System.out.print("second");

}

publicBootchy(intbootch,Stringsnootch){

this.bootch=bootch;

this.snootch=snootch;

System.out.print("third");

}

publicstaticvoidmain(String[]args){

Bootchyb=newBootchy();

System.out.print(b.snootch+""+b.bootch);

}

}

Whatistheresult?

(运行结果)

(6)

GiventhefollowingJavacode:

09.

10.

11.

12.

13.

14.

15.

16.

17.

18.

19.

20.

21.

publicclassTest{

staticclassA{

voidprocess()throwsException{thrownewException();}

}

staticclassBextendsA{

voidprocess(){System.out.println("B");}

}

publicstaticvoidmain(String[]args){

Aa=newB();

a.process();

}

}

Whatistheresult?

A.B

B.Thecodeexceptionisthrownatruntime(执行时抛出exception)

C.Thecoderunwithnooutput(沒有任何输出)

D.Compilationfailsbecauseofanerrorinline15.

E.Compilationfailsbecauseofanerrorinline18.

F.Compilationfailsbecauseofanerrorinline19.

(7)

GiventhefollowingJavacode:

10

11.

12.

13.

14.

15.

16.

17.

18.

interfaceFoo{}

classAlphaimplementsFoo{}

classBetaextendsAlpha{}

classDeltaextendsBeta{

publicstaticvoidmain(String[]args){

Betax=newBeta();

//insertcodehere

}

}

Whichcode,insertedatline16willcauseajava.lang.ClassCastException?

A.Alphaa=x;

B.Foof=(Delta)x;

C.Foof=(Alpha)x;

D.Betab=(Beta)(Alpha)x;

(8)

GiventhefollowingJavacode:

publicclassPerson{

privateStringname,comment;

privateintage;

publicPerson(Stringn,inta,Stringc){

name=n;age=a;comment=c;

}

publicBooleanequals(Objecto){

if(!

(oinstanceofPerson))returnfalse;

Personp=(Person)o;

returnage==p.age&&name.equals(p.name);

}

}

WhatistheappropriateddefinitionofthehashCodemethodinclassPerson?

A.returnsuper.hashCode();

B.returnname.hashCode()+age*7;

C.returnnamehashcode()+comment.hashCode()/2;

D.returnname.hashCode()+comment.hashCode()/2-age*3;

(9)

Giventhefollowingcode:

01.

02.

03.

04.

05.

06.

07.

publicclassPerson{

privateStringname;

publicPerson(Stringname){this.name=name;}

publicBooleanequals(Personp){

returnp.name.equals(this.name);

}

}

Whichstatementistrue?

A.TheequalsmethoddoesNOTproperlyoverridetheobjectObject.equalsmethod.

B.Compilationfailsbecausetheprivateattributep.namecannotbeaccessedinline5.

C.Toworkcorrectlywithhash-baseddatastructures,thisclassmustalsoimplementthehashCodemethod.

D.WhenaddingPersonobjectstojava.util.Setcollection,theequalsmethodinline4willpreventduplicates.

(10)

PlacetheOutputOptionsintheActualOutputSequencetoindicatetheoutputfromthiscode:

classAlpha{

publicvoidfoo(String...args){

System.out.print("Alpha:

foo");

}

publicvoidbar(Stringa){

System.out.print("Alpha:

bar");

}

}

publicclassBetaextendsAlpha{

publicvoidfoo(Stringa){

System.out.print("Beta:

foo");

}

publicvoidbar(Stringa){

System.out.print("Beta:

bar");

}

publicstaticvoidmain(String[]args){

Alphaa=newBeta();

Betab=(Beta)a;

a.foo("test");

b.foo("test");

a.bar("test");

b.bar("test");

}

}

ActulOutputSequence:

Placehere

Placehere

Placehere

Placehere

OutputOption:

Alpha:

foo

Alpha:

bar

Beta:

foo

Beta:

bar

(11)

GiventhefollowingJavacode:

1.

2.

3.

publicinterfaceA{

publicvoiddoSomething(Stringthing);

}

1.

2.

3.

publicclassAImp1implementsA{

publicvoiddoSomething(Stringmsg){}

}

1.

2.

3.

4.

5.

6.

7.

8.

9.

publicclassB{

publicAdoit(){

//morecodehere

}

publicStringexecute(){

//morecodehere

}

}

1.

2.

3.

4.

5.

6.

7.

8.

9.

publicclassCextendsB{

publicAImp1doit(){

//morecodehere

}

publicObjectexecute(){

//morecodehere

}

}

Whichstatementistrueabouttheclassandinterfacesinthis"Code"?

A.Compilationwillsucceedforallclassesandinterfaces.

B.CompilationofclassCwillfailbecauseofanerrorintline2.

C.CompilationofclassCwillfailbecauseofanerrorintline6.

D.CompilationofAImplwillfailbecauseofanerrorinline2.

(12)

GiventhefollowingJavacode:

classA{

Stringname="A";

StringgetName(){

returnname;

}

Stringgreeting(){

return"classA";

}

}

classBextendsA{

Stringname="B";

Stringgreeting(){

return"classB";

}

}

publicclassClient{

publicstaticvoidmain(String[]args){

Aa=newA();

Ab=newB();

System.out.println(a.greeting()+"hasname"+a.getName());

System.out.println(b.greeting()+"hasname"+b.getName());

}

}

Placethenames"A"and"B"inthefollowingoutput.

ClassPlaceherehasnamePlacehere

ClassPlaceherehasnamePlacehere

Names:

A

B

(13)

GiventhefollowingJavacode:

1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

classSuperClass{

publicAgetA(){

returnnewA();

}

}

classSubClassextendsSuperClass{

publicBgetA(){

returnnewB();

}

}

Whichstatementistrue?

A.CompilationwillsucceedifAextendsB

B.CompilationwillsucceedifBextendsA

C.Compilationwillalwaysfailbecauseofanerrorinline7

D.Compilationwillalwaysfailbecauseofanerrorinline8

(14)

GiventhefollowingJavacode:

1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

12.

13.

14.

15.

classPizza{

java.util.ArrayListtoppings;

publicfinalvoidaddTopping(Stringtopping){

toppings.add(topping);

}

}

publicclassPepperoniPizzaextendsPizza{

publicvoidaddTopping(Stringtopping){

System.out.println("CannotandUoppings");

}

publicstaticvoidmain(String[]args){

Pizzapizza=newPepperoniPizza();

Pizza.addTopping("Mushrooms");

}

}

Whatistheresult?

A.Compilationfails

B.CannotandUoppings

C.Thecoderunswithnooutput

D.ANullPointerExceptionisthrowninLine4

(15)

GiventhefollowingJavacode:

10.

11.

12.

13.

14.

15.

16.

17.

18.

19.

interfaceA{

voidx();

}

classBimplementsA{

publicvoidx(){}

publicvoidy(){}

}

classCextendsB{

publicvoidx(){}

}

And:

20.

21.

22.

23.

24.

25.

26.

java.util.Listlist=newjava.util.ArrayList();

list.add(newB());

list.add(newC());

for(Aa:

list){

a.x();

a.y();

}

Whatistheresult?

A.Thecoderunswithnooutput

B.Anexceptionthrownatruntime

C.Compilationfailsbecauseofanerrorinline20

D.Compilationfailsbecauseofanerrorinline21

E.Compilationfailsbecauseofanerrorinline23

F.Compilationfailsbecauseofanerrorinline25

(16)

GiventhefollowingJavacode:

10.

11.

12.

13.

14.

15.

classOne{

voidfoo(){}

}

classTwoextendsOne{

//insertmethodhere

}

Whichthreemethods,insertedindividuallyatline14willcorrectlyclassTwo?

(Choosethree)

A.intfoo(){/*morecodehere*/}

B.voidfoo(){/*morecodehere*/}

C.publicvoidfoo(){/*morecodehere*/}

D.privatevoidfoo(){/*morecodehere*/}

E.protectedvoidfoo(){/*morecodehere*/}

(17)

WhichtwostatementsaretrueaboutthehashCodemethod?

(Choosetwo)

A.ThehashCodemethodforagivenclasscanbeusedtotestforobjectequalityandobjectinequalityforthatclass.(hashCodemethod

B.ThehashCodemethodisusedbythejava.util.SortedSetcollectionclasstoordertheementswithinthatset.

C.ThehashCodemethodforagivenclasscanbeusedtotestforobjectinequality,butNOTobjectequalityforthatclass.

D.TheonlyimportantcharacteristicofthevaluesreturnedbyahashCo

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 职业教育 > 其它

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1