SCJP認(rèn)證考試題庫1_第1頁
SCJP認(rèn)證考試題庫1_第2頁
SCJP認(rèn)證考試題庫1_第3頁
SCJP認(rèn)證考試題庫1_第4頁
SCJP認(rèn)證考試題庫1_第5頁
已閱讀5頁,還剩33頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

?考試大綱

?第一部分:Java語言基礎(chǔ)

?第二部分:流程控制

?第三部分:面向?qū)ο蠓庋b,繼承,多態(tài)

?第四部分:異常處理

?第五部分:多線程

?第六部分:Java垃圾回收機(jī)制

?第七部分:JavaI/O

?第八部分:Java集合和泛型

?第九部分:JavaSE實(shí)用API

QUESTION1

Giventhecodeintheexhibit.

Whatistheresult?

importjava.io.*.;

13.publicclassForestimplementsSerializable{

14.privateTreetree=newTree();

15.publicstaticvoidmain(String[|args)(

16.Forestf=newForest();

17.try(

18.FileOutputStreamfs■newFileOutputStream(uForest.Ser*1);

19.ObjectOutputStreamOS工newObjectOutputStream(fs);

20.os.writeObject(f);OS.Close();

21}catch(Exceptionex){ex.PrintStackTrace();}

22

23..

24.classTree[)

A.Compilationfails

B.Anexceptionisthrownatnantime.

C.AninstanceofForestisserialized.

D.AninstanceofForestandaninstanceofTreearebothserialized.

Answer:(B)

執(zhí)行時(shí)期會(huì)拋出java.io.NotSerializableExcetpion異常。Tree必須實(shí)現(xiàn)Serialized接口;因?yàn)镕orest實(shí)現(xiàn)了序列化,并

且引用了Tree,但是Tree沒有實(shí)現(xiàn)序列化!!

當(dāng)一個(gè)實(shí)現(xiàn)序列化的類在類體里調(diào)用另外一個(gè)類的時(shí)候,那么另外一個(gè)類也要實(shí)現(xiàn)序列化!如果沒有實(shí)現(xiàn),則會(huì)報(bào)出運(yùn)行

時(shí)異常!!如果要實(shí)現(xiàn)序列化,他的成員變量也必須實(shí)現(xiàn)序列化.本題中Tree沒有實(shí)現(xiàn)序列化,所以會(huì)產(chǎn)生

java.i。,NotSerializableException的運(yùn)行異常!

參考大綱:IO操作一對(duì)象的序列化

序列化的過程就是對(duì)象寫入字節(jié)流和從字節(jié)流中讀取對(duì)象。見SCJP.ul.SerializableTest

QUESTION2

Whichcode,insertedatline14,willallowthisclasstocorrectlyserializedand

desterilized?

1laport^ava10?:

2publicclassFooi?pleaentsSerializable{

3.publicintx.y:.

4.publicFoo(intx.inty){this.x-x,thisy■y;}

6privatevoidvriteOboect(ObjectOutputStreams)

7throwslOException(

8svritelnt(x);svritelnt(y),

9.)

10.

11privatevoidreadObject(ObiectInputStreams)

12throwslOException.ClassNotFoundException{

13

14//insentcodehere

15

16,}

17.}

A.s.defaultReadObject();

B.this=s.defaultReadObject();

C.y=s.default();x=s.readlnt();

D.x=s.readlnt();y=s.readlnt();

Answer:(D)

在反序列化方法中,從s對(duì)象中讀取兩個(gè)整數(shù).序列化是寫對(duì)象,反序列化是讀對(duì)象…

參考大綱:IO操作一對(duì)象的序列化

QUESTION3

Giventheexhibit.

11Sringtest="Thisisatest”;

12String[]tokens=testsplit(As**),

13Systemoutprintln(tokenslengyh),

Whatistheresult?

A.0

B.1

C.4

D.Compilationfails

E.Anexceptionisthrownatruntime

Answer:(D)產(chǎn)生illegalescapecharacter非法轉(zhuǎn)意符的編譯錯(cuò)誤

split。字符切割器

本題是想用空格來分割字符串,只能用“”或者“\\S”來分割,“\s”沒有這個(gè)轉(zhuǎn)意字符!所以會(huì)報(bào)編譯錯(cuò)誤……

tab可以用“\匕可以用"\\”表示.

String的split方法用來分割字符串,這個(gè)方法接受一個(gè)正則表達(dá)式,根據(jù)表達(dá)式來分割,“\\s”表示空格,“\s”沒有這

個(gè)轉(zhuǎn)意字符,所以會(huì)產(chǎn)illegalescapecharacter的編譯錯(cuò)誤。

參考大綱:實(shí)用API—String的split。方法和正則表達(dá)式

QUESTION4

Giventheexhibit:

12Datedate=newDate();

13dfsetLoeale(LccalLdy);

14Strings=dfFormat(date).

ThevariabledfisanobjectoftypeDateFormatthathasbeeninitializedinline11.

WhatistheresultifthiscodeisrunonDecember14,2000?

A.ThevalueofSis14-dic-2004

B.ThevalueofSisDec14,2000

C.Anexceptionisthrownatruntime

D.Compilationfailsbecauseofanerrorinline13.

Answer:(D)

DateFormat用來格式日期,它放在java.text包里,它沒有setLocale方法,Local.laly應(yīng)該為L(zhǎng)ocale.ITALY.代碼語法

有問題”編譯錯(cuò)誤!

參考大綱:實(shí)用API—java.util包和java.text包

QUESTION5

ThedoesFileExistmethodtakesanarrayofdirectorynamesrepresentingapath

fromtherootfilesystemandafilename.Themethodreturnstrueifthefileexists,

falseifdoesnot.

Placethecodefragmentsinpositiontocompletethismethod.

h?e

>

CodeFragments

teturn(lieM]

Fiepath?new曰5

returnpathi?fiteQ

Answer:()

publicstaticbooleandoesFileExist(String|]directories,Stringfilename){

Stringpath=HH;

for(Stringdir:directories){

path=path+File.separator+dir;

)

Filefile=newFilefpath,filename);

returnfile.exists。;

)

參考大綱:IO操作一File

QUESTION6

Given:

System.out.printf(HPiisapproximately%fandEisapproximately%b”,Math.PI,Math.E);

Placethevalueswheretheywouldappearintheoutput.

Piisapproxmately[Rrehgt"|

andEisapproximately|Rrt*

Values

Answer:()

3.141593

True................判斷E是否是NULL,NULL是FALSE否貝U是TRUE.

Pi=3.1415926.........

E=2.718282........

PrintfO是C中常用的語法;

%f表示浮點(diǎn)數(shù)(小數(shù)點(diǎn)后6位),

%b表示boolean,

%d表示整數(shù).

%e十進(jìn)制的科學(xué)計(jì)數(shù)法表示浮點(diǎn)數(shù)

%a16進(jìn)制表示浮點(diǎn)型科學(xué)計(jì)數(shù)法的整數(shù)部分,以10進(jìn)制表示指數(shù)

%0以8進(jìn)制表示整數(shù)

%x以16進(jìn)制表示整數(shù)

%s字符串個(gè)數(shù)輸出

%Cchar型格式輸出,提供的值應(yīng)為整數(shù)型

%t輸出日期時(shí)間的前置????

參考大綱:實(shí)用API—Formatter格式化輸出工具

QUESTION7

Whencomparingjava.io.BufferedWritertojava.io.FileWriter,whichcapability

existasamethodinonlyoneofthetwo?

A.closingthestream

B.flushingthestream

C.writingtothestream

D.markingalocationinthestream

E.writingalineseparatortothestream

Answer:(E)

只有BufferedWriter具有newLine()方法;Reader才有mark功能。

參考大綱:DO操作一BufferWriter和FileWriter

QUESTION8

Giventheexhibit:

Whichtwocodefragments,insertedindependentlyatline3,generatetheoutput

4247?(choosetwo)

1.publicclassCertkiller3(

2.publicstaticvoidmain(String[]args)(

3.//insertcodehere

5.Systemoutprintin(s);

6.)

7.)

A.Strings="123456789";

s.=(s-n123").replace(1,3,"24")-“89";//String中只有表示連接,但是無產(chǎn)生poerator?cannotbeappliedto

java.Jang.String的編譯錯(cuò)誤

B.StringBuffers=newStringBuffer(n123456789M);

s.delete(0,3).replace。,3,"24").delete(4,6);〃delete(0,3)表示從0下標(biāo)開始刪除到3下標(biāo)以前

C.StringBuffers=newStringBuffer(u123456789n);

s.substring(3,6).delete(l,3).insert(1,"24").Substring?;貍鞯氖且粋€(gè)String而不是StringBuffer,String沒有delete方

法,產(chǎn)生cannotfindsymbol的編譯錯(cuò)誤

D.StringBuilders=newStringBuilder("123456789");

s.substring(3,6)delete(1,2).insert(1,“24")錯(cuò)誤同上

E.StringBuilders=newStringBuilder(n123456789");

s.delete(0,3)replace(1,3;*').delete(2,5).insert(1,n24n)

Answer:(B,E)

A,String沒有運(yùn)算符;String不能修改!

B,正確4247

C,S.substring返回的是String,String沒有delete()方法

D,S.substring返回的是String

E,正確4247

參考大綱:實(shí)用API-

String、StringBuffer線程安全的適用于多線程、StringBuikler線程不安全適合單線程,但是性能高.

StringBufferStringBuilder的用法一樣

QUESTION9

Whichthreestatementsconcerninetheuseofthejava.io.Serialization

interfacearetrue?(choosethree)

A.Objectfromclassesthatuseaggregationcannotbeserialized.

B.AnobjectserializedononeJVNIcanbesuccessftiliydesterilizedonadifferentJVM.

C.ThexaluesinfieldswiththeVolatilemodifierwillNOTsurviveserializationand

deserialization

D.The\alliesinfieldwiththetransientmodifierwillNOTsurviveserializationand

deserialization

E.ItislegaltoserializeanobjectofahpethathasasupertypethatdoesNOTimplement

java.^.Serialization

Answer:(B,D,E)

A錯(cuò)誤,聚合中的對(duì)象實(shí)現(xiàn)了serializable就能被序列化一個(gè)類包含另外一個(gè)類就是聚合.A的描述和對(duì)象是否支持序

列化沒有直接關(guān)系

B正確java是跨平臺(tái)的,序列化的工作也統(tǒng)一交給各個(gè)平臺(tái)的jvm來處理

C錯(cuò)誤,不是volatile而是transient;有這個(gè)transient瞬態(tài)關(guān)鍵字的話,就不能序列化了!

Volatile這個(gè)修飾符的變量,實(shí)現(xiàn)的和多線程同步的方法類似,但是不是很好用!

D正確transient瞬態(tài)的對(duì)象是不支持序列化的

E正確,只要子類實(shí)現(xiàn)serializable,不用考慮父類有無實(shí)現(xiàn)serializable

參考大綱:IO操作一對(duì)象的序列化

QUESTION10

Giventheexhibit:

12.publicclassCertkiler{

13.publicstaticvoidgo(shortn)(SysemoutpnntlnCshorO,)

14.publicstaticvoidgo(Shortn)(Sysemoutpnntln^SHORT*);)

15.publicstaticvoidgo(Longa){2ysemout.printl^14LONG1*);)

16.publicstaticvoidmain(Stnng[]args)(

17.Shdrty=6;

18.mtz=7;

19.go(y);

20.go(i);

21.)

22.)

Whatistheresult?

A.shortLong

B.SHORTLONG

C.Compilationfails

D.Anexceptionisthrownatruntime

Answer:(C)向上就近原則.

第20行的go⑵將會(huì)依序找go(inti),go(longl),go(floatf),go(doubled)或go(Integeri)方法.但是并不會(huì)自動(dòng)向上轉(zhuǎn)型

Long然后再呼叫g(shù)o(Longn),這種依順序向上找的特性只會(huì)發(fā)生在對(duì)應(yīng)端基本資料型別的情況下,

參考大綱:面向?qū)ο笠恢剌d;實(shí)用API—自動(dòng)封包、拆包

QUESTION11

Giventheexhibit:

*disvalid,non-nullDateobject

*dfisavalid,non-nullDateFormatobjectsettothecurrentlocal

Whatoutputsthecurrentlocal'scountrynameandtheappropriateversionofD'sdate?

A.Localeloc=Locale.getLocal();

System.outprintin(loc.getDisplayCountry()

B.Localeloc=Locale.getDefault();

System.outprintin(loc.getDisplayCountry()+”"+df.fonnat(d));

C.Localeloc=Locale.getLocal();

System.outprintin(loc.getDisplayCountry()+”"+df.setDateFormat(d));

D.Localeloc=Locale.getDefault();

System.outprintin(loc.getDisplayCountry()+"”+df.seDateFormat(d));

Answer:(B)

ALocale類沒有g(shù)etLocal。方法,編譯錯(cuò)誤

B正確

C錯(cuò)誤Locale類沒有g(shù)etLocal()方法DateFormat沒有setDateFormat()方法,編譯錯(cuò)誤

DDateFormat沒有setDateFormat()方法編譯錯(cuò)誤

參考大綱:實(shí)用API—java.util包和java.text包

QUESTION12

Giventheexhibit:

1.publicclassCertkfller3implemeirsRunnable(

2.publicvoidrun()(

3.system.out.print("running");

4.)

5.publicstaticvoidmain(String:]args)(

6.Threadt=newThread(newCertkiller3());

7.t.run();

8.trun();

9.t.start();

10.)

11-)

Whatistheresult?

A.Compilationfails.

B.Anexceptionisthrownatruntime

C.Thecodeexecutesandprints”running"

D.ThecodeexecutesandprintsHrunningrunning"

E.Thecodeexecutesandprints'Yunningrunninigrunning

Answer:(E)

t.run()調(diào)用main主線程去執(zhí)行2-4行的run。,就是一次普通的方法調(diào)用;t.start()表示起用thread線程負(fù)責(zé)去執(zhí)行

2-4行的run();把2?4行的代碼改為:

publicvoidrun(){

StringthreadName=Thread.currentThread().getName();

System?out.println(threadName+”:running");

)

這樣就可以看出是誰調(diào)用了run()了,顯示如下:

main:running

main:running

Thread-o:running

參考大綱:多線程

QUESTION13------------仔細(xì)看看

Exhibit:

1.publicclassThreads1{

2.intx?0.

3.publicclassRunneriBpleaentsRunnable

4

5publicvoidrun(){

6intcurrent■0;

7for(inti■0;i<4.ii){

8current?x:

9Systeaoutprint(current+;

10x-current+2:

11

12

13

14

15publicstaticvoid?am(String(]args){

16newThreadsl()):

17

18

19publicvoidgo(){

20Runnablerl■newRunner();

21nevThread(rl).start().

22nevThread(rl)start();

23>

>

Whichtwoarepossibleresults?(choosetwo)

A.0,2,4,4,6,8,10,6,

B.0,2,4,6,8,10,2,4,

C.0,2,4,6,8,10,12,14,

D.0,0,224,4,6,6,8,8,10,10,12/2,14』4,

E.0,2,4,6,8,10,12,14,0,2,4,6,8,10,12,14,

Answer:(A,C)

A第一個(gè)線程循環(huán)到第三遍使得x等于4,并執(zhí)行完第8句后掛起(此時(shí)curnmt也是4);第二個(gè)線程開始執(zhí)行,執(zhí)

行完以后第一個(gè)線程接著執(zhí)行最后一次循環(huán),打印6

C兩個(gè)線程依次執(zhí)行

參考大綱:多線程

QUESTION14

Giventheexhibit:

7voidwaitForSignal()(

6objectobj=newObject();

7synchronized(ThreadcurrentThread())(

8objwait();

9objnotify();

10.)

11.}

Whichstatementistrue?

A.ThiscodemaythrowanInterruptedException

B.ThiscodemaythrowanIllegalStateExcepion

C.ThiscodemaythrowaTimeOutExceptionaftertenminutes

D.Thiscodewillnotcompileunless"obj.wait()\nisreplacedwith"((Thread)obj).wait()n

E.Reversingtheorderofobj.wait()andobj.notify()mayvcausethismethodtocompletenormally

Answer:(A)

首先編譯通不過,

第5行:voidwaltForSignalOthrowsInterruptedException{........}

第6行:Objectobj=newObject();

obj=Thread.currentThread();

第7行應(yīng)該是:synchronized(obj);

...在寫wait。和notify。方法時(shí),除了要寫在synchronized區(qū)段,.還需要撰寫InterruptedException異常的trj-catch,本

題就算寫了try-catch也可能會(huì)在執(zhí)行的時(shí)候產(chǎn)生currentthreadnotowner的IllegalMonitorStateException的異常.

參考大綱:多線程一同步處理

QUESTION15

Giventheexhibit:

1publicclassTestOneimplementsRunnable(

2.publicstaticvoidmain(String匚args)throwsException{

3Threadt=newThread(newTestOneO):

4.t.start(),

5System.out.print(^Started**);

6tjoin(X

7.System,out.print(<<Co!T.pleteM),

8)

9.publicvoidrun(){

10.for(intI=0;I<4;i-H-)(

11.Systemoutprint(i);

12.)

13.)

14.)

Whatcanbearesult?

A.Compilationfails

B.Anexceptionisthrownatruntime

C.ThecodeexecutesandprintsnStartedCompleteu

D.Thecodeexecutesandprints"StartedCompleteOl23u

E.Thecodeexecutesandprints"StartedO123CompIeten

Answer:(E)

Join()方法使得某個(gè)線程加入到正在執(zhí)行的線程(本題是main線程)中,執(zhí)行完該線程才繼續(xù)執(zhí)行main線程.本程

序中第5行由main線程執(zhí)行,6行因?yàn)橄逻_(dá)了join。,所以mian的執(zhí)行將被暫停,等t做完run。方法的全部工作之后才

論到main繼續(xù)執(zhí)行未完成的工作!

參考大綱:多線程

QUESTION16

WhichtwocodefragmentswillexecutethemethoddoStuff()inaseparatethread?

(choosetwo)

A.newThread(){

publicvoidrun(){doStuff();}

);

B.newThread(){

publicvoidstart(){doStuff();}

);

C.newThread(){

publicvoidrun(){doStuff();}

!.run();

D.newThread(){

publicvoidrun(){doStuff();)

}.start();

E.newThread(newRunable(){

publicvoidrun(){doStuff();}

}).run();

F.newThread(newRunnable(){

publicvoidrun(){doStuff();)

}).start();

Answer:(D>F)

D匿名類別中復(fù)寫run方法,并調(diào)用start。方法啟動(dòng)線程

F利用匿名實(shí)現(xiàn)ninnable接口中的run方法,并調(diào)用start。啟動(dòng)線程

參考大綱:多線程

QUESTION17

Whichthreewillcompileandrunwithoutexception?(choosethree)

A.privatesynchronizedobjecto;

B.voidgo(){

synchronized(){/*codehere*/}

)

C.publicsynchronizedvoidgo(){/*codehere*/}

D.privatesynchronized(this)voidgo(){/*codehere*/}

E.voidgo(){

synchronized(Object.class){/*codehere*/}

)

F.voidgo(){

synchronized(o){/*codehere*/}

Answer:(C,E,F)

A錯(cuò)誤synchronized不可以成為屬性麼量的修飾符

B錯(cuò)誤synchronized()中的括號(hào)中要加入欲鎖定的物件或類型〃synchronized(this)

C正確利用synchronized來修飾對(duì)象instance方法,鎖定的物件將會(huì)是this,

D錯(cuò)誤修飾方法時(shí),this不用特別在synchronized()中指明

E正確合法的classliteralssynchronized〃針對(duì)某個(gè)類同步

F正確合法的instanceblocksynchronized〃針對(duì)某個(gè)對(duì)象同步

參考大綱:多線程一線程同步

QUESTION18

Exhibit:

1

2classComputationextendsThread{

3

4privateintnua;

5privatebooleanisCoMplete.

6privateintresult:

7

8DublicCoMDutation(intnum){this.nua■mm;}

9

10publicsynchronizedvoidrun(){

11result-nua*2;

12isComplete-true;

13notify().

14)

15

16publicsynchronizedintgetResult(){

17while(IisCo?plete){

18try{

19wait();

-}catch(InterruptedExceptione){}

O

}

MC1

C2returnresult;

M}

MC3

o4

tpublicstaticvoidmain(Strinqf1arets){

Co?putation[]computations?nevComDutation[4];

for(inti■0,i<computations.length;i++)彳

28computations[1]=nevComputation(i);

29computations[x]start();

30)

31for(Computationc:computations)

Systemoutprint(c.getResult()+"");

32

33)

}

Whatistheresult?

A.Thecodewilldeadlock

B.Thecodemayrunwithnooutput

C.Anexceptionisthrownatruntime

D.Thecodemayrunwithoutputu06n

E.Thecodemayrunwithoutput"2064”

F.Thecodemayrunwithoutput"0246"

Answer:(F)

情況1run方法運(yùn)行以后運(yùn)行再調(diào)用getResuIt();

情況2先調(diào)用getResult(),掛起,等待nm()方法修改isComplete的值

線程0,線程1,線程2,線程3,main共有5個(gè)線程,中間線程執(zhí)行的順序如上面所說的,但是最后的結(jié)果是main的

for(Computationc:computations)決定的所以結(jié)果總是0246

參考大綱:多線程

QUESTION19

Giventheexhibit:

PublicclassCertkilter(

1.publicsta:icvoidmain(String[]args)throwsException{

2.Thread,sleep(3000);

3.Systemout.ptintln("sleep");

4)

5.)

Whatistheresult?

A.Compilationfails

B.Anexceptionisthrownatruntime

C.Thecodeexecutesnormallyandprints"sleep"

D.Thecodeexecutesnormally,butnothingisprinted.

Answer:(C)執(zhí)行2行的時(shí)候main被放到Blocked中,過3秒后才會(huì)回到Runnablepool中,繼續(xù)執(zhí)行輸出“sleep”

參考大綱:多線程

QUESTION20

Whichtwostatementsaretrueabouthas-aandis-arelationships?(choosetwo)

A.Inheritancerepresentsanis-arelationship

B.Inheritancerepresentsahas-arelationship

C.Interfacesmustbeusedwhencreatingahas-arelationship

D.Instancevariablescanbeusedwhencreatingahas-arelationship

Answer:(A,D)

Is-a:繼承classAextendsB{}is-a”是一個(gè)“,屬于上下的關(guān)系,

Has?a:聚合classA{Bb;}has-a“有一個(gè)”屬于聚合的關(guān)系,在java中用來表示類中的成員變量

參考大綱:面向?qū)ο?/p>

QUESTION21

Giventheexhibit:

1packagecertkiller

2.

3.classTarget(

4.publicStringname="hullo”

5.)

Whatcandirectlyaccessandchangethevalueofthevariablename?

A.anyclass

B.onlytheTargetclass

C.anyclassintheCertkillerpackage

D.anyclassthatextendsTarget

Answer:(C)

要修改name,必須先取得Target的對(duì)象,Target的存取權(quán)限是default,因此同一個(gè)包才能訪問。

參考大綱:面向?qū)ο笠籶ackage與import

QUESTION22

Whichthreestatementsaretrue?(choosethree)

A.AfinalmethodinclassXcanbeabstractifandonlyifXisabstract

B.AprotectedmethodinclassXcanbeoverriddenbyanysubclassofX.

C.AprivatestaticmethodcanbecalledonlywithinotherstaticmethodsinclassX.

D.Anon-staticpublicfinalmethodinclassXcanbeoverriddeninanysubclassofX.

E.ApublicstaticmethodinclassXcanbecalledbyasubclassofXwithoutexplicitly

referencingtheclassX.

F.AmethodwiththesamesignatureasaprivatefinalmethodinclassXcanbe

implementedinasubclassofX.

Answer:(B,E,F)

A錯(cuò)誤抽象方法必須被實(shí)現(xiàn),因此是不能加final修飾符的

B正確子類可以合法的復(fù)寫父類的protected權(quán)限的方法

C錯(cuò)誤privatestatic方法在X類中的方法都可以調(diào)用,static和non-static都可以

D錯(cuò)誤final方法不可以復(fù)寫,除非是private方法.但是這種情況不叫改寫,而是子類有和父類一樣的privatefinal方法,

兩個(gè)方法是各自獨(dú)立的!!!!

E正確不用加上父類別的引用,子類就可以直接調(diào)用父類的static方法

F正確父類的privatestatic方法和子類中同名privatestatic方法是兩個(gè)獨(dú)立方法,這種情況就不叫改寫,而是各臼擁有自

己的方法!

參考大綱:面向?qū)ο?/p>

QUESTION23

PlacetheTypesinoneoftheTypecolumns,andtheRelationshipsinthe

Relationshipcolumn,todefineappropriatehas-aandis-arelationships.

TypeRelationshipTypeRelationshipsTypes

Ptocehere|Phceh?e|Animal曲aDog

?

Forest|Ptacehece|PldcehecehasaSide

Tail

Rectangle[Placehere|Ptacehere

Square

PlaceherePhcehereProgrammingBookTree

Book

JavaBook

Pen

Answer:()

Dogis-aAnimal是一類

Foresthas-aTree有一個(gè)

Rectanglehas-aSquare矩形有一個(gè)正方形

JavaBookis-aProgrammingBook是一個(gè)

參考大綱:面向?qū)ο?/p>

QUESTION24

ReplacetwooftheModifiersthatappearintheSingleclasstomakethecodecompile.

Note:Threemodifierswillnotbeusedandfourmodifiersinthecodewillremain

unchanged.

Code

publicclassSingle{Modifiers

||~|Sin91einstance.IfinalI

IpublicI[Atctiu]Singleget:nstance(){[p-tM]

it(instance■null)instance-create().1

returninstance

)

[private]Smglef>{)

[protected|Singlecreate()(returnnevSm91e(>

)

classSingleSubextendsSingle(

)

Answer:()

publicclassSingle{

privatestaticSingleinstance;

publicstaticSinglegetlnstance(){

if(instance==null)instatnce=create();

returninstance;

protectedSingle(){}因?yàn)镾ingleSub繼承了Single,所以Single的構(gòu)造器必須可以讓子類訪問到!!!!

staticSingleCreate(){returnnewSingle();)因?yàn)榈?行…二create。;說明這個(gè)方法必須是static的方法

)

參考大綱:static成員和繼承時(shí)方法的權(quán)限控制

QUESTION25

Exhibit:

1.publicclassSimpleCalc{

2.publicintvalue;

3.publicvoidcalculate(){value+=7;}

4?}

And:

publicclassMultiCalcextendsSimpleCalc{

publicvoidcalculate(){value-?3;}

publicvoidcalculate(intMultiplier){

calculate();

super.calculate();

value*amultiplier;

}r

publicstaticvoidmain(String[]args){

MultiCalccalculator=newMultiCalc();

10calculator.calculate(2);

11Systemoutprintln("Valueis:"+calculatorvalue);

12}

1。

Whatistheresult?

A.V如eis:8

B.Compilationfails.

C.Valueis:12

D.Valueis;-l2

E.Thecoderunswithnooutput

F.Anexceptionisthrownatruntime.

Answer:(A)

2行,value默認(rèn)是=0;9行實(shí)例化一個(gè)MultiCale對(duì)象,并傳了一個(gè)參數(shù)multiplier=2,10行調(diào)用3行的方法;方法內(nèi)的

calculate()又調(diào)用2行,結(jié)果value=0?3;然后super.calculate。調(diào)用父類的calculate(),value=-3+7=4;然后6

行,value=vakie*multiplier=4*2=8;然后到11行輸出valueis:8

參考大綱:面向?qū)ο?/p>

QUESTION26

Giventheexhibit:

20.publicclassCertkillerCard(

21.

22privateStringcardID

23.privateIntcgrlimit,

24publicStringownerName;

25.

26publicvoidsetCardlnfbrmsiion(StringcardID,

27.StringownerName,

28.Integerlimit)(

29.this.cardlD=cardID,

30tliis.owxicrNanicyownerNamc,

31.this.limit=limit;

32.)

33.)

Whichstatementistrue?

A.Theclassisfullyencapsulated

B.Thecodedemonstratespolymorphism.

C.TheownerNamevariablebreaksencapsulation

D.TheCardIDandlimitvariablesbreakpolymorphism

E.ThesetCardlnfbrmationmethodbreaksencapsulation

Answer:(C)

第24行破壞了封裝,應(yīng)該成privateStringownerName;

參考大綱:面向?qū)ο笠环庋b

QUESTION27

Giventheexhibit:

11.classAnimal(publicStringnoise()(return"peep"))

12.classDogextendsAnimal(

13.publicStrmgnoise()(return“back";)

14.)

15.classCatex^ndsAnimal(

16.publicStnngnoise()(return"move";)

17.)

30.Animalanimal=newDog();

31.Catcat=(Cat)animal;

32.System.out.println(Cat.Noise());

Whatistheresult?

A.peep

B.bark

C.move

D.Compilationfails.

E.Anexceptionisthrownatruntime

Answer:(E)

31行運(yùn)行時(shí)發(fā)生造型異常,運(yùn)行時(shí)錯(cuò)誤!!30行后Animal的實(shí)體是dog,31行有轉(zhuǎn)型到cat是不對(duì)的(兄弟類之間不能轉(zhuǎn)換)!

編譯是沒有錯(cuò)誤是因?yàn)閐og和cat有共同的父類,

參考大綱:面向?qū)ο笠欢鄳B(tài)

QUESTION28概念

Exhibit:

1publicclassCar{

3privateintvheelCount;

4privateStringvin;

5publicCar(Stringvin){

6thisvin=vin:

7}thisvheelCount=4,

8

9publicStringextend(){

10return*zoom"zoom",

}

A11

12publicString9etlnfo(){

13return"VIN"+vin+"wheels-"+vheelCount.

14)一.

)

And:

1publicclassMeGoextendsCar{

2.publicMeGo(Stringvin)<

3.thisvheelCount553;

4}

5)

Whattwomusttheprogrammerdotocorrectthecompilationerrors?

A.insertacalltothis()intheCarconstructor

B.insertacalltothis()intheMeGoconstructor

C.insertacalltosuper()intheMeGoconstructor

D.insertacalltosuper(vin)intheMeGoconstructor

E.changethewheelCountvariableinCARTOPROTECTED

F.CHANGELINE3INTHEMeGoclasstosuper.wheelCount=3;

Answer:(E)

D父類無默認(rèn)構(gòu)造函數(shù),子類需顯示調(diào)用父類的構(gòu)造函數(shù)利用super(參數(shù))來調(diào)用父類的構(gòu)造函數(shù)

EwheelCount為私有變量,子類無法訪問,改成default,publicprotected都可以,本題改成protected比較合適

2行改為protectedintwheelCount;

And2行和3行之間插入super(vin);

參考大綱:面向?qū)ο?/p>

QUESTION29------------多態(tài)

Giventheexhibit:

10.interfaceA{publicintgtValue();}

11classBimple

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論