测试与答案4.docx
《测试与答案4.docx》由会员分享,可在线阅读,更多相关《测试与答案4.docx(26页珍藏版)》请在冰豆网上搜索。
测试与答案4
J@Whiz1.4---30道1.4模拟经典题(附详解)
1.Whatwillhappenwhenyouattempttocompileandrunthefollowingcode?
(Assumethatthecodeiscompiledandrunwithassertionsenabled.)
publicclassAssertTest{
publicvoidmethodA(inti){
asserti>=0:
methodB();/////此处要求表达式,返回值VOID,通不过编译。
System.out.println(i);
}
publicvoidmethodB(){ //无返回值
System.out.println("Thevaluemustnotbenegative");
}
publicstaticvoidmain(Stringargs[]){
AssertTesttest=newAssertTest();
test.methodA(-10);
}
}
A.itwillprint-10
B.itwillresultinAssertionErrorshowingthemessage-“thevaluemustnotbenegative”.
C.thecodewillnotcompile.
D.Noneofthese.
2.Whatwillhappenwhenyouattempttocompileandrunthefollowingcode?
D
publicclassStatic{
static{
intx=5; //在static内有效
}
staticintx,y; //初始化为0
publicstaticvoidmain(Stringargs[]){
x--; //-1
myMethod();
System.out.println(x+y+++x);
}
publicstaticvoidmyMethod(){
y=x+++++x; //y=-1+1 x=1
}
}
A.compiletimeerror
B.prints:
1
C.prints:
2
D.prints:
3
E.prints:
7
F.prints:
8
3.Giventhefollowingcode,whatwillbetheoutput?
B
classValue{
publicinti=15;
}
publicclassTest{
publicstaticvoidmain(Stringargv[]){
Testt=newTest();
t.first();
}
publicvoidfirst(){
inti=5;
Valuev=newValue();
v.i=25;
second(v,i);
System.out.println(v.i);
}
publicvoidsecond(Valuev,inti){
i=0;
v.i=20;
Valueval=newValue();
v=val;
System.out.println(v.i+""+i);
}
}
A.15020
B.15015
C.20020
D.01520
4.Whatwillhappenwhenyouattempttocompileandrunthefollowingcode?
classMyParent{
intx,y;
MyParent(intx,inty){
this.x=x;
this.y=y;
}
publicintaddMe(intx,inty){
returnthis.x+x+y+this.y;
}
publicintaddMe(MyParentmyPar){
returnaddMe(myPar.x,myPar.y);
}
}
classMyChildextendsMyParent{
intz;
MyChild(intx,inty,intz){
super(x,y);
this.z=z;
}
publicintaddMe(intx,inty,intz){
returnthis.x+x+this.y+y+this.z+z;
}
publicintaddMe(MyChildmyChi){
returnaddMe(myChi.x,myChi.y,myChi.z);
}
publicintaddMe(intx,inty){
returnthis.x+x+this.y+y;
}
}
publicclassMySomeOne{
publicstaticvoidmain(Stringargs[]){
MyChildmyChi=newMyChild(10,20,30);
MyParentmyPar=newMyParent(10,20);
intx=myChi.addMe(10,20,30);
inty=myChi.addMe(myChi);
intz=myPar.addMe(myPar);
System.out.println(x+y+z);
}
}
A.300
B.240
C.120
D.180
E.compileerror
F.noneoftheabove
5.TheclassAssertionErrorhas"is-a"relationshipwiththeseclasses(choosetwo)B,E
A.RuntimeException
B.Error
C.VirtualMachineError
D.IllegalAccessException
E.Throwable
6.Whatwillbetheresultofexecutingthefollowingcode?
D
1.booleana=true;
2.booleanb=false;
3.booleanc=true;
4.if(a==true)
5. if(b==true)
6. if(c==true)System.out.println("Somethingsaretrueinthisworld");
7. elseSystem.out.println("Nothingistrueinthisworld!
");
8. elseif(a&&(b=c)) //这里是赋值,不是比较
System.out.println("It'stooconfusingtotellwhatistrueandwhatisfalse");
9. else System.out.println("Heythiswon'tcompile");
A.Thecodewon’tcompile.
B.“somethingsaretrueinthisworld”willbeprinted
C.“heythiswon’tcompile”willbeprinted
D.Noneofthese
7.Whatwillhappenwhenyouattempttocompileandrunthefollowingcode?
C
interfaceMyInterface{}
publicclassMyInstanceTestimplementsMyInterface{
staticStrings;
publicstaticvoidmain(Stringargs[]){
MyInstanceTestt=newMyInstanceTest();
if(tinstanceofMyInterface){
System.out.println("Iamtrueinterface");
}else{
System.out.println("Iamfalseinterface");
}
if(sinstanceofString){
System.out.println("IamtrueString");
}else{
System.out.println("IamfalseString");
}
}
}
A.compiletimeerror
B.runtimeerror
C.prints:
“Iamtrueinterface”followedby“IamtrueString”
D.prints:
“Iamfalseinterface”followedby“IamfalseString”
E.prints:
“Iamtrueinterface”followedby“IamfalseString”
F.prints:
“Iamfalseinterface”followedby“IamtrueString”
8.Whatresultsfromattemptingtocompileandrunthefollowingcode?
D
publicclassTernary{
publicstaticvoidmain(Stringargs[]){
inta=5;
System.out.println("Valueis-"+((a<5)?
9.9:
9));
}
}
A.print:
Valueis-9
B.print:
Valueis-5
C.Compilationerror
D.Noneofthese
9.Inthefollowingpiecesofcode,AandDwillcompilewithoutanyerror.True/False?
FAL
A:
StringBuffersb1="abcd";
B:
Booleanb=newBoolean("abcd");
C:
byteb=255;
D:
intx=0x1234;
E:
floatfl=1.2;
10.Consideringthefollowingcode,Whichvariablesmaybereferencedcorrectlyatline12?
ABCE
1.publicclassOuter
2.{
3.publicinta=1;
4.privateintb=2;
5.publicvoidmethod(finalintc)
6.{
7.intd=3;
8.classInner
9.{
10.privatevoidiMethod(inte)
11.{
12.
13.}
14.}
15.}
16.}
abcde
11.Whatwillbetheresultofexecutingthefollowingcode?
C
publicstaticvoidmain(Stringargs[]){
chardigit='a';
for(inti=0;i<10;i++){
switch(digit){
case'x':
{
intj=0;
System.out.println(j);
}
default:
{
intj=100;
System.out.println(j);
}
}
}
inti=j;
System.out.println(i);
}
A.100willbeprinted11times.
B.100willbeprinted10timesandthentherewillbearuntimeexception
C.Thecodewillnotcompilebecausethevariableicannotbedeclaredtwicewithinthemani()method.
D.Thecodewillnotcompilebecausethevariablejcannotbedeclaredtwicewithintheswitchstatement.
E.Noneofthese.
12.Whichofthefollowingcollectionclassesfromjava.utilpackageareThreadsafe?
A
A.Vector
B.ArrayList //与Vector类似,只是不同步
C.HashMap
D.Hashtable
13.Whatwillhappenwhenyouattempttocompileandrunthefollowingcode?
CD
classMyThreadextendsThread{
publicvoidrun(){
System.out.println("MyThread:
run()");
}
publicvoidstart(){
System.out.println("MyThread:
start()");
}
}
classMyRunnableimplementsRunnable{
publicvoidrun(){
System.out.println("MyRunnable:
run()");
}
publicvoidstart(){
System.out.println("MyRunnable:
start()");
}
}
publicclassMyTest{
publicstaticvoidmain(Stringargs[]){
MyThreadmyThread = newMyThread();
MyRunnablemyRunnable=newMyRunnable();
Threadthread = newThread(myRunnable);
myThread.start();
thread.start();
}
}
A.prints:
MyThread:
start()followedbyMyRunnable:
run()
B.prints:
MyThread:
run()followedbyMyRunnable:
start()
C.prints:
MyThread:
start()followedbyMyRunnable:
start()
D.prints:
MyThread:
run()followedbyMyRunnable:
run()
E.compiletimeerror
F.Noneoftheabove
14.Whatwillbetheresultofexecutingthefollowingcode?
D
//Filename;SuperclassX.java
packagepackageX;
publicclassSuperclassX{
protectedvoidsuperclassMethodX(){}
intsuperclassVarX;
}
//FilenameSubclassY.java
1.packagepackageX.packageY;
2.
3.publicclassSubclassYextendsSuperclassX
4.{
5.SuperclassXobjX=newSubclassY();
6.SubclassYobjY=newSubclassY();
7.voidsubclassMethodY()
8.{
9.objY.superclassMethodX();
10.inti;
11.i=objY.superclassVarX;
12.}
13.}
A.Compileerroratline5.
B.Compileerroratline9.
C.Runtimeexceptionatline11.
D.Noneofthese
15.Considertheclasshierarchyshownbelow:
E
FourWheeler
(implementsDrivingUtilities)
//\\
/ / \ \
/ / \ \
/ / \ \
/ / \ \
Car Truck Bus Crane
Considerthefollowingcodebelow:
1.DrivingUtilitiesdu;
2.FourWheelerfw;
3.TruckmyTruck=newTruck();
4.du=(DrivingUtilities)myTruck;
5.fw=newCrane();
6.fw=du;
Whichofthestatementsbelowaretrue?
A.Line4willnotcompilebecauseaninterfacecannotrefertoanobject.
B.Thecodewillcompileandrun.
C.Thecodewillnotcompilewithoutanexplicitcastatline6,becausegoingdownthehierarchywithoutcastingisnotallowed.
D.Thecodeatline4willcompileevenwithouttheexplicitcast.
E.Thecodewillcompileifweputanexplicitcastatline6butwillthrowanexceptionatruntime.
16.Whatresultsfromthefollowingcode?
D
1.classMyClass
2.{
3.voidmyMethod(inti){System.out.println("intversion");}
4.voidmyMethod(Strings){System.out.println("Stringversion");}
5.publicstaticvoidmain(Stringargs[])
6.{
7.MyClassobj=newMyClass();
8.charch='c';
9.obj.myMethod(ch);
10.}
11.}
A.Line4willnotcompileasvoidmethodcan’teoverridden.
B.Anexceptionatline9.
C.Line9willnotcompileasthereisnoversionofmyMethodwhichtakesacharasargument.
D.Thecodecompilesandproducesoutput:
intversion
E.Thecodecompilesandproducesoutput:
Stringversion
17.Whatistheresultwhenyoucompileandrunthefollowingcode?
D
publicclassThrowsDemo{
staticvoidthrowMethod(){
System.out.println("InsidethrowMethod.");
thrownewIllegalAccessException("demo");
}
publicstaticvoidmain(Stringargs[]){
try{
throwMethod();
}catch(IllegalAccessExceptione){
System.out.println("Caught"+e);
}
}
}
A.compileerror
B.runtimeerror
C.compilesuccessfully,nothingisprinted.
D.insidethrowMethodfollowedbycaught:
java.lang.IllegalAccessException:
demo
18.Whatwillbeprintedwhenyouexecutethefollowingcode?
C
classX{
Yb=newY();
X(){
System.out.print("X");
}
}
classY{
Y(){
System.out.print("Y");
}
}
publicclassZextendsX{
Yy=newY();
Z(){
System.out.print("Z");
}