javaSCJP.docx
《javaSCJP.docx》由会员分享,可在线阅读,更多相关《javaSCJP.docx(10页珍藏版)》请在冰豆网上搜索。
javaSCJP
question1)
whatwillhappenwhenyouattempttocompileandrunthiscode?
abstractclassbase{
abstractpublicvoidmyfunc();
publicvoidanother(){
system.out.println("anothermethod");
}
}
publicclassabsextendsbase{
publicstaticvoidmain(stringargv[]){
absa=newabs();
a.amethod();
}
publicvoidmyfunc(){
system.out.println("myfunc");
}
publicvoidamethod(){
myfunc();
}
}
1)thecodewillcompileandrun,printingoutthewords"myfunc"
2)thecompilerwillcomplainthatthebaseclasshasnonabstractmethods
3)thecodewillcompilebutcomplainatruntimethatthebaseclasshasnonabstractmethods
4)thecompilerwillcomplainthatthemethodmyfuncinthebaseclasshasnobody,nobodyatalltoloooveit
question2)
whatwillhappenwhenyouattempttocompileandrunthiscode?
publicclassmymain{
publicstaticvoidmain(Stringargv){
System.out.println("hellocruelworld");
}
}
1)thecompilerwillcomplainthatmainisareservedwordandcannotbeusedforaclass
2)thecodewillcompileandwhenrunwillprintout"hellocruelworld"
3)thecodewillcompilebutwillcomplainatruntimethatnoconstructorisdefined
4)thecodewillcompilebutwillcomplainatruntimethatmainisnotcorrectlydefined
question5)
whymightyoudefineamethodasnative?
1)togettoaccesshardwarethatjavadoesnotknowabout
2)todefineanewdatatypesuchasanunsignedinteger
3)towriteoptimisedcodeforperformanceinalanguagesuchasc/c++
4)toovercomethelimitationoftheprivatescopeofamethod
question6)
whatwillhappenwhenyouattempttocompileandrunthiscode?
classbase{
publicfinalvoidamethod(){
System.out.println("amethod");
}
}
publicclassfinextendsbase{
publicstaticvoidmain(Stringargv[]){
baseb=newbase();
b.amethod();
}
}
1)compiletimeerrorindicatingthataclasswithanyfinalmethodsmustbedeclaredfinalitself
2)compiletimeerrorindicatingthatyoucannotinherit(继承)fromaclasswithfinalmethods
3)runtimeerrorindicatingthatbaseisnotdefinedasfinal
4)successincompilationandoutputof"amethod"atruntime
question7)
whatwillhappenwhenyouattempttocompileandrunthiscode?
publicclassmod{
publicstaticvoidmain(Stringargv[]){
}
publicstaticnativevoidamethod();
}
1)erroratcompilation:
nativemethodcannotbestatic
2)erroratcompilationnativemethodmustreturnvalue
3)compilationbuterroratruntimeunlessyouhavemadecodecontainingnativeamethodavailable
4)compilationandexecutionwithouterror
question8)
whatwillhappenwhenyouattempttocompileandrunthiscode?
privateclassbase{}
publicclassvis{
transientintival;
publicstaticvoidmain(Stringelephant[]){
}
}
1)compiletimeerror:
basecannotbeprivate
2)compiletimeerrorindicatingthatanintegercannotbetransient
3)compiletimeerrortransientnotadatatype
4)compiletimeerrormalformedmainmethod
question9)
whathappenswhenyouattempttocompileandrunthesetwofilesinthesamedirectory?
//filep1.java
packagemypackage;
classp1{
voidafancymethod(){
system.out.println("whatafancymethod");
}
}
//filep2.java
publicclassp2extendsp1{
publicstaticvoidmain(Stringargv[]){
p2p2=newp2();
p2.afancymethod();
}
}
1)bothcompileandp2outputs"whatafancymethod"whenrun
2)neitherwillcompile
3)bothcompilebutp2hasanerroratruntime
4)p1compilescleanlybutp2hasanerroratcompiletime
question10)
youwanttofindoutthevalueofthelastelementofanarray.youwritethefollowingcode.whatwillhappenwhenyoucompileandrunit.?
publicclassmyar{
publicstaticvoidmain(Stringargv[]){
int[]i=newint[5];
System.out.println(i[5]);
}
}
1)anerroratcompiletime
2)anerroratruntime
3)thevalue0willbeoutput
4)thestring"null"willbeoutput
question18)
whatwillhappenwhenyouattempttocompileandrunthefollowingcode?
publicclassbgroundextendsthread{
publicstaticvoidmain(stringargv[]){
bgroundb=newbground();
b.run();
}
publicvoidstart(){
for(inti=0;i<10;i++){
system.out.println("valueofi="+i);
}
}
}
1)acompiletimeerrorindicatingthatnorunmethodisdefinedforthethreadclass
2)aruntimeerrorindicatingthatnorunmethodisdefinedforthethreadclass
3)cleancompileandatruntimethevalues0to9areprintedout
4)cleancompilebutnooutputatruntime
question25)
whatwillhappenwhenyouattempttocompileandrunthefollowingcode
publicclasshope{
publicstaticvoidmain(Stringargv[]){
hopeh=newhope();
}
protectedhope(){
for(inti=0;i<10;i++){
System.out.println(i);
}
}
}
1)compilationerror:
constructorscannotbedeclaredprotected
2)runtimeerror:
constructorscannotbedeclaredprotected
3)compilationandrunningwithoutput0to10
4)compilationandrunningwithoutput0to9
question26)
whatwillhappenwhenyouattempttocompileandrunthefollowingcode
publicclassmyswitch{
publicstaticvoidmain(Stringargv[]){
myswitchms=newmyswitch();
ms.amethod();
}
publicvoidamethod(){
intk=10;
switch(k){
default:
//putthedefaultatthebottom,nothere
system.out.println("thisisthedefaultoutput");
break;
case10:
system.out.println("ten");
case20:
system.out.println("twenty");
break;
}
}
}
1)noneoftheseoptions
2)compiletimeerrortargetofswitchmustbeanintegraltype
3)compileandrunwithoutput"thisisthedefaultoutput"
4)compileandrunwithoutputofthesingleline"ten"
question28)
whatwillhappenwhenyouattempttocompileandrunthefollowingcode
publicclassas{
inti=10;
intj;
charz=1;
booleanb;
publicstaticvoidmain(Stringargv[]){
asa=newas();
a.amethod();
}
publicvoidamethod(){
System.out.println(j);
System.out.println(b);
}
}
1)compilationsucceedsandatruntimeanoutputof0andfalse
2)compilationsucceedsandatruntimeanoutputof0andtrue
3)compiletimeerrorbisnotinitialised
4)compiletimeerrorzmustbeassignedacharvalue
question29)
whatwillhappenwhenyouattempttocompileandrunthefollowingcodewiththecommandline"hellothere"
publicclassarg{
string[]myarg;
publicstaticvoidmain(stringargv[]){
myarg=argv;
}
publicvoidamethod(){
System.out.println(argv[1]);
}
}
1)compiletimeerror
2)compilationandoutputof"hello"
3)compilationandoutputof"there"
4)noneoftheabove
question30)
whatwillhappenwhenyouattempttocompileandrunthefollowingcode
publicclassstreq{
publicstaticvoidmain(stringargv[]){
streqs=newstreq();
}
privatestreq(){
strings="marcus";
strings2=newstring("marcus");
if(s==s2){
system.out.println("wehaveamatch");
}else{
system.out.println("notequal");
}
}
}
1)compiletimeerrorcausedbyprivateconstructor
2)outputof"wehaveamatch"
3)outputof"notequal"
4)compiletimeerrorbyattemptingtocomparestringsusing==
question32)
whatwillhappenwhenyouattempttocompileandrunthisprogram
publicclassouter{
publicstringname="outer";
publicstaticvoidmain(stringargv[]){
inneri=newinner();
i.showname();
}//endofmain
privateclassinner{
stringname=newstring("inner");
voidshowname(){
system.out.println(name);
}
}//endofinnerclass
}
1)compileandrunwithoutputof"outer"
2)compileandrunwithoutputof"inner"
3)compiletimeerrorbecauseinnerisdeclaredasprivate
4)compiletimeerrorbecauseofthelinecreatingtheinstanceofinner
question34)
whichoptionmostfullydescribeswillhappenwhenyouattempttocompileandrunthefollowingcode
publicclassmyar{
publicstaticvoidmain(stringargv[]){
myarm=newmyar();
m.amethod();
}
publicvoidamethod(){
staticinti;
system.out.println(i);
}
}
1)compilationandoutputofthevalue0
2)compiletimeerrorbecauseihasnotbeeninitialized
3)compilationandoutputofnull
4)compiletimeerror
question35)
whichofthefollowingwillcompilecorrectly
1)shortmyshort=99s;
2)stringname='excellenttutorialmrgreen';
3)charc=17c;
4)intz=015;
question38)
giventhefollowingmainmethodinaclasscalledcycleandacommandlineof
javacycleonetwo
whatwillbeoutput?
publicstaticvoidmain(stringbicycle[]){
system.out.println(bicycle[0]);
}
1)noneoftheseoptions
2)cycle
3)one
4)two
question41)
giventhefollowingcode
classbase{}
publicclassmycastextendsbase{
staticbooleanb1=false;
staticinti=-1;
staticdoubled=10.1;
publicstaticvoidmain(stringargv[]){
mycastm=newmycast();
baseb=newbase();
//here
}
}
whichofthefollowing,ifinsertedatthecomment//herewillallowthecodetocompileandrunwithouterror
1)b=m;
2)m=b;
3)d=i;
4)b1=i;
question45)
whatwillhappenwhenyouattempttocompileandrunthefollowingcode
intoutput=10;
booleanb1=false;
if((b1==true)&&((output+=10)==20)){
system.out.println("weareequal"+output);
}else
{
system.out.println("notequal!
"+output);
}
1)compileerror,attemptingtopeformbinarycomparisononlogicaldatatype
2)compilationandoutputof"weareequal10"
3)compilationandoutputof"notequal!
20"
4)compilationandoutputof"notequal!
10"
question48)
giventhefollowingvariables
charc='c';
inti=10;
doubled=10;
longl=1;
strings="hello";
whichofthefollowingwillcompilewithouterror?
1)c=c+i;
2)s+=i;
3)i+=s;
4)c+=s;
question51)
giventhefollowingcodewhatwillbetheoutput?
classvalhold{
pub