Java试题4.docx
《Java试题4.docx》由会员分享,可在线阅读,更多相关《Java试题4.docx(15页珍藏版)》请在冰豆网上搜索。
Java试题4
JavaText(四)
1.Whichofthefollowinglineswillcompilewithoutwarningorerror.
1)floatf=;
2)charc="a";
3)byteb=257;
4)booleanb=null;
5)inti=10;
2.Whatwillhappenifyoutrytocompileandrunthefollowingcode
publicclassMyClass{
publicstaticvoidmain(Stringarguments[]){
amethod(arguments);
}
publicvoidamethod(String[]arguments){
}
}
1)errorCan'tmakestaticreferencetovoidamethod.
2)errormethodmainnotcorrect
3)errorarraymustincludeparameter
4)amethodmustbedeclaredwithString
3.Whichofthefollowingwillcompilewithouterror
1)import.*;
packageMypackage;
classMyclass{}
2)packageMyPackage;
import.*;
classMyClass{}
3)/*Thisisacomment*/
packageMyPackage;
import.*;
classMyClass{}
4.Whatwillbeprintedoutifthiscodeisrunwiththefollowingcommandline
javamyproggoodmorning
publicclassmyprog{
publicstaticvoidmain(Stringargv[])
{
}
}
1)myprog
2)good
3)morning
4)Exceptionraised:
"2"
5.Whatwillhappenwhenyoucompileandrunthefollowingcode
publicclassMyClass{
staticinti;
publicstaticvoidmain(Stringargv[]){
}
}
1)ErrorVariableimaynothavebeeninitialized
2)null
3)1
4)0
6.Whatwillhappenifyoutrytocompileandrunthefollowingcode
publicclassQ{
publicstaticvoidmain(Stringargv[]){
intanar[]=newint[]{1,2,3};
}
}
1)1
2)Erroranarisreferencedbeforeitisinitialized
3)2
4)Error:
sizeofarraymustbedefined
7.Whatwillhappenifyoutrytocompileandrunthefollowingcode
publicclassQ{
publicstaticvoidmain(Stringargv[]){
intanar[]=newint[5];
}
}
1)Error:
anarisreferencedbeforeitisinitialized
2)null
3)0
4)5
8.Whatwillbetheresultofattemptingtocompileandrunthefollowingcode
abstractclassMineBase{
abstractvoidamethod();
staticinti;
}
publicclassMineextendsMineBase{
publicstaticvoidmain(Stringargv[]){
int[]ar=newint[5];
for(i=0;i<;i++)
}
}
1)asequenceof50'swillbeprinted
2)Error:
arisusedbeforeitisinitialized
3)ErrorMinemustbedeclaredabstract
4)IndexOutOfBoundesError
9.Whatwillbeprintedoutifyouattempttocompileandrunthefollowingcode
inti=1;
switch(i){
case0:
"zero");
break;
case1:
"one");
case2:
"two");
default:
"default");
}
1)one
2)one,default
3)one,two,default
4)default
10.Whichofthefollowinglinesofcodewillcompilewithouterror
1)inti=0;
if(i){
"Hello");
}
2)booleanb=true;
booleanb2=true;
if(b==b2){
"Sotrue");
}
3)inti=1;
intj=2;
if(i==1||j==2)
"OK");
4)inti=1;
intj=2;
if(i==1&|j==2)
"OK");
11.Whatwillbeoutputifyoutrytocompileandrunthefollowingcode,butthereisnofilecalledinthecurrentdirectory.
import.*;
publicclassMine
{
publicstaticvoidmain(Stringargv[])
{
Minem=newMine();
}
publicintamethod()
{
try
{
FileInputStreamdis
=newFileInputStream("");
}
catch(FileNotFoundExceptionfne)
{
"Nosuchfilefound");
return-1;
}
catch(IOExceptionioe)
{}
finally
{
"Doingfinally");
}
return0;
}
}
1)Nosuchfilefound
2Nosuchfilefound,-1
3)Nosuchfilefound,Doingfinally,-1
4)0
ofthefollowingstatementsaretrue
1)Methodscannotbeoverridentobemoreprivate
2)staticmethodscannotbeoverloaded
3)privatemethodscannotbeoverloaded
4)Anoverloadedmethodcannotthrowexceptionsnotcheckedinthebaseclass
willhappenifyouattempttocompileandrunthefollowingcode
classBase{}
classSubextendsBase{}
classSub2extendsBase{}
publicclassCEx{
publicstaticvoidmain(Stringargv[]){
Baseb=newBase();
Subs=(Sub)b;
}
}
1)Compileandrunwithouterror
2)CompiletimeException
3)RuntimeException
ofthefollowingstatementsaretrue
1)-1>>>2);willoutputaresultlargerthan10
2)-1>>>2);willoutputapositivenumber
3)2>>1);willoutputthenumber1
4)1<<<2);willoutputthenumber4
willhappenwhenyouattempttocompileandrunthefollowingcode
publicclassTuxextendsThread{
staticStringsName="vandeleur";
publicstaticvoidmain(Stringargv[]){
Tuxt=newTux();
(sName);
}
publicvoidpiggy(StringsName){
sName=sName+"wiggy";
start();
}
publicvoidrun(){
for(inti=0;i<4;i++){
sName=sName+""+i;
}
}
}
1)Compiletimeerror
2)Compilationandoutputof"vandeleurwiggy"
3)Compilationandoutputof"vandeleurwiggy0123"
4)Compilationandoutputofeither"vandeleur","vandeleur0","vandeleur01""vandaleur012"or"vandaleur0123"
willbedisplayedwhenyouattempttocompileandrunthefollowingcode
;
publicclassButtextendsFrame{
publicstaticvoidmain(Stringargv[]){
ButtMyBut=newButt();
}
Butt(){
ButtonHelloBut=newButton("Hello");
ButtonByeBut=newButton("Bye");
add(HelloBut);
add(ByeBut);
setSize(300,300);
setVisible(true);
}
}
4)Thestring"Dolly"willbeshownatthebottomoftheframe.
willbetheresultwhenyouattempttocompilethisprogram
publicclassRand{
publicstaticvoidmain(Stringargv[]){
intiRand;
iRand=();
}
}
1)Compiletimeerrorreferringtoacastproblem
2)Arandomnumberbetween1and10
3)Arandomnumberbetween0and1
4)Acompiletimeerroraboutrandombeinganunrecognisedmethod
thefollowingcode
import.*;
publicclassTh{
publicstaticvoidmain(Stringargv[]){
Tht=newTh();
();
}
publicvoidamethod(){
try{
ioCall();
}catch(IOExceptionioe){}
}
}
WhatcodewouldbemostlikelyforthebodyoftheioCallmethod
1)publicvoidioCall()throwsIOException{
DataInputStreamdin=newDataInputStream;
();
}
2)publicvoidioCall()throwIOException{
DataInputStreamdin=newDataInputStream;
();
}
3)publicvoidioCall(){
DataInputStreamdin=newDataInputStream;
();
}
4)publicvoidioCallthrowsIOException(){
DataInputStreamdin=newDataInputStream;
();
}
willhappenwhenyoucompileandrunthefollowingcode
publicclassScope{
privateinti;
publicstaticvoidmain(Stringargv[]){
Scopes=newScope();
();
}YouaregoingtousetheGridBagLayoutmanagertocontrolthewaythebuttonsaresetout.HowwillyoumodifythewaytheGridBagLayoutactsinordertochangethespacingaroundthefirstbutton
1)CreateaninstanceoftheGridBagConstraintsclass,calltheweightx()methodandthenpasstheGridBagConstraintsinstancewiththecomponenttothesetConstraintsmethodoftheGridBagLayoutclass.
2)CreateaninstanceoftheGridBagConstraintsclass,settheweightxfieldandthenpasstheGridBagConstraintsinstancewiththecomponenttothesetConstraintsmethodoftheGridBagLayoutclass.
3)CreateaninstanceoftheGridBagLayoutclass,settheweightxfieldandthencallthesetConstraintsmethodoftheGridBagLayoutClasswiththecomponentasaparameter.
4)CreateaninstanceoftheGridBagLayoutclass,callthesetWeightx()methodandthenpasstheGridBagConstraintsinstancewiththecomponenttothesetConstraintsmethodoftheGridBagLayoutclass.
ofthefollowingcanyouperformusingtheFileclass
1)Changethecurrentdirectory
2)Returnthenameoftheparentdirectory
3)Deleteafile
4)Findifafilecontainstextorbinaryinformation
statementistrueofthefollowingcode
publicclassRpcraven{
publicstaticvoidmain(Stringargv[]){
Pmcravenpm1=newPmcraven("One");
();
Pmcravenpm2=newPmcraven("Two");
();
}
}
classPmcravenextendsThread{
privateStringsTname="";
Pmcraven(Strings){
sTname=s;
}
publicvoidrun(){
for(inti=0;i<2;i++){
try{
sleep(1000);
}catch(InterruptedExceptione){}
yield();
}
}
}
1)Compiletimeerror,classRpcravendoesnotimportOutputofOneOneTwoTwo
3)OutputofOneTwoOneTwo
4)Compilationbutnooutputatruntime
areconcernedthatyourprogrammayattempttousemorememorythanisavailable.ToavoidthissituationyouwanttoensurethattheJavaVirtualMachinewillrunitsgarbagecollectionjustbeforeyoustartacomplexroutine.Whatcanyoudotobecertainthatgarbagecollectionwillrunwhenyouwant.
1)Youcannotbecertainwhengarbagecollectionwillrun
2)Usethe()methodtoforcegarbagecollection
3)Ensurethatallthevariablesyourequiretobegarbagecollectedaresettonull
4)Usethe()methodtoforcegarbagecollection
28、Whichstatementsaboutthegarbagecollectionaretrue
1.Theprogramdevelopermustcreateathreadtoberesponsibleforfreethememory.
2.Thegarbagecollectionwillcheckforandfreememorynolongerneeded.
3.Thegarbagecollectionallowtheprogramdevelopertoexplicityandimmediatelyfreethememory.
4.Thegarbagecollectioncanfreethememoryusedjavaobjectatexpecttime.
havethesefilesinthesamedirectory.Whatwillhappenwhenyouattempttocompileandrunifyouhavenotalreadycompiled
//
packageBase;
classBase{
protectedvoidamethod(){
"amethod");
}//Endofamethod
}//Endofclassbase
packageClass1;
//
publicclassClass1extendsBase{
publicstaticvoidmain(Stringargv[]){
Baseb=newBase();
();
}//Endofmain
}//EndofClass1
1)CompileError:
MethodsinBasenotfound
2)CompileError:
Unabletoaccessprotectedmethodinbaseclass
3)Compilationfollowedbytheoutput"amethod"
4)Compileerror:
Superclassofclassnotfound
willhappenwhenyouattempttocompileandrunthefollowingcode
classBase{
privatevoidamethod(intiBas