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頁,還剩32頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

?考試大綱

?第一部分: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.For??tf=newFor?$t();

17.by(

18.FileOutputStreamfs■newFileOutputStream(**Forest.Ser");

19.ObjectOutputStreamOS=newObjectOutputStream(fs);

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

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

22

23..

24.classTree()

A.Compilationfails

B.Anexceptionisthrownatruntime.

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)生的運(yùn)行異常!

參考大綱:IO操作一對象的序列化

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

QUESTION2

Whichcode,insertedatline14,willallowthisclasstocorrectlyserializedand

desterilized?

1

2laportjava10?;

3publicclassFooiMpleaentsSerializable{

4publicintx.y;..

5publicFoo(mtx.inty){this.x-this.y■y;}

6

7privatevoidvriteObject(ObjeetOutputStreaas)

8throwslOException{

9svritelnt(x).s.vritelnt(y).

10}

11

12privatevoidrcadObject(ObjectInputStreaMS)

13throwslOException.ClassNotFoundException(

14

15//insentcodehere

16)

17)

A.s.defaultReadObject();

B.this=s.defaultReadObject();

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

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

Answer:(D)

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

參考大綱:IO操作一對象的序列化

QUESTION3

Giventheexhibit.

11.Stringtest="Thisisatest",

12String[]tokens=testsplit(,s)

13.System.outpnntln(tokenslengyh).

Whatistheresult?

A.O

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();

13.d£setLocale(Locallaly);

14Strings=dfFormat(date),

ThevariabledfisanobjectoftypeDateFormatthathasbeeninitializedinline11.

WhatistheresultifthiscodeisrunonDecember14,2000?

A.ThevalueofSis14-dic-2004

B.ThevalueofSisDec14,2000

C.Anexceptionisthrownatruntime

D.Compilationfailsbecauseofanerrorinline13.

Answer:(D)

DateFormat用來格式日期,它放在t包里,它沒有.代碼語法有問題,,編譯錯(cuò)誤!

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

QUESTION5

ThedoesFileExistmethodtakesanarrayofdirectorynamesrepresentingapath

fromtherootfilesystemandafilename.Themethodreturnstrueifthefileexists,

falseifdoesnot.

Placethecodefragmentsinpositiontocompletethismethod.

return恥gexistsj]|[Mumpathisfile4][Fie*.newFtefpathHenamel

|palh'newFtejpath匈|(zhì)|F-path■newteparatop.||path?path?Heseparaof?*"]

Answer:()

publicstaticbooleandoesFileExist(String(]directories.Stringfilename){

Stringpath=

fbr(Stringdir:directories){

path=path+Fileseparator+dir;

Filefile=newFile(path,filename);

returnfile.exists();

}

參考大綱:I。操作一File

QUESTION6

Given:

Syscem.out.printf("Piisapproximately%fandEisapproximately%b".Math.PI,Math.E);

Placethevalueswheretheywouldappearintheoutput.

Piisapproxmateiy|FMcentre"|

Answer:()

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

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

Rwritingalineseparatortothestream

Answer:(E)

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

參考大綱:I/O操作一BufferWriter和FileWriter

QUESTION8

Giventheexhibit:

Whichtwocodefragments,insertedindependentlyatline3,generatetheoutput

4247?(choosetwo)

1.publicclassCertkilef3(

2publicstaticvoidmain(String[]args)(

3//insertcodehere

5Systetnout.pnntln(s),

6)

7.)

A.Strings三”123456789”;

s.=(s-"123M).replace(1,3,H24M)-"89M;//String中只有表示連接,但是無

B.StringBuffers=newStringBuffer(n123456789");

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

C.StringBuffers=newStringBuffer("123456789");

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,n24")錯(cuò)誤同上

E.StringBuilders=newStringBuilder("123456789");

s.delete(0,3)replace。,3,'"').delete(2,5).insert(1,“24”)

Answer:(B,E)

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

B,正確4247

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

D,S.substring返回的是String

E,正確4247

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

Siring、StringBuffer線程安全的適用于多線程、StringBuilder線程不安全,適合單線程,但是性能

高.

StringBufferStringBuilder的用法一樣

QUESTION9

Whichthreestatementsconcerninstheuseofthejava.io.Serialization

interfacearetrue?(choosethree)

A.Objectfromclassesthatuseaggregationcannotbeserialized.

B.AnobjectserializedononeJVMcanbesuccessfullydesterilizedonadifferentJVM.

C.ThevaluesinfieldswiththeVolatilemodifierwillNOTsurviveserializationand

deserialization

D.ThevaluesinfieldwiththetransientmodifierwillNOTsuniveserializationand

deserialization

E.ItislegaltoserializeanobjectofatypethathasasupertypethatdoesNOTimplement

java.io.Serialization

Answer:(B,D,E)

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

描述和對象是否支持序列化沒有直接關(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)的對象是不支持序列化的

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

參考大綱:1O操作一對象的序列化

QUESTION10

Giventheexhibit:

12.publicclassCertkiler{

13.publicstaticvoidgo(shortn)(SysemoutpnntlnCshorr),)

14.publicstaticvoidgo(Shortft)(SysetnoutpnntlnCSHORT*);)

15.publicstaticvoidgo(Longn)(Sysem.out.prmtIn(MLONG");)

16.publicstaticvoidmain(String[]args)(

17.Shorty=6;

18.intz=7;

19.go(y);

20.go(z);

21.

22.

Whatistheresult?

A.shortLong

B.SHORTLONG

C.Compilationfails

D.Anexceptionisthrownatruntime

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

第2。行的go(z)將會(huì)依序找gn(inti),gn(lnngl),gn(floatf),gn(dnnhled)或gc(Integeri)方法.但是并

不會(huì)自動(dòng)向上轉(zhuǎn)型Long然后再呼叫g(shù)o(Longn),這種依順序向上找的特性只會(huì)發(fā)生在對應(yīng)端基本

資料型別的情況下,

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

QUESTION11

Giventheexhibit:

*disvalid,non-nullDateobject

*dfisavalid,non-nullDatcFormatobjectsettothecurrentlocal

Whatoutputsthecurrentlocal'scountrynameandtheappropriateversionofD'sdate?

A.Localeloc=Locale.getLocal();

System.outprintin(loc.getDisplayCountry()

B.Localeloc=Locale.getDefault();

System.outprintin(loc.getDisplayCountry()+M"+df.format(d));

C.Localeloc=Locale.getLocal();

System.outprintin(loc.getDisplayCountry()+"*'+df.setDatcFormat(d));

D.Localeloc=Locale.getDefault();

System.outprintin(loc.getDisplayCountry()+"H+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:

1publicclassCertkfller3implementRunnabie(

2.publicvoidrun()(

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

4)

5.publicstaticvoidmain(String']args){

6.Threadt=newThread(newCertMler3());

7.trun();

8.t.run();

9.t.start();

10.)

11-)

Whatistheresult?

A.Compilationfails.

B.Anexceptionisthrownatruntime

C.Thecodeexecutesandprints"running"

D.Thecodeexecutesandprints"runningrunning"

F.Thecodeexecutesandprints"ninninaninninigninning

Answer:(E)

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

線程負(fù)責(zé)去執(zhí)行2-4行的run();把2-4行的代碼改為:

publicvoidrun(){

StringthreadName=Threa(l.currentThread().getName();

System.out.println(threadName+,,:runningM);

}

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

main:running

main:running

Thread-o:running

參考大綱:多線程

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

Exhibit:

1.publicclassThreadsl{

2.intx-0.

3publicclassRunneriMplenentsRunnable

<

4publicvoidrim(){

5.intcurrent■0;

6for(inti?0.i<4;iw){

current?x:

8Syste*outprint(current?I");

9x■current+2:

10.>

11.}

12}

13.

14publicstaticvoidMam(Stnng[]arss){

15newThreadsl()go();

161

17

18publicvoid90(){

19Runnablerl■newRunner().

20newThread(rl).start();

21newThread(rl).start();

22}

23}

Whichtwoarepossibleresults?(choosetwo)

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

B.0,2,468,10,2,4,

C.0,246,8,10,12,14,

D.0,022,4,4,6,6,8,8,10,10,12,12,14,14、

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í)current也是4);第二

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

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

參考大綱:多線程

QUESTION14

Giventheexhibit:

7voidwaitForSignal()(

6.objectobj=newObject();

7.synchronized(ThreadcurrentThread())(

8.obj.wait();

9.objnotify();

10.)

11}

Whichstatementistrue?

A.ThiscodemaythrowanInterruptedException

B.ThiscodemaythrowanIllegalStateExcepion

C.ThiscodemaythrowaTimeOutExceptionaftertenminutes

D.Thiscodewillnotcompileunless"obj.wail()\"isreplacedwith"((Thread)obj).wail()"

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

Answer:(A)

首先編譯通不過,

第5行:voidwaitForSignal()throwsInterruptedException{........}

第6行:Objectobj=newObject();

obj=Thread.currentThread();

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

---在寫wait。和notify。方法時(shí),除了要寫在synchronized區(qū)段,?還需要撰寫InteiruptedException

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

的IllegalMonitorStateException的異常.

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

QUESTION15

Giventheexhibit:

1publicclassTestOneimplementsRunnable(

2.publicstaticvoidmain(String[]args)throwsException(

3.Threadt=newThread(newTestOne()).

4.tstart(),

5System.out.print("Started');

6t.jom(%

7.System.out.print(MCoirpleteM),

8)

9.publicvoidrun()(

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

11.Systemoutprint(i);

12.)

13.}

14.)

Whatcanbearesult?

A.Compilationfails

B.Anexceptionisthrownatruntime

C.Thecodeexecutesandprints"StartcdComplctc"

D.Thecodeexecutesandprints"StartedComplete0123"

E.Thecodeexecutesandprints"StartedO123Complete"

Answer:(E)

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

執(zhí)行main線程.本程序中第5行由main線程執(zhí)行,6行因?yàn)橄逻_(dá)了join。,所以mian的執(zhí)行將被暫

停,等t做完nin()方法的全部工作之后,才論到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();

EnewThread(newRunnable(){

publicvoidrun(){doStuff();}

}).start();

Answer:(D^F)

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

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

參考大綱:多線程

QUESTION17

Whichthreewillcompileandrunwithoutexception?(choosethree)

A.privatesynchronizedobjecto;

R.voidgo(){

synchronized(){/*codehere*/)

I

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來修飾對象instance方法,鎖定的物件將會(huì)是this,

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

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

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

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

QUESTION18

Exhibit:

classComputationextendsThread{

3

4privateintnua;

5privatebooleanisCoapIete.

6privateintresult

7

8publicCoMDUtationCintnum){this.nun■nua;}

9

10publicsynchronizedvoidrun(){

11result■nun*2;

12isComplete■true;

13notify().

14

15

16publicsynchronizedintgetResult(){

17while(IisCo*plete){

18try{

19wait();

2-}catch(InterruptedExceptione){}

0

21)

22returnresult

23)

24

2sDublicstaticvoidmain(Stringf1ar?s){

26Computation[]computations=newComoutation[4];

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

28computations[1]?nevComputation(i).

29computations[i].start();

30)

31for(Coaputationc:computations)

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

}

33.}

Whatistheresult?

A.Thecodewilldeadlock

B.Thecodemayrunwithnooutput

C.Anexceptionisthrownatruntime

D.Thecodemayrunwithoutput"06"

E.Thecodemayrunwithoutput,'2064"

F.Thecodemayrunwithoutput"0246"

Answer:(F)

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

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

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

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

參考大綱:多線程

QUESTION19

Giventheexhibit:

PublicclassCertkiller(

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:

1packagecertkiler

2.

3.classTarget(

4publicStringname="hello”

5.)

Whatcandirectlyaccessandchangethevalueofthevariablename?

A.anyclass

B.onlytheTargetclass

C.anyclassintheCertkillerpackage

D.anyclassthatextendsTarget

Answer:(C)

要修改name,必須先取得Target的對象,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

Placehere|PUcehe(eAnimal電-a

ForestPhceherePlacehetehas-a

RectanglePhceherePtecehete

PlaceherePbcehereProgrammingBook

Answer:()

Dogis-aAnimal是一類

Foresthas-aTree有一個(gè)

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

JavaRookis-aProgrammingRook是一個(gè)

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

QUESTION24

ReplacetwooftheModifiersthatappearintheSingleclasstomakethecodecompile.

Note:Threemodifierswillnotbeusedandfourmodifiersinthecodewillremain

unchanged.

Code

publicclassSingle{Modifiers

IprmtaI[mtie|Singleinstance.IfinalI

jpublic""!ISingleget:nstance(){[proiectm]

it(instance■null)instance?create().Iprivate"]

returninstanceIabstraeTl

>

[privateISingle(I()Istatic~~|

[protected]Singlecreate(>{returnnewSingle

}

classSingleSubextendsSingle(

)

Answer:()

publicclassSingle)

privatestaticSingleinstance;

publicstaticSinglegetlnstance(){

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

returninstance;

)

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

到!!!!

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

static的方法

)

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

QUESTION25

Exhibit:

publicclassSiapleCalc{

publicintvalue;

publicvoidcalculate(){value+■7.}

DublicclassMultiCalcextendsSijftpleCalc{

publicvoidcalculate(){value--3.}

publicvoidcalculate(intMultiplier){

colculate(),

super.calculate();

value?■multiplier;

publicstaticvoidmain(String[]args){

MultiCalccalculator■newMultiCalcC);

calculator.calculate(2);

Systemoutprintln("Valueis:"+calculatorvalue).

}

Whatistheresult?

A.Valueis:8

B.Compilationfails.

C.Valueis:12

D.Valueis;-12

E.Thecoderunswithnooutput

F.Anexceptionisthrownatruntime.

Answer:(A)

2行,value默認(rèn)是=0;9行實(shí)例化一個(gè)MultiCalc對象,并傳了一個(gè)參數(shù)multiplicr=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=value*multiplier=4*2=8;然后到11行,輸出valueis:8

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

QUESTION26

Giventheexhibit:

20.publicclassCertkillerCard{

21.

22privateStringcardID

23.privateIntcgrlimit,

24publicStringownerName;

25.

26publicvoidsetCardlnfbrmaiion(StringcardID,

27.StringownerName,

28.Integerlimit)(

29.this.cardlD=cardID,

30.Uns.owncrNanic■,ownerNamc,

31.this.limit=limit;

32)

33.)

Whichstatementistrue?

A.Theclassisfullyencapsulated

R.Thecodedemonstratespolymorphism.

C.TheownerNamevariablebreaksencapsulation

D.TheCardIDandlimitvariablesbreakpolymorphism

E.ThesetCardlnformationmethodbreaksencapsulation

Answer:(C)

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

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

QUESTION27

Giventheexhibit:

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

12classDogextendsAnimal{

13.publicStringnoise()(return"back”;)

14.)

15.classCatex^ndsAnimal(

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

17)

30.Animalanimal=newDog();

31.Catcat=(Cat)animal,

32.System.out.printin(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)型到cal是不對的(兄

弟類之間不能轉(zhuǎn)換)!編譯是沒有錯(cuò)誤是因?yàn)閐og和cat有共同的父類,

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

QUESTION28------------概念

Exhibit:

1

2publicclassCar{

3privateintvheelCount;

4privateStringvia;

5publicCar(Stringvin){

6thisvin=vin:

7}thiswheelCount=4;

0

0publicStringextendf){

9

10return"zoom"zoom".

1}

12publicString9etlnfo(){

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

4}??

}

And:

1publicclassMeGoextendsCar{

2.publicMeGo(Stringvin){

3.this.wheelCount-3;

41

5}

Whattwomusttheprogrammerdotocorrectthecompilationerrors?

A.insertacalltothis()intheCarconstructor

B.insertacalltothis()intheMeGoconstructor

C.insertacalltosuper()intheMeGoconstructor

D.insertacalltosuper(vin)intheMeGoconstructor

E.changethewheelCountvariableinCARTOPROTECTED

F.CHANGELINE3INTHEMeGoclasstosupcr.whcelCount=3;

Answer:(D、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:

10interfaceA{publicintgtValue();}

11classBimplementsA(

12.publicintgotValue(){return1;)

13)

14classCextendsB(

15.//insertcodehere

16.)

Whatthreecodefragmentsinsertedindixiduallyatline15,makeuseof

polymorphism?(choosethree)

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

R.publicvoidadd(Rh){h.getValne();}

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

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

E.publicvoidadd(Ccl,Cc2){cl.getValue();}

Answer:(B,C,D)

AE只是簡單的使用C的方法

多態(tài)必須存在繼承

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

QUESTION30

Giventheexhibit:

11certkiller=newReailyBigObject();

12.//morecodehere

13certkiltef=null;

14./*insertcodehere*/

Whichstatementshouldbeplacedatline14tosuggestthatthevirtualmachine

expendefforttowardrecyclingthememoryusedbytheobjectCertkiller?

A.System.gc()

B.Runtime.Gc()

C.System.freeMemory()

D.Runtime.getRuntime().growHeap()

E.Runtime.getRuntime().freeMemory()

Answer:(A)

建議JVM進(jìn)行資源回收的方法:

System.gcO;

Runtime.getRuntimeO.gcO;

參考大綱:面向?qū)ο笠焕厥諜C(jī)制

QUESTION31

Exhibit:

10classFoo{

1112privateintx,

13publicFoo(mtx){thisx=x;}

14publicvoidsetX(intx){thisx■x.}

15publicintgetX(){returnx,}

16}

17

18publicclasssubmit{

19staticFoofooBar(Foofoo){

20foo■newFoo(100);

21returnfoo

22}

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(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ǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論