ocj精选考试题库Word格式文档下载.docx

上传人:b****5 文档编号:16275656 上传时间:2022-11-22 格式:DOCX 页数:25 大小:28.03KB
下载 相关 举报
ocj精选考试题库Word格式文档下载.docx_第1页
第1页 / 共25页
ocj精选考试题库Word格式文档下载.docx_第2页
第2页 / 共25页
ocj精选考试题库Word格式文档下载.docx_第3页
第3页 / 共25页
ocj精选考试题库Word格式文档下载.docx_第4页
第4页 / 共25页
ocj精选考试题库Word格式文档下载.docx_第5页
第5页 / 共25页
点击查看更多>>
下载资源
资源描述

ocj精选考试题库Word格式文档下载.docx

《ocj精选考试题库Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《ocj精选考试题库Word格式文档下载.docx(25页珍藏版)》请在冰豆网上搜索。

ocj精选考试题库Word格式文档下载.docx

3.

1)publicclassFoo{

2)

publicstaticvoidmain(Stringargs[]){

whatistheresult?

A.Theprogramrunsandprintsnothing.

B.Theprogramrunsandprints“Finally”.

C.Thecodecompiles,butanexceptionisthrownatruntime.

D.Thecodewillnotcompilebecausethecatchblockismissing.

Answer:

b

trycatchfinally的问题。

程序中如果遇到return,贝Ufinally

块先被执行,然后再执行retrun,而finally块后面的语句将不被执行。

如果

遇到System.exit

(1),则finally块及其后的语句都不执行,整个程序退出,

还执行什么呀。

4.

1)publicclassTest{

2)publicstaticStringoutput二"

"

3)publicstaticvoidfoo(inti){

4)try{

5)if(i==1){

6)thrownewException();

7)}

8)output+="

1"

;

9)}

10)catch(Exceptione){

13)}

14)finally{

15)output+二"

3"

16)}

17)output+="

4"

18)}

19)publicstaticvoidmain(Stringargs[]){

20)foo(0);

21)foo

(1);

22)

22)}

23)}

whatisthevalueofoutputatline22?

Asnwer:

13423

执行第一个foo(0)时,执行第8条语句,output=1,然后执行语句15,

output=13,然后是17条,output=134,因为是static类型的变量,所以任何对其值的修改都有效。

执行第二条foo

(1),先执行语句5,结果抛出异常,转到

catch块,output=1342,finally任何情况下都执行,所以output=13423,然

后return跳出方法体,所以output=13423

5.

3)publicstaticvoidmain(Stringargs[]){

4)if(odd(5))

5)else

8)publicstaticintodd(intx){returnx%2;

whatisoutput?

Answer:

编译错误。

if中的判断条件的结果必须是boolean类型的。

注意这里说的是结果.

6•@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

1)classExceptionTest{

2)publicstaticvoidmain(Stringargs[]){

3)try{

4)methodA();

5)}catch(IOExceptione){

7)}catch(Exceptione){

10)}

11)}

IfmethodA()throwsaIOException,whatistheresult?

caughtIOException

如果methodA()抛出lOExecption,被语句6捕获,输出caughtlOException,

然后呢?

然后就结束了呗。

7.

1)inti=1,j=10;

2)do{

3)if(i++>

--j)continue;

4)}while(i<

5);

AfterExecution,whatarethevalueforiandj?

A.i=6j=5

B.i=5j=5

C.i=6j=4

D.i=5j=6

E.i=6j=6

d

程序一直循环,直到i=4,j=6时,执行完语句3后,i会++,这时i就等于了

5,continue后就不能再循环了,所以选D。

8.

1)publicclassX{

2)publicObjectm(){

3)Objecto二newFloat(3.14F);

4)Object]]oa二newObject[1];

6)

o二nuII;

7)

oa[0]=null;

9)

10)

whichlineistheearliestpointtheobjectareferedisdefinitely

elibiletobegarbagecollectioned?

A.Afterline4

B.Afterline5

C.Afterline6

D.Afterline7

E.Afterline9(thatis,asthemethodreturns)

当执行第6行后,仍然有对象指向0,所以o不能满足条件,当第7条语句被执

行后,就再也没有对象指向o了,所以选Do

9•@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

1)interfaceFoo{

2)intk=0;

3)}

4)publicclassTestimplementsFoo{

5)publicstaticvoidmain(Stringargs[]){

inti;

Testtest二newTest();

8)

i=test.k;

i=Test.k;

i=Foo.k;

11)

12)}

Whatistheresult?

A.Compilationsucceeds.

B.Anerroratline2causescompilationtofail.

C.Anerroratline9causescompilationtofail.

D.Anerroratline10causescompilationtofail.

E.Anerroratline11causescompilationtofail.Answer:

A

编译通过,通过测试的

10.

whatisreserved(保留)wordsinjava?

A.run

B.default

C.implement

D.import

b,D

11.

2)publicstaticvoidmain(String[]args){

3)Stringfoo=args[1];

4)Sringbar=args[2];

5)Stringbaz=args[3];

6)}

javaTestRedGreenBlue

whatisthevalueofbaz?

A.bazhasvalueof"

B.bazhasvalueofnull

C.bazhasvalueof"

Red"

D.bazhasvalueof"

Blue"

E.bazhasvalueof"

Green"

F.thecodedoesnotcompile

G.theprogramthrowanexception

G

,运行时

当执行javaTestRedGreenBlue时,数组args只有[0][1][2]

ArrayIndexOutOfBoundsException这个异常会被抛出,数组越界。

12.

intindex=1;

intfoo[]二newint[3];

intbar=foo[index];

intbaz=bar+index;

A.bazhasavalueof0

B.bazhasvalueof1

C.bazhasvalueof2

D.anexceptionisthrown

E.thecodewillnotcompile

b

数组初始化后默认值是0,所以baz=0+1=1

13.

whichthreearevaliddeclaraction(行为)ofafloat?

A.floatfoo=-1;

B.floatfoo=1.0;

C.floatfoo=42e1;

D.floatfoo=2.02f;

E.floatfoo=3.03d;

F.floatfoo=0x0123;

A,D,F

其它的系统都会认为是double类型,所以出错。

说一下A和C的区别吧,-1系

统会认为是一个int类型,把int类型再赋给float类型的foo,当然没错了,

可C就不同啦,42e1是int类型吗?

2)publicstaticvoidmain(Stringargs[]){

3)Strings;

5)}

A.Thecodecompilesand“s二”isprinted.

B.

(初

(引

whentoString

Thecodecompilesand“s=null”isprinted.

C.Thecodedoesnotcompilebecausestringsisnotinitialized

始化).

D.Thecodedoesnotcompilebecausestringscannotberefereneed用).

E.Thecodecompiles,butaNullPointerExceptionisthrown

iscalled.

C

只有实例变量系统才给予自动赋默认值的这种待遇

15.

2)publicstaticvoidmain(Stringargs[]){

3)inti=oxFFFFFFF1;

4)intj二〜i;

whichisdecimalvalueofjatline5?

A.0

B.1

C.14

D.-15

E.compileerroratline3

F.compileerroratline4

C

算一算就知道了。

16.

floatf=4.2F;

Floatg二newFloat(4.2F);

Doubled=newDouble(4.2);

Whicharetrue?

A.f==g

b.g==g

C.d==f

D.d.equals(f)

Ed.equals(g)

F.g.equals(4.2);

B

==两边类型不同不相等。

所以A和C不等。

equals只能用于引用类型,不能用

于基本类型,所以D不对,而且两边类型不兼容的话,即使对象的内容一样,也不相等,所以E和F不对。

17.@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

2)publicstaticvoidadd3(lntegeri){

3)intval=i.intValue();

4)val+=3;

5)i=newInteger(val);

7)publicstaticvoidmain(Stringargs[]){

8)Integeri=newInteger(0);

9)add3(i);

11)}

⑵}

A.compilefail

B.printout"

0"

C.printout"

D.compilesuccededbutexceptionatline3

在第五行里,程序又操作了New重新分配了内存空间。

所以此i非彼i啦18.

4)}

5算呗。

19.

2)publicstaticvoidstringReplace(Stringtext){

3)text二text.replace('

j'

T);

5)publicstaticvoidbufferReplace(StringBuffertext){

6)text=text.append("

c"

);

8)publicstaticvoidmain(Stringargs[]){

9)StringtextString二newString("

java"

10)StringBuffertextBuffer=newStringBuffer("

11)StringReplace(textString);

12)bufferReplace(textBuffer);

whatistheoutput?

javajavac

textString是String类型的,具有不变性,语句3其实是创建了一个新的字符

串,而不是修改原来的textString,而对于StringBuffer类型的对象,则所有

修改都是实在的。

所以在语句6中textBuffer变成了javac,所以输出为

javajavac。

20.

1)publicclassConstOver{

2)publicConstOver(intx,inty,intz){}

whichtwooverloadtheConstOverconstructor?

A.ConstOver(){}

B.protectedintConstOver(){}

C.privateConstOver(intz,inty,bytex){}

D.publicvoidConstOver(bytex,bytey,bytez){}

E.publicObjectConstOver(intx,inty,intz){}

a,c

主要的问题是overload,参数列表必须不同,方法名相同,访问控制无限制。

也无异常限制。

这道题因为是构造器,所以B,D和E不对,因为构造器不能有

返回类型。

1)publicclassMethodOver{

2)publicvoidsetVar(inta,intb,floatc){}

whichoverloadthesetVar?

A.privatevoidsetVar(inta,floatc,intb){}

B.protectedvoidsetVar(inta,intb,floatc){}

C.publicintsetVar(inta,floatc,intb){returna;

D.publicintsetVar(inta,floatc){returna;

a,c,d

overload无访问控制限制,所以A对,顺序也属于参数列表,顺序不同也一样

是overload,所以C正确,D当然正确了,参数列表明显不同。

22.@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

1)classEnclosingOne{

2)publicclassInsideOne{}

4)publicclassInnerTest{

5)publicstaticvoidmain(Stringargs[]){

6)EnclosingOneeo二newEnclosingOne();

7)//insertcodehere

8)}

A.lnsideOneei=eo.newInsideOne();

C.lnsideOneei=EnclosingOne.newInsideOne();

D.lnsideOneei=eo.newInsideOne();

e

这里边的一些形式是固定的。

(1)静态方法访问非静态内类:

方法为:

Outermyouter二newOuter();

//这里的myouter是创建的外部类的对

象。

Outer.Innermyinner二myouter.newInner();

//myinner是内类的对

然后再myinner.showName();

//showName(是外类中的非静态方法。

(2)非静态方法访问非静态内类

直接创建该内部类的对象:

newInner().showName();

(3)静态方法访问静态内类:

也是直接创建该内部类的对象,即Innermyinner=new

lnner(),或者Outer」nnermyinner=newOuter」nner()也行得通哦。

23.

Whatis"

isa"

relation?

A.publicinterfaceColor{}

publicclassShape{privateColorcolor;

B.interfaceComponent{}

classContainerimplementsComponent{

privateComponent[]children;

C.publicclassSpecies{}

publicclassAnimal{privateSpeciesspecies;

isa"

意思为是什么:

定义了一个超类和一个子类之间的一种直接关系:

子类

是超类的一种。

也即是继承的关系

24.@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

packagefoo;

3)publicclassOuter{

4)publicstaticclasslnner{

whichistruetoinstantiated(事例)InnerclassinsideOuter?

A.newOuter」nner()

B.newInner()

a,b

25.

classBaseClass{

privatefloatx=1.0f;

classSubClassextendsBaseClass{

privatefloatx=2.0f;

//insertcode

whataretruetooverridegetVar()?

A.floatgetVar(){

B.publicfloatgetVar(){

C.publicdoublegetVar(){

D.protectedfloatgetVar(){

E.publicfloatgetVar(floatf){

a,b,d

又是override的问题,参数列表和返回值以及方法名(好像是费话)必须精确匹

配,访问控制要更公有化,如果抛出异常,那么必须异常本身或其子集或什么都不抛•

26.

publicclassSychTest{

privateintx;

privateinty;

publicvoidsetX(inti){x=i;

publicvoidsetY(inti){y=i;

publicSynchronizedvoidsetXY(inti){

setX(i);

setY(i);

publicSynchronizedbooleancheck(){

returnx!

=y;

Underwh

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

当前位置:首页 > 小学教育 > 学科竞赛

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

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