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

下載本文檔

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

文檔簡(jiǎn)介

?考試大綱

,第一部分:Java語(yǔ)言基礎(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?stf=newForest();

17.Uy(

18.FileOutputStreamfs=newFileOutputStream(uForest.Serw);

19.ObjectOutputStreamOS?newObjectOutputStream(fs);

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

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

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)行異常!

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

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

QUESTION2

Whichcode,insertedatline14,willallowthisclasstocorrectlyserializedand

desterilized?

1import^ava10?;

2publicclassFoompleaentsSerializable{

3.publicintx.y:.

4publicFy){this.x"x.this.y■y;}

6privatevoidvriteObnect(OboectOutputStreams)

7.throwslOException{

8s.vritelnt(x);s.vritelnt(y);

9.)

10.

11privatevoidreadObject(ObjectInputStreams)

12throwslOException.ClassNotFoundException(

13.

14//insentcodehere

15

16.)

17.}

A.s.defaultReadObject();

B.this=s.defaultReadObject();

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

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

Answer:(D)

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

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

QUESTION3

Giventheexhibit.

11.Stringtest="Thisisatest”,

12.String[]tokens=test,split(,s)

13.Systemourpnntln(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可以用“\t”;可以用“\\”表示.

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

示空格,"\s”沒有這個(gè)轉(zhuǎn)意字符,所以會(huì)產(chǎn)illegalescapecharacter的編譯錯(cuò)誤。

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

QUESTION4

Giventheexhibit:

12.Datedate=newDate();

13dfsetLocale(Locallaly);

14Strings=dfFormat(date),

ThevariabledfisanobjectoftypeDateFormatthathasbeeninitializedinline11.

WhatistheresultifthiscodeisrunonDecember14,2000?

A.ThevalueofSis14-dic-2004

B.ThevalueofSisDec14,2000

C.Anexceptionisthrownatruntime

D.Compilationfailsbecauseofanerrorinline13.

Answer:(D)

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

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

QUESTION5

ThedoesFileExistmethodtakesanarrayofdirectorynamesrepresentingapath

fromtherootfilesystemandafilename.Themethodreturnstrueifthefileexists,

falseifdoesnot.

Placethecodefragmentsinpositiontocompletethismethod.

Pbcehete

}

CodeFragments

|麻必—切如]|letumlHettNeQ||皿n未必I

|ZnrgpyW"][path.pathqeiffeWename]]|Atepath?newAteTl|

|retuEEleexists}]-][returnpathi*fiteQ.||FteMe?newHename{~l

|path.newFfetpaih.|Fiepath?newII|

Answer:()

publicstaticbooleandoesFileExist(String[|directories,Stringfilename){

Stringpath=

for(Stringdir:directories){

path=path+File.separator+dir;

)

Filefile=newFile(path,filename);

returnfile.exists。;

)

參考大綱:IO操作一File

QUESTION6

Given:

System.out.printf(nPiisapproximately%fandEisapproximately%b",Math.PI,Math.E);

Placethevalueswheretheywouldappearintheoutput.

Piis?pproxxnately[Pto*~|

Answer:()

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

PrintfO是C中常用的語(yǔ)法;

%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功能。

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

QUESTION8

Giventheexhibit:

Whichtwocodefragments,insertedindependentlyatline3,generatetheoutput

4247?(choosetwo)

1.publicclassCertkiller3(

2.publicstaticvoidmain(String[]args)(

3//insertcodehere

5Systemoutprintin(c);

6)

7.)

A.Strings="123456789";

s.二(s-,,123u).replace(1,3,“24“)-“89";//String中只有”+“表示連接,但是無

B.StringBuffers=newStringBuffer("123456789*1);

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

C.StringBuffers=newStringBuffer(n123456789");

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

StringBuffer,String沒有delete方法,產(chǎn)生cannotfindsymbol的編譯錯(cuò)誤

D.StringBuilders=newStringBuilder(M123456789");

s.substring(3,6)delete(1,2).insert(1,“24”)錯(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—

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

高.

StringBufferStringBuilder的用法一樣

QUESTION9

Whichthreestatementsconcerningtheuseofthejava.io.Serialization

interfacearetnie?(choosethree)

A.Objectfromclassesthatuseaggregationcannotbeserialized.

B.AnobjectserializedononeJVNIcanbesuccessfullydesterilizedonadifferentJVNL

C.ThevaluesinfieldswiththeVolatilemodifierwillNOTsurviveserializationand

deserialization

D.ThevaluesinfieldwiththetransientmodifierwillNOTsurviveserializationand

deserialization

E.ItislegaltoserializeanobjectofatypethathasasupertypethatdoesNOTimplement

java.io.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.publicclassCertkilef(

13.publicstaticvoidgo(shortn)(SysemoutpnntlnCshort^),)

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

15.publicstaticvoidgo(Longn)(Sysem.ovt.prmtIn(aLONGw);)

16.publicstaticvoidmain(String[]args)(

17.Shorty=6;

18.intz=7;

19.go(y);

20.go(z);

2L)

22.)

Whatistheresult?

A.shortLong

B.SHORTLONG

C.Compilationfails

D.Anexceptionisthrownatruntime

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

第20行的go(z)將會(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.format(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

2publicclassCertWter3implementRunnable(

3publicvoidrun()(

4system,outprint("running");

5

6publicstaticvoidmain(String:]args)(

Threadt=newThread(newCertldler3());

7.t.run();

8.t.run();

9.t.start();

10.)

IL)

Whatistheresult?

A.Compilationfails.

B.Anexceptionisthrownatruntime

C.Thecodeexecutesandprints"running"

D.Thecodeexecutesandprints"runningrunning”

E.ThecodeexecutesandprintsHrunningrunninigrunning

Answer:(E)

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

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

publicvoidrun(){

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

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

)

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

main:running

main:running

Thread-o:running

參考大綱:多線程

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

Exhibit:

1.publicclassThreadsl{

2.intx?0;

3publicclassRunnerimplewentsRunnable

4

5publicvoidrun(){

6intcurrent,0;

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

8current■x;

9Systemoutprint(current?;

10x?ourrent?2;

11

12

13

14

15publicstaticvoidMain(Stnng[]arss){

16nevThreadsK)^o();

17

18

19publicvoidgo(){

20Runnablerl■nevRunner().

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,24

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

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

E.0,2,46,8/012,14,0,246,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();

7synchronized(ThreadcurrentThread())(

8objwait();

9.obj.notify()t

10-)

11.)

Whichstatementistrue?

A.ThiscodemaythrowanInterruptedException

B.ThiscodemaythrowanIllegalStateExcepion

C.ThiscodemaythrowaTimeOutExceptionaftertenminutes

D.Thiscodewillnotcompileunless"obj.wait()\uisreplacedwith"((Thread)obj).wait()H

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

Answer:(A)

首先編譯通不過,

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

第6行:Objectobj=newObject();

obj=Thread.currentThread();

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

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

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

的IllegalMonitorStateException的異常.

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

QUESTION15

Giventheexhibit:

1

2publicclassTestOneimplementsRunnable(

3publicstaticvoidmain(String['args)throwsException(

4Threadt=newThread(newTestOnef));

5t.start(),

6Systemoutprint(^Started**);

7tjom(%

8System,out.print("Corrplete"),

)

9.publicvoidrun()(

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

11.Systemoutprint(i);

12.)

13.)

14.)

Whatcanbearesult?

A.Compilationfails

B.Anexceptionisthrownatruntime

C.Thecodeexecutesandprints"StartedComplete"

D.ThecodeexecutesandprintsnStartedCompleteO123"

E.Thecodeexecutesandprints"Started0123Complete"

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

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;

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:

classComputationextendsThread{

privateintnua;

privatebooleanisCoaplete.

privateintresult

publicCoMDUtation(intnum)(this.num■num;}

publicsynchronizedvoidrun(){

resultBHUM*2;

isComplete?true;

notify().

}

publicsynchronizedintgetResult(){

while(IisCohplete){

18try{

19wait();

--}catch(InterruptedExceptione)(}

20}

21

22returnresult;

23)

24

publicstaticvoidmain(Stringf1arets){

25

B

26Computation!]computationsnewComoutation[4];

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

27

computationsfi]?nevComputation(i);

28

29computations[i]start();

30}

31tor(Computationc:conputations)

Systemoutprint(c.getResult()+-";

32

)

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(),掛起,等待mn()方法修改isComplete的值

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

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

參考大綱:多線程

QUESTION19

Giventheexhibit:

PublicclassCertkiller(

1.publicstaricvoidmain(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)

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

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

中的成員變量

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

QUESTION21

Giventheexhibit:

1packagecertkiller

2.

3.classTarget(

4publicStringname="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

Placehete|PUcehereAnimal■a

hasa

ForestPtdceherePlacehere

RectanglePtaceherePlacehete

PbceherePUceheeProgrammingBook

Answer:()

Dogis-aAnimal是一類

Foresthas-aTree有一個(gè)

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

JavaBookis-aProgrammingBook是一個(gè)

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

QUESTION24

ReplacetwooftheModifiersthatappearintheSingleclasstomakethecodecompile.

Note:Threemodifierswillnotbeusedandfourmodifiersinthecodewillremain

unchanged.

Code

publicclassSingle{Modifiers

|~|Singleinstance.Ifioa】|

Iprotect同

[public""!Ivdc:—1Singleget:nstance(){

Iprivate|

it(instance?null)instance?create()

returninstance|abstract']

)

[private]Sxngle(><)

[petectedlSinglecreate(){returnnewSingle())

)

classSingleSubextendsSingle(

Answer:()

publicclassSingle{

privatestaticSingleinstance;

publicstaticSinglegetlnstance(){

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

returninstance;

)

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

至I」!!!!

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

static的方法

)

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

QUESTION25

Exhibit:

1.publicclassSimpleCalc{

2.publicintvalue;

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

4?}

And:

DublicclassMultiCailcextendsSijapleCalc{

publicvoidcalculate(){value3;}

publicvoidcalculate(intMultiplier){

calculate();

supercalculate();

value?smultiplier;

},

publicstaticvoidmain(String[]args){

MultiCalccalculator=newMultiCalcC);

10.calculator.calculate(2);

11System.outprmtln("Valueis:"+calculatorvalue);

?12.}

1,

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è)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=vahie*imiltiplier=4*2=8;然后至!|11行,輸出valueis:8

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

QUESTION26

Giventheexhibit:

20.publicclassCertkillerCard{

21.

22privateStringcardID:

23.privateIntcgrhrait,

24publicStringownerName;

25.

26publicvoidsetCardlnfbrm^ion(StringcardID,

27.StringownerName,

28Integerlimit)(

29.this.cardlD=cardID,

30tliis.QwnerNanicqQwncrNarnc,

31.this.limit=limit;

32)

33.}

Whichstatementistrue?

A.Theclassisfullyencapsulated

B.Thecodedemonstratespolymorphism.

C.TheownerNamevariablebreaksencapsulation

D.TheCardIDandlimitvariablesbreakpolymorphism

E.ThesetCardlnformationmethodbreaksencapsulation

Answer:(C)

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

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

QUESTION27

Giventheexhibit:

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

12.classDogextendsAnimal(

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

14)

15.classCatexxndsAnimal(

16.publicStnngnoise()(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)型到cat是不對(duì)的(兄

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

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

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

Exhibit:

1publicclassCar{

3privatemtvheeLCount;

4privateStringvin;

5publicCar(Stringvin){

6thisvin=vin:

7}thiswheeiCount=4,

8

9publicStringextend(){

10return“zoom"zoom".

1}

12publicStringgetlnfo(){

1Jreturn"VIN"+vin+*wheels“+vheelCount;

14)一?

}

And

1publicclassMeGoextendsCar{

2publicMeGo(Strmgvin)<

3.thiswheelCount=3,

4}

5.)

Whattwomusttheprogrammerdotocorrectthecompilationerrors?

A.insertacalltothis()intheCarconstructor

B.insertacalltothis()intheMeGoconstructor

C.insertacalltosuper()intheMeGoconstructor

D.insertacalltosuper(vin)intheMeGoconstructor

E.changethewheelCountvariableinCARTOPROTECTED

F.CHANGELINE3INTHEMeGoclasstosuper.wheelCount=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();}

11.classBimplementsA(

12.publicintgotValue(){return1;)

13.J

14classCextendsB(

15.//insertcodehere

16.)

Whatthreecodefragmentsinsertedindixiduallyatline15.makeuseof

polymorphism?(choosethree)

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

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

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

D.publicvoidadd(Aa,Bb){a.getValu

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(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)論