Giventhefollowingcode,whichstatementsconcerningtheobjectsreferencedthroughthemembervariablesi,jandkaretrue,giventhatanythreadmaycallthemethodsa,bandcatanytime?()
classCounter{intv=0;
synchronizedvoidinc(){v++;}
synchronizedvoiddec(){v--;}
}
publicclassQ7ed5{
Counteri;
Counterj;
Counterk;
publicsynchronizedvoida(){
i.inc();
System.out.println("a");
i.dec();}
publicsynchronizedvoidb(){
i.inc();
j.inc();
k.inc();
System.out.println("b");
i.dec();
j.dec();
k.dec();}
publicvoidc(){
k.inc();
System.out.println("c");
k.dec();
}
}
第1题:
Giventhefollowingcode:publicclassTest{voidprintValue(intm){do{System.out.println("Thevalueis"+m);}while(--m>10)}publicstaticvoidmain(Stringarg[]){inti=10;Testt=newTest();t.printValue(i);}}Whichwillbeoutput?()
A.Thevalueis8
B.Thevalueis9
C.Thevalueis10
D.Thevalueis11
第2题:
Giventhefollowingcode,whichmethoddeclarations,wheninsertedattheindicatedposition,willnotcausetheprogramtofailcompilation?()publicclassQdd1f{publiclongsum(longa,longb){returna+b;}//insertnewmethoddeclarationhere}
A.publicintsum(inta,intb){returna+b;}
B.publicintsum(longa,longb){return0;}
C.abstractintsum();
D.privatelongsum(longa,longb){returna+b;}
E.publiclongsum(longa,intb){returna+b;}
第3题:
Giventhefollowingcode,whichcodefragments,wheninsertedattheindicatedlocation,willsucceedinmakingtheprogramdisplayabuttonspanningthewholewindowarea?()importjava.awt.*;publicclassQ1e65{publicstaticvoidmain(Stringargs[]){Windowwin=newFrame();Buttonbut=newButton("button");//insertcodefragmentherewin.setSize(200,200);win.setVisible(true);}}
A.win.setLayout(newBorderLayout());win.add(but);
B.win.setLayout(newGridLayout(1,1));win.add(but);
C.win.setLayout(newBorderLayout());win.add(but,BorderLayout.CENTER);
D.win.add(but);
E.win.setLayout(newFlowLayout());win.add(but);
第4题:
Giventhefollowingcode:if(x>0){System.out.println("first");}elseif(x>-3){System.out.println("second");}else{System.out.println("third");}Whichrangeofxvaluewouldprintthestring"second"?()
A.x>0
B.x>-3
C.x<=-3
D.x<=0&x>-3
x>0时打印"first",x>-3&&x<=0时打印"second",x<=-3时打印"third"。这个题目没有什么难的,只要理解if语句的语法就可以了。
第5题:
Giventhefollowingcode,writealineofcodethat,wheninsertedattheindicatedlocation,willmaketheoverridingmethodinExtensioninvoketheoverriddenmethodinclassBaseonthecurrentobject.
classBase{
publicvoidprint(){
System.out.println("base");
}
}
classExtentionextendsBase{
publicvoidprint(){
System.out.println("extension");
//insertlineofimplementationhere
}
}
publicclassQ294d{
publicstaticvoidmain(Stringargs[]){
Extentionext=newExtention();
ext.print();
}
}
Fillinasinglelineofimplementation.()