}
}
Selectallrightanswer:
A、acompilererror
B、arunningerror
C、true
D、false
考察关系运算,不能用0替代false
5、whatistheresultwhenyoucompileandrunthefollowingcode?
A
ClassExample
{
Publicstaticvoidmain(String[]args)
{
Shorts=newShort(“5”);
Booleanb;
//insertcodehere
}
}
Which,insertedindependentlyat//insertcodehere?
willcompile?
BD
Selectallrightanswer:
Ab=(Numberinstanceofs);
Bb=(sinstanceofShort);
Cb=sinstanceof(Short);
Db=(sinstanceofNumber);
Eb=sinstanceof(Object);
Fb=(sinstanceofString);
考察instanceof运算符,A写反了,C.E语法错误F类型不兼容
6、whichofthefollowinglinesofcodewillcompilewithouterror?
BC
A
inti=0;
if(i)System.out.println(“Hi”);
B
booleanb=true;
booleanb2=true;
if(b==b2)System.out.println(“sotrue”);
C
inti=1;
intj=2;
if(i==1||j==2)System.out.println(“OK”);
D
inti=1;
intj=2;
if(i==1&|j==2)System.out.println(“OK”);
本题考察boolean表达式,A错在不能用数字充当boolean型值,D错在逻辑操作非法
7、whatistheresultwhenyoucompileandrunthefollowingcode?
BD
ClassExample
{
Publicstaticvoidmain(String[]args)
{
Floatf=newFloat(4.2f);
Floatc;
Doubled=newDouble(4.2);
floatf1=4.2f;
c=f;
}
}
Selectallrightanswer:
Af.equals(d)Bc==fCc==dDc.equals(f)
==比较内存地址,equals比较内容
8、whatistheresultwhenyoucompileandrunthefollowingcode?
D
ClassExample
{
Publicstaticvoidmain(String[]args)
{
bytex=--64;
bytey=--6;
System.out.println(x/y+””+x%y);
}
}
Selectallrightanswer:
AacompilererrorBaruntimeerrorC104D10-4
9、whatistheresultwhenyoucompileandrunthefollowingcode?
G
ClassExample
{
publicstaticvoidmain(String[]args)
{
longx=42L;
longx=44L;
System.out.println(““+7+2+”“);
System.out.println(foo()+x+5+”“);
System.out.println(x+y+foo());
}
staticStringfoo()
{return“foo”;
}
}
Selectallrightanswer:
A、9foo4786fooB、9foo474244fooC、9foo42586foo
D、9foo4254244fooE、72foo4786fooF、72foo474244foo
G、72foo42586fooH、72foo4254244fooI、compilationfails
本题考察字符串连接,,两个操作数中有一个以上字符串就进行连接,俩操作数都是数值时做+运算
10、whatistheresultwhenyoucompileandrunthefollowingcode?
A
ClassExample
{
publicstaticvoidmain(String[]args)
{
Strings1=”Amit”;
Strings2=”Amit”;
Strings3=newString(“abcd”);
Strings4=newString(“abcd”);
System.out.println(s1.equals(s2));
System.out.println((s1==s2));
System.out.println(s3.equals(s4));
Strings3=newString((s3==s4));
}
}
Selectallrightanswer:
AtruetruetruefalseBtruetruetruetrueCtruefalsetruefalse
S1和S2内容一样,引用也一样,因为不是new的就都是从字符串池中取的,s3,s4内容一样,但不是指同一对象
11、whatistheresultwhenyoucompileandrunthefollowingcode?
B
charc=’c’;
inti=10;
doubled=10;
longl=1;
Strings=”Hello”
Selectallrightanswer:
Ac=c+iBs+=iCi+=sDc+s
+=符号左侧应是字符串
12、whichofthefollowingreturnstruewhenreplacewith*****?
A
ClassExample
{
publicstaticvoidmain(String[]args)
{
Buttonb=newButton(“Button”);
if(*******)
System.out.println(“thisisaninstanceofButton”);
}
}
Selectallrightanswer:
A、binstanceofButtonB、ButtoninstancofbC、b==ButtonD、Button==(Object)b
考察比较运算符,
13、whatistheresultwhenyoucompileandrunthefollowingcode?
A
importjava.awt.*;
ClassExample
{
publicstaticvoidmain(String[]args)
{
try
{
Button[]var=newButton[5];
System.out.println(varinstanceof[]);
}
catch(Exceptione)
{
System.out.println(“Exceptionthrown”);
}
}
}
Selectallrightanswer:
AacompilererrorBExceptionthrownCtrueDflaseEnoneoftheabove
Instanceof右侧应该是类,接口或数组类型
14、whatistheresultwhenyoucompileandrunthefollowingcode?
D132
importjava.awt.*;
ClassExample
{
publicstaticvoidmain(String[]args)
{
intOutput=10;
booleanb1=false;
if((b1==true)&&(Output+=10)==20))
{
System.out.println(“weareequal”+Output);
}
else
System.out.println(“Notequal”+Output);
}
}
Selectallrightanswer:
Acompileerror,attemptingtoperformbinarycomparisononlogicaldatatype
BCompilationandoutputof“weareequal10”
Ccompilationandoutputof“notequal20”
Dcompilationandoutputof“notequal10”
考察逻辑短路因为逻辑短路,所以(Output+=1没被执行
15、whatistheresultwhenyoucompileandrunthefollowingcode?
F145
ClassExample
{Strings;
publicstaticvoidmain(String[]args)
{Exampleks=newExample();
intj,i;
i=ks.hai();
j=ks.hello();
System.out.println(i+””+j);
}
inthai()
{
if((s==null)||(s.length()==0)){return10;}
elsereturn0;
}
inthello()
{
if((s==null)||(s.length()==20)){return10;}
elsereturn0;
}
}
Selectallrightanswer:
A1010B10nullC00DcompilationerrorEAnexceptioninhaiFanexceptoninhello
考察逻辑短路,|不属于短路运算,所以s.lengh()会被执行,因为s为null,所以抛出异常
课后作业:
1.Given:
classHexy{
publicstaticvoidmain(String[]args){
Integeri=42;
Strings=(i<40)?
"life":
(i>50)?
"universe":
"everything";
System.out.println(s);
}
}
Whatistheresult?
A.null
B.life
C.universe
D.everything
E.Compilationfails.
F.Anexceptionisthrownatruntime.
Answer:
®3Discorrect.Thisisaternarynestedinaternarywithalittleunboxingthrownin.
Bothoftheternaryexpressionsarefalse.
®˚A,B,C,E,andFareincorrectbasedontheabove.
(Objective7.6)
2.Given:
1.classExample{
2.publicstaticvoidmain(String[]args){
3.Shorts=15;
4.Booleanb;
5.//insertcodehere
6.}
7.}
Which,insertedindependentlyatline5,willcompile?
(Chooseallthatapply.)
A.b=(Numberinstanceofs);
B.b=(sinstanceofShort);
SelfTestAnswers307
C.b=s.instanceof(Short);
D.b=(sinstanceofNumber);
E.b=s.instanceof(Object);
F.b=(sinstanceofString);
Answer:
®3BandDcorrectlyuseboxingandinstanceoftogether.
®˚Aisincorrectbecausetheoperandsarereversed.CandEuseincorrectinstanceofsyntax.
FiswrongbecauseShortisn'tinthesameinheritancetreeasString.
(Objective7.6)
3.Given:
1.classComp2{
2.publicstaticvoidmain(String[]args){
3.floatf1=2.3f;
4.float[][]f2={{42.0f},{1.7f,2.3f},{2.6f,2.7f}};
5.float[]f3={2.7f};
6.Longx=42L;
7.//insertcodehere
8.System.out.println("true");
9.}
10.}
Andthefollowingfivecodefragments:
F1.if(f1==f2)
F2.if(f1==f2[2][1])
F3.if(x==f2[0][0])
F4.if(f1==f2[1,1])
F5.if(f3==f2[2])
Whatistrue?
A.Oneofthemwillcompile,onlyonewillbetrue.
B.Twoofthemwillcompile,onlyonewillbetrue.
C.Twoofthemwillcompile,twowillbetrue.
D.Threeofthemwillcompile,onlyonewillbetrue.
E.Threeofthemwillcompile,exactlytwowillbetrue.
F.Threeofthemwillcompile,exactlythreewillbetrue.
308Chapter4:
Operators
Answer:
®3Discorrect.FragmentsF2,F3,andF5willcompile,andonlyF3istrue.
®˚A,B,C,E,andFareincorrect.F1isincorrectbecauseyoucan’tcompareaprimitiveto
anarray.F4isincorrectsyntaxtoaccessanelementofatwo-dimensionalarray.
(Objective7.6)
4.Given:
classFork{
publicstaticvoidmain(String[]args){
if(args.length==1|args[1].equals("test")){
System.out.println("testcase");
}else{
System.out.println("production"+args[0]);
}
}
}
Andthecommand-lineinvocation:
javaForklive2
Whatistheresult?
A.testcase
B.production
C.testcaselive2
D.Compilationfails.
E.Anexceptionisthrownatruntime.
Answer:
®3Eiscorrect.Becausetheshortcircuit(||)isnotused,bothoperandsareevaluated.Since
args[1]ispasttheargsarraybounds,anArrayIndexOutOfBoundsExceptionisthrown.
®˚A,B,C,andDareincorrectbasedontheabove.
(Objective7.6)
SelfTestAnswers309
5.Given:
classFoozit{
publicstaticvoidmain(String[]args){
Integerx=0;
Integery=0;
for(Shortz=0;z<5;z++)
if((++x>2)||(++y>2))
x++;
System.out.println(x+""+y);
}
}
Whatistheresult?
A.51
B.52
C.53
D.81
E.82
F.83
G.102
H.103
Answer:
®3Eiscorrect.Thefirsttwotimestheiftestruns,bothxandyareincrementedonce(the
x++isnotreacheduntilthethirditeration).Startingwiththethirditerationoftheloop,
yisnevertouchedagain,becauseoftheshort-circuitoperator.
®˚A,B,C,D,F,G,andHareincorrectbasedontheabove.
(Objective7.6)
6.Given:
classTitanic{
publicstaticvoidmain(String[]args){
Booleanb1=true;
booleanb2=false;
booleanb3=true;
if((b1&b2)|(b2&b3)&b3)
System.out.print("alpha");
if((b1=false)|(b1&b3)|(b1|b2))
System.out.print("beta");
}
}
310Chapter4:
Operators
Whatistheresult?
A.beta
B.alpha
C.alphabeta
D.Compilation