java.docx
- 文档编号:2723196
- 上传时间:2023-05-04
- 格式:DOCX
- 页数:15
- 大小:18.45KB
java.docx
《java.docx》由会员分享,可在线阅读,更多相关《java.docx(15页珍藏版)》请在冰点文库上搜索。
java
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){//改为:
main(String[]argv)
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{}//类的修饰符只能是public,final,abstract
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();//应该是重写run()方法,继承start()方法,
}
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){
case10:
system.out.println("ten");
case20:
system.out.println("twenty");
break;
default:
system.out.println("thisisthedefaultoutput");
break;
}
}
}
1)noneoftheseoptions//应该是tentwenty
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;//静态方法只能使用静态变量,myarg不是静态变量
}
publicvoidamethod(){
System.out.println(argv[1]);//argv是局部变量,在这个函数里不能使用
}
}
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==
question31)
whatwillhappenwhenyouattempttocompileandrunthefollowingcode
importjava.io.*;
classbase{
publicvoidamethod()throwsfilenotfoundexception{}
}
publicclassexcepdemoextendsbase{
publicstaticvoidmain(stringargv[]){
excepdemoe=newexcepdemo();
}
publicvoidamethod(){}//重写方法时,子类的方法抛出的异常只能比父类的少,不能比父类多
protectedexcepdemo(){
try{
datainputstreamdin=newdatainputstream(system.in);
system.out.println("pausing");
din.readbyte();
system.out.println("continuing");
this.amethod();
}catch(ioexceptionioe){}
}
}
1)compiletimeerrorcausedbyprotectedconstructor
2)compiletimeerrorcausedbyamethodnotdeclaringexception
3)runtimeerrorcausedbyamethodnotdeclaringexception
4)compileandrunwithoutputof"pausing"and"continuing"afterakeyishit
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
question33)(不做)
whatwillhappenwhenyouattempttocompileandrunthiscode
//demonstrationofeventhandling
importjava.awt.*;
importjava.awt.event.*;
publicclassmywcextendsframeimplementswindowlistener{
publicstaticvoidmain(stringargv[]){
mywcmwc=newmywc();
}
publicvoidwindowclosing(windoweventwe){
system.exit(0);
}//endofwindowclosing
publicvoidmywc(){
setsize(300,300);
setvisible(true);
}
}//endofclass
1)erroratcompiletime
2)visibleframecreatedthatthatcanbeclosed
3)compilationbutnooutputatruntime
4)erroratcompiletimebecauseofcommentbeforeimportstatements
question34)
whichoptionmostfullydescribeswillhappenwhenyouattempttocompileandrunthefollowingcode
publicclassmyar{
publicstaticvoidmain(stringargv[]){
myarm=newmyar();
m.amethod();
}
publicvoidamethod(){
staticinti;//局部变量不能定义为static,编译出错
system.out.println(i);
}
}
1)compilationandoutputofthevalue0
2)compiletimeerrorbecauseihasnotbeeninitialized
3)compilationandoutputofnull
4)compiletime
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java