Java试题及答案英文版.docx
《Java试题及答案英文版.docx》由会员分享,可在线阅读,更多相关《Java试题及答案英文版.docx(33页珍藏版)》请在冰豆网上搜索。
Java试题及答案英文版
1.Whichtwodemonstratean“isa”relationship?
(ChooseTwo)
A.publicinterfacePerson{}
publicclassEmployeeextendsPerson{}
B.publicinterfaceShape{}
publicclassEmployeeextendsShape{}
C.publicinterfaceColor{}
publicclassEmployeeextendsColor{}
D.publicclassSpecies{}
publicclassAnimal{privateSpeciesspecies;}
E.interfaceComponent{}
ClassContainerimplementsComponent(
PrivateComponent[]children;
Answer:
de
2.Given:
1.packagefoo;
2.
3.publicclassOuter(
4. publicstaticclassInner(
5. )
6.)
Whichstatementistrue?
A.AninstanceoftheInnerclasscanbeconstructedwith“newOuter.Inner()”
B.Aninstanceoftheinnerclasscannotbeconstructedoutsideofpackagefoo
C.Aninstanceoftheinnerclasscanonlybeconstructedfromwithintheouterclass
D.Fromwithinthepackagebar,aninstanceoftheinnerclasscanbeconstructedwith“newinner()”
Answer:
c
3.Exhibit:
1publicclassenclosinggone{
2publicclassinsideone{}
3}
4publicclassinertest{
5publicstaticvoidmain(String[]args){
6enclosingoneeo=newenclosingone();
7//insertcodehere
8}
}
Whichstatementatline7constructsaninstanceoftheinnerclass?
A.InsideOneei=eo.newInsideOne();
B.B.Eo.InsideOneei=eo.newInsideOne();
CInsideOneei=EnclosingOne.newInsideOne();
D.EnclosingOneInsideOneei=eo.newInsideOne();
Answer:
a
C.4.
D.1)classSuper{
E.2)publicfloatgetNum(){return3.0f;}
F.3)}
G.4)
H.5)publicclassSubextendsSuper{
I.6)
J.7)}
K.whichmethod,placedatline6,willcauseacompilererror?
L.A.publicfloatgetNum(){return4.0f;}
M.B.publicvoidgetNum(){}
N.C.publicvoidgetNum(doubled){}
O.D.publicdoublegetNum(floatd){return4.0d;}
Answer:
B
5.
1)publicclassFoo{
2)publicstaticvoidmain(Stringargs[]){
3)try{return;}
4)finally{System.out.println("Finally");}
5)}
6)}
whatistheresult?
A.Theprogramrunsandprintsnothing.
B.Theprogramrunsandprints“Finally”.
C.Thecodecompiles,butanexceptionisthrownatruntime.
D.Thecodewillnotcompilebecausethecatchblockismissing.
Answer:
b
6.
//pointX
publicclassFoo{
publicstaticvoidmain(String[]args){
PrintWriterout=newPrintWriter(newjava.io.OutputStreamWriter(System.out),true);
out.println("Hello");
}
}
whichstatementatpointXonline1allowsthiscodetocompileandrun?
A.importjava.io.PrintWriter B.includejava.io.PrintWriter
C.importjava.io.OutputStreamWriter D.includejava.io.OutputStreamWriter
E.Nostatementisneeded
Answer:
a
7.whichthreearevaliddeclaractionofafloat?
A.floatfoo=-1;
B.floatfoo=1.0;
C.floatfoo=42e1;
D.floatfoo=2.02f;
E.floatfoo=3.03d;
F.floatfoo=0x0123;
Answer:
adf
8.
intindex=1;
intfoo[]=newint[3];
intbar=foo[index];
intbaz=bar+index;
whatistheresult?
A.bazhasavalueof0
B.bazhasvalueof1
C.bazhasvalueof2
D.anexceptionisthrown
E.thecodewillnotcompile
Answer:
b
9.
1)inti=1,j=10;
2)do{
3)if(i++>--j)continue;19283746
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
Answer:
d
10.
1)publicclassX{
2)publicObjectm(){
3)Objecto=newFloat(3.14F);
4)Object[]oa=newObject[1];
5)oa[0]=o;
6)o=null;
7)oa[0]=null;
8)System.out.println(oa[0]);
9)}
10)}
whichlineistheearliestpointtheobjectareferedisdefinitelyelibiletobegarbagecollectioned?
A.Afterline4
B.Afterline5
C.Afterline6
D.Afterline7
E.Afterline9
Answer:
d
11.
1.publicclassX{
2.publicstaticvoidmain(String[]args){
3.Objecto1=newObject();
4.Objecto2=o1;
5.if(o1.equals(o2)){
6.System.out.println("Equal");
7.}
8.}
9.}
Whatistheresult?
A.Theprogramrunsandprintsnothing.
B.Theprogramrunsandprints"Equal".
C.Anerroratline5causescompilationtofail.
D.Theprogramrunsbutabortswithanexception.
Answer:
B
12.
1)publicclassTest{
2)publicstaticvoidadd3(Integeri){
3)intval=i.intValue();
4)val+=3;
5)i=newInteger(val);
6)}
7)publicstaticvoidmain(Stringargs[]){
8)Integeri=newInteger(0);
9)add3(i);
10)System.out.println(i.intValue());
11)}
12)}
whatistheresult?
A.compilefail
B.printout"0"
C.printout"3"
D.compilesuccededbutexceptionatline3
Answer:
b
13.
Given:
1.publicclassFoo{
2.publicvoidmain(String[]args){
3.system.out.printIn(“HelloWorld.”);
4.}
5.}
Whatistheresult?
A.Anexceptionisthrown.
B.Thecodedoesnotcompile.
C.“HelloWorld.”isprintedtotheterminal.
D.Theprogramexitswithoutprintinganything.
AnswerA
14.Given:
13.publicclassFoo{
14.publicstaticvoidmain(String[]args){
15.StringBuffera=newStringBuffer(“A”);
16.StringBufferb=newStringBuffer(“B”);
17.operate(a,b);
18.system.out.printIn{a+“,”+b};
19.)
20.staticvoidoperate(StringBufferx,StringBuffery){
21.y.append(x);
22.y=x;
23.)
24.}
Whatistheresult?
A.Thecodecompilesandprints“A,B”.
B.Thecodecompilesandprints“A,BA”.
C.Thecodecompilesandprints“AB,B”.
D.Thecodecompilesandprints“AB,AB”.
E.Thecodecompilesandprints“BA,BA”.
F.Thecodedoesnotcompilebecause“+”cannotbeoverloadedforstringBuffer.
AnswerB
15.
Given:
1.publicclassSyncTest{
2.privateintx;
3.privateinty;
4.publicsynchronizedvoidsetX(inti)(x=1;)
5.publicsynchronizedvoidsetY(inti)(y=1;)
6.publicsynchronizedvoidsetXY(int1)(setX(i);setY(i);)
7.publicsynchronizedBooleancheck()(returnx!
=y;)
8.)
Underwhichconditionswillcheck()returntruewhencalledfromadifferentclass?
A.Check()canneverreturntrue.
B.Check()canreturntruewhensetXYiscalledbymultiplethreads.
C.Check()canreturntruewhenmultiplethreadscallsetXandsetYseparately.
D.Check()canonlyreturntrueifSyncTestischangedtoallowxandytobesetseparately.
Answer:
A
16.
1)publicclassTest{
2)publicstaticvoidmain(String[]args){
3)Stringfoo=args[1];
4)Sringbar=args[2];
5)Stringbaz=args[3];
6)}
7)}
javaTestRedGreenBlue
whatisthevalueofbaz?
A.bazhasvalueof""
B.bazhasvalueofnull
C.bazhasvalueof"Red"
D.bazhasvalueof"Blue"
E.bazhasvalueof"Green"
F.thecodedoesnotcompile
G.theprogramthrowanexception
Answer:
G
17.
1) interfaceFoo{
2) intk=0;
3) }
4)publicclassTestimplementsFoo{
5) publicstaticvoidmain(Stringargs[]){
6) inti;
7) Testtest=newTest();
8) i=test.k;
9) i=Test.k;
10) i=Foo.k;
11) }
12)}
Whatistheresult?
A.Compilationsucceeds.
B.Anerroratline2causescompilationtofail.
C.Anerroratline9causescompilationtofail.
D.Anerroratline10causescompilationtofail.
E.Anerroratline11causescompilationtofail.
Answer:
a
18.
classBaseClass{
privatefloatx=1.0f;
privatefloatgetVar(){returnx;}
}
classSubClassextendsBaseClass{
privatefloatx=2.0f;
//insertcode
}
whataretruetooverridegetVar()?
A.floatgetVar(){
B.publicfloatgetVar(){
C.publicdoublegetVar(){
D.protectedfloatgetVar(){
E.publicfloatgetVar(floatf){
Answer:
a,b,d
19.
Given:
inti=1,j=10;
do{
if(i>j)continue;
j--;
}while(++i<6);
whatarethevaleofiandj?
A.i=6,j=5
B.i=5,j=5
C.i=6,j=4
D.i=5,j=6
E.i=6,j=6
Answer:
A
20.
byte[]array1,array2[]
bytearray3[][]
byte[][]array4
ifeachhasbeeninitialized,whichstatementwillcauseacompileerror?
A.array2=array1;
B.array2=array3;
C.array2=array4;
D.bothAandB
E.bothAandC
F.bothBandC
Answer:
a
21.
whichfourtypesofobjectscanbethrownuse"throws"?
A.Error
B.Event
C.Object
D.Excption
E.Throwable
F.RuntimeException
Answer:
A,D,E,F
22.
1)publicclassTest{
2)publicstaticvoidmain(String[]args){
3)unsignedbyteb=0;
4)b--;
5)
6)}
7)}
whatisthevalueofbatline5?
A.-1B.255C.127D.compilefailE.compilesucceededbutrunerror
Answer:
d
23.
publicclassExceptionTest{
classTestExceptionextendsException{}
publicvoidrunTest()throwsTestException{}
publicvoidtest()/*pointx*/{
runTest();
}
}
Atpointx,whichcodecanbeaddontomakethecodecompile?
A.throwsException
B.catch(Exceptione)
C.throwsRuntimeException
D.catch(TestExceptione)
E.nocodeisnecessary
Answer:
A
24.
Stringfoo="blue";
boolean[]bar=newboolean[1];
if(bar[0]){
foo="green";
}
whatisthevalueoffoo?
A.""B.nullC.blueD.green
Answer:
C
25.
whichtwoareequivalent?
A.3/2
B.3<2
C.3*4
D.3<<2
E.3*2^2
F.3<<<2
Answer:
c,d
26.
intindex=1;
String[]test=newString[3];
Stringfoo=test[index];
whatistheresultoffoo?
A.foohasthevalue“”
B.foohasthevaluenull
C.anexceptionisthrown
D.thecodewillnotcompile
Answer:
b
27.
whichtwoaretrue?
A.staticinnerclassrequiresastaticinitializer
B.Astaticinnerclassrequiresaninstanceoftheenclosingclass
C.Astaticinnerclasshasnoreferencetoaninstanceoftheenclosingclass
D.Astaticinnerclasshasaccessstothenon-staticmemberoftheotherclass
E.staticmembersofastaticinnerclasscanbereferencedusingtheclassnameofthestaticinnerclass