文章转载自:http://blog.csdn.net/hdwt/article/details/1294558
这些题目对我的笔试帮助很大,有需要的朋友都可以来看看,在笔试中能遇到的题目基本上下面都会出现,虽然形式不同,当考察的基本的知识点还是相同的。
在分析中肯定有不足和谬误的地方还请大虾们能够给予及时的纠正,特此感谢。
publicclassReturnIt{ returnTypemethodA(bytex,doubley){//line2 return(short)x/y*2; } } whatisvalidreturnTypeformethodAinline2?答案:返回double类型,因为(short)x将byte类型强制转换为short类型,与double类型运算,将会提升为double类型.
classSuper{ publicfloatgetNum(){return3.0f;} } publicclassSubextendsSuper{ }whichmethod,placedatline6,willcauseacompilererror?
A.publicfloatgetNum(){return4.0f;}
B.publicvoidgetNum(){}
C.publicvoidgetNum(doubled){}
D.publicdoublegetNum(floatd){return4.0d;}
Answer:BA属于方法的重写(重写只存在于继承关系中),因为修饰符和参数列表都一样.B出现编译错误,如下:
Sub.java:6:Sub中的getNum()无法覆盖Super中的getNum();正在尝试使用不
兼容的返回类型
找到:void
需要:float
publicvoidgetNum(){}
^
1错误B既不是重写也不是重载,重写需要一样的返回值类型和参数列表,访问修饰符的限制一定要大于被重写方法的访问修饰符(public>protected>default>private);
重载:必须具有不同的参数列表;
可以有不同的返回类型,只要参数列表不同就可以了;
可以有不同的访问修饰符;把其看做是重载,那么在java中是不能以返回值来区分重载方法的,所以b不对.
publicclassIfTest{ publicstaticvoidmain(Stringargs[]){ intx=3; inty=1; if(x=y) System.out.println("Notequal"); else System.out.println("Equal"); } }whatistheresult?
Answer:compileerror错误在与if(x=y)中,应该是x==y;=是赋值符号,==是比较操作符 publicclassFoo{ publicstaticvoidmain(Stringargs[]){ try{return;} finally{System.out.println("Finally");} } }whatistheresult?
A.printoutnothing
B.printout"Finally"
C.compileerror
Answer:Bjava的finally块会在return之前执行,无论是否抛出异常且一定执行. publicclassTest{ publicstaticStringoutput=""; publicstaticvoidfoo(inti){ try{ if(i==1){ thrownewException(); } output+="1"; } catch(Exceptione){ output+="2"; return; } finally{ output+="3"; } output+="4"; } publicstaticvoidmain(Stringargs[]){ foo(0); foo(1); 24) } }whatisthevalueofoutputatline24?Answer:13423如果你想出的答案是134234,那么说明对return的理解有了混淆,return是强制函数返回,本题就是针对foo(),那么当执行到return的话,output+="4";就不再执行拉,这个函数就算结束拉.
publicclassIfElse{ publicstaticvoidmain(Stringargs[]){ if(odd(5)) System.out.println("odd"); else System.out.println("even"); } publicstaticintodd(intx){returnx%2;} }whatisoutput?
Answer:CompileError classExceptionTest{ publicstaticvoidmain(Stringargs[]){ try{ methodA(); } catch(IOExceptione){ System.out.println("caughtIOException"); } catch(Exceptione){ System.out.println("caughtException"); } } }IfmethodA()throwsaIOException,whatistheresult?(其实还应该加上:importjava.io.*;)
Answer:caughtIOException异常的匹配问题,如果2个catch语句换个位置,那就会报错,catch只能是越来越大,意思就是说:catch的从上到下的顺序应该是:孙子异常->孩子异常->父亲异常->老祖先异常.这么个顺序. inti=1,j=10; do{ if(i++>--j)continue; }while(i<5);(注意不要丢了这个分号呦)AfterExecution,whatarethevalueforiandj?
A.i=6j=5
B.i=5j=5
C.i=6j=4
D.i=5j=6
E.i=6j=6
Answer:D 1)publicclassX{ 2)publicObjectm(){ 3)Objecto=newFloat(3.14F); 4)Object[]oa=newObject[1]; 5)oa[0]=o; 6)o=null; 7)oa[0]=null; 8)System.out.println(oa[0]); 9)} 10)}whichlineistheearliestpointtheobjectareferedisdefinitelyelibile
tobegarbagecollectioned?
A.Afterline4B.Afterline5C.Afterline6
D.Afterline7E.Afterline9(thatis,asthemethodreturns)
Answer:D如果6)o=null变成o=9f,并且把7)去掉,那么8)将会输出什么呢?
interfaceFoo{ 2)intk=0; 3)} 4)publicclassTestimplementsFoo{ 5)publicstaticvoidmain(Stringargs[]){ 6)inti; 7)Testtest=newTest(); 8)i=test.k; 9)i=Test.k; 10)i=Foo.k; 11)} 12)}whatistheresult?Answer:compilesuccessedandi=0接口中的intk=0虽然没有访问修饰符,但在接口中默认是static和final的
whatisreservedwordsinjava?
A.run
B.default
C.implement
D.import
Answer:B,D publicclassTest{ publicstaticvoidmain(String[]args){ Stringfoo=args[1]; Sringbar=args[2]; Stringbaz=args[3]; } }javaTestRedGreenBlue
whatisthevalueofbaz?
A.bazhasvalueof""
B.bazhasvalueofnull
C.bazhasvalueofRed
D.bazhasvalueofBlue
E.bazhasvalueofGreen
F.thecodedoesnotcompile
G.theprogramthrowanexception
Answer:G分析:感觉原应该多一些语句吧,至少应该有红绿蓝的赋值语句之类的,才能叫javaTestRedGreenBlue才能有后面的选项,所以现在感觉很奇怪,不过就这个样子吧.这个问题在于:数组参数的理解,编译程序没有问题,但是运行这个程序就会出现问题,因为参数args没有给他分配空间那么他的长度应该是0,下面却用拉args[1]........等等的语句,那么定会出现越界错误.
错误如下:Exceptioninthread"main"java.lang.ArrayIndexOutOfBoundsException:1
atTest.main(Test.java:4) intindex=1; intfoo[]=newint[3]; intbar=foo[index]; intbaz=bar+index;whatistheresult?
A.bazhasavalueof0
B.bazhasvalueof1
C.bazhasvalueof2
D.anexceptionisthrown
E.thecodewillnotcompile
Answer:B分析:《thinkinginjava》中的原话:若类的某个成员是基本数据类型,即使没有进行初始化,java也会确保它获得一个默认值,如下表所示:
基本类型 默认值 boolean false char /u0000(null) byte (byte)0 short (short)0 int 0 long 0L float 0.0f double 0.0d千万要小心:当变量作为类的成员使用时,java才确保给定其默认值,。。。。。(后面还有很多话,也很重要,大家一定要看完成,要不然还是不清楚)
whichthreearevaliddeclaractionofafloat?
A.floatfoo=-1;
B.floatfoo=1.0;
C.floatfoo=42e1;
D.floatfoo=2.02f;
E.floatfoo=3.03d;
F.floatfoo=0x0123;
Answer:A,D,F分析:B错误,因为1.0在java中是double类型的,C,E错误同样道理,都是double类型的 publicclassFoo{ publicstaticvoidmain(Stringargs[]){ Strings; System.out.println("s="+s); } }whatistheresult?
Answer:compileerror分析:需要对s进行初始化,和13题是不是矛盾呢:不矛盾,因为它不是基本类型,也不是类的成员,所以不能套用上述的确保初始化的方法。 publicclassTest{ 2)publicstaticvoidmain(Stringargs[]){ 3)inti=0xFFFFFFF1; 4)intj=~i; 5) 6)} 7)}whichisdecimalvalueofjatline5?
A.0B.1C.14D.-15E.compileerroratline3F.compileerroratline4
Answer:C分析:int是32位的(范围应该在-231~231-1),按位取反后,后4位是1110,前面的全部是0,所以肯定是14 floatf=4.2F; Floatg=newFloat(4.2F); Doubled=newDouble(4.2);Whicharetrue?
A.f==gB.g==gC.d==fD.d.equals(f)Ed.equals(g)F.g.equals(4.2);
Answer:B,E(网上的答案是B,E;我测试的结果是:true,true,false,false,fasle,fasle,所以答案是:A,B,还请各位大虾明示)分析:以下是我从网络上找到的,但是感觉应用到这个题目上反而不对拉,郁闷中,希望能给大家有所提示,要是你们明白拉,记得给我留言啊!:~
1.基本类型、对象引用都在栈中;而对象本身在堆中;
2.“==“比的是两个变量在栈内存中的值,而即使变量引用的是两个对象,“==”比的依旧是变量所拥有的“栈内存地址值”;
3.equals()是每个对象与生俱来的方法,因为所有类的最终基类就是Object(除去Object本身);而equals()是Object的方法之一,也就是说equals()方法来自Object类。观察一下Object中equals()的sourcecode:
publicbooleanequals(Objectobj){return(this==obj);}
注意:“return(this==obj)”this与obj都是对象引用,而不是对象本身。所以equals()的缺省实现就是比较“对象引用”是否一致,所以要比较两个对象本身是否一致,须自己编写代码覆盖Object类里的equals()的方法。来看一下String类的equals方法代码:
publicbooleanequals(ObjectanObject){ if(this==anObject){ returntrue; } if(anObjectinstanceofString){ StringanotherString=(String)anObject; intn=count; if(n==anotherString.count){ charv1[]=value; charv2[]=anotherString.value; inti=offset; intj=anotherString.offset; while(n--!=0){ if(v1[i++]!=v2[j++]) returnfalse; } returntrue; } } returnfalse; } publicclassEquals{ publicstaticvoidadd3(Integeri){ intval=i.intValue(); val+=3; i=newInteger(val); } publicstaticvoidmain(Stringargs[]){ Integeri=newInteger(0); add3(i); System.out.println(i.intValue()); } }whatistheresult?
A.compilefailB.printout"0"C.printout"3"
D.compilesuccededbutexceptionatline3
Answer:B分析:java只有一种参数传递方式,那就是值传递.(大家可以看我转载的另一个同名文章,会让大家豁然开朗) publicclassTest{ publicstaticvoidmain(String[]args){ System.out.println(6^3); } }whatisoutput?Answer:5分析:^isyihuo(计算机器上是Xor);异或的逻辑定义:真^真=假真^假=真假^真=真假^假=假
publicclassTest{ publicstaticvoidstringReplace(Stringtext){ text=text.replace(j,l); } publicstaticvoidbufferReplace(StringBuffertext){ text=text.append("c"); } publicstaticvoidmain(Stringargs[]){ StringtextString=newString("java"); StringBuffertextBuffer=newStringBuffer("java"); stringReplace(textString); bufferReplace(textBuffer); System.out.println(textString+textBuffer); } }whatistheoutput?
Answer:javajavac分析:根据我转载的一篇文章<Java只有一种参数传递方式,那就是传值>可以得出答案,不过还有几个类似的题目,用该文章解释不通,因为本人对java传递参数也一直没有弄明白,所以,还请大虾多多指教.
publicclassConstOver{ publicConstOver(intx,inty,intz){} }whichtwooverloadtheConstOverconstructor?
A.ConstOver(){}
B.protectedintConstOver(){}//notoverload,butnoaerror
C.privateConstOver(intz,inty,bytex){}
D.publicvoidConstOver(bytex,bytey,bytez){}
E.publicObjectConstOver(intx,inty,intz){}
Answer:A,C分析:测试不通过的首先是B,E,因为要求有返回值,这2个选项没有,要想通过编译那么需要加上返回值,请注意如果加上返回值,单纯看选项是没有问题拉,可以针对题目来说,那就是错之又错拉,对于构造器来说是一种特殊类型的方法,因为它没有返回值.对于D选项在<java编程思想第3版>91页有详细的介绍,空返回值,经管方法本身不会自动返回什么,但可以选择返回别的东西的,而构造器是不会返回任何东西的,否则就不叫构造器拉.
publicclassMethodOver{ publicvoidsetVar(inta,intb,floatc){} }whichoverloadthesetVar?
A.privatevoidsetVar(inta,floatc,intb){}
B.protectedvoidsetVar(inta,intb,floatc){}
C.publicintsetVar(inta,floatc,intb){returna;}
D.publicintsetVar(inta,floatc){returna;}
Answer:A,C,D分析:方法的重载,根据概念选择,B是错误的,因为他们有相同的参数列表,所以不属于重载范围. classEnclosingOne{ publicclassInsideOne{} } publicclassInnerTest{ publicstaticvoidmain(Stringargs[]){ EnclosingOneeo=newEnclosingOne(); //insertcodehere } }A.InsideOneei=eo.newInsideOne();
B.eo.InsideOneei=eo.newInsideOne();
C.InsideOneei=EnclosingOne.newInsideOne();
D.InsideOneei=eo.newInsideOne();
E.EnclosingOne.InsideOneei=eo.newInsideOne();
Answer:EWhatis"isa"relation?
A.publicinterfaceColor{}
publicclassShape{privateColorcolor;}
B.interfaceComponent{}
classContainerimplementsComponent{privateComponent[]children;}
C.publicclassSpecies{}
publicclassAnimal{privateSpeciesspecies;}
D.interfaceA{}
interfaceB{}
interfaceCimplementsA,B{}//syntexerror
Answer:B我没有明白这个题目的意思,有人能告诉我嘛? 1)packagefoo; 2) 3)publicclassOuter{ 4)publicstaticclassInner{ 5)} 6)}whichistruetoinstantiatedInnerclassinsideOuter?
A.newOuter.Inner()
B.newInner()
Answer:B
ifoutofouterclassAiscorrect分析:在Outer内部,B方式实例化内部类的方法是正确的,如果在Outer外部进行inner的实例化,那么A方法是正确的. classBaseClass{ privatefloatx=1.0f; privatefloatgetVar(){returnx;} } classSubClassextendsBaseClass{ privatefloatx=2.0f; //insertcode }whataretruetooverridegetVar()?
A.floatgetVar(){
B.publicfloatgetVar(){
C.publicdoublegetVar(){
D.protectedfloatgetVar(){
E.publicfloatgetVar(floatf){
Answer:A,B,D分析:返回类型和参数列表必须完全一致,且访问修饰符必须大于被重写方法的访问修饰符. publicclassSychTest{ privateintx; privateinty; publicvoidsetX(inti){x=i;} publicvoidsetY(inti){y=i;} publicSynchronizedvoidsetXY(inti){ setX(i); setY(i); } publicSynchronizedbooleancheck(){ returnx!=y; } }Underwhichconditionswillcheck()returntruewhencalledfromadifferentclass?
A.check()canneverreturntrue.
B.check()canreturntruewhensetXYiscallledbymultiplethreads.
C.check()canreturntruewhenmultiplethreadscallsetXandsetYseparately.
D.check()canonlyreturntrueifSychTestischangedallowxandytobesetseparately.
Answer:C分析:答案是C,但是我想不出来一个测试程序来验证C答案.希望高手们给我一个测试的例子吧,万分感谢..........
publicclassXimplementsRunnable{ 2)privateintx; 3)privateinty; 4)publicstaticvoidmain(String[]args){ 5)Xthat=newX(); 6)(newThread(that)).start(); 7)(newThread(that)).start(); } 9)publicsynchronizedvoidrun(){ 10)for(;;){ 11)x++; 12)y++; 13)System.out.println("x="+x+",y="+y); 14)} 15)} 16)}whatistheresult?
A.compileerroratline6
B.theprogramprintspairsofvaluesforxandythatarealwaysthesameonthesametime
Answer:B分析:我感觉会出现不相等的情况,但是我说不出为什么会相等。线程方面,还有好多路要走啊,咳 classAimplementsRunnable{ inti; publicvoidrun(){ try{ Thread.sleep(5000); i=10; }catch(InterruptedExceptione){} } publicstaticvoidmain(String[]args){ try{ Aa=newA(); Threadt=newThread(a); t.start(); 17) intj=a.i; 19) }catch(Exceptione){} } }whatbeaddedatlineline17,ensurej=10atline19?
A.a.wait();B.t.wait();C.t.join();D.t.yield();E.t.notify();F.a.notify();G.t.interrupt();
Answer:CGivenanActionEvent,howtoindentifytheaffectedcomponent?
A.getTarget();
B.getClass();
C.getSource();//publicobject
D.getActionCommand();
Answer:C importjava.awt.*; publicclassXextendsFrame{ publicstaticvoidmain(String[]args){ Xx=newX(); x.pack(); x.setVisible(true); } publicX(){ setLayout(newGridLayout(2,2)); Panelp1=newPanel(); add(p1); Buttonb1=newButton("One"); p1.add(b1); Panelp2=newPanel(); add(p2); Buttonb2=newButton("Two"); p2.add(b2); Buttonb3=newButton("Three"); p2.add(b3); Buttonb4=newButton("Four"); add(b4); } }whentheframeisresized,
A.allchangeheightB.allchangewidthC.Button"One"changeheight
D.Button"Two"changeheightE.Button"Three"changewidth
F.Button"Four"changeheightandwidth
Answer:F publicclassX{ publicstaticvoidmain(String[]args){ Stringfoo="ABCDE"; foo.substring(3); foo.concat("XYZ"); } }whatisthevalueoffooatline6?
Answer:ABCDEHowtocalculatecosine42degree?
A.doubled=Math.cos(42);
B.doubled=Math.cosine(42);
C.doubled=Math.cos(Math.toRadians(42));
D.doubled=Math.cos(Math.toDegrees(42));
E.doubled=Math.toRadious(42);
Answer:C publicclassTest{ publicstaticvoidmain(String[]args){ StringBuffera=newStringBuffer("A"); StringBufferb=newStringBuffer("B"); operate(a,b); System.out.pintln(a+","+b); } publicstaticvoidoperate(StringBufferx,StringBuffery){ x.append(y); y=x; } }whatistheoutput?
Answer:AB,B分析:这道题的答案是AB,B,网上有很多答案给错啦,大家注意啊。 publicclassTest{ 2)publicstaticvoidmain(String[]args){ 3)classFoo{ 4)publicinti=3; 5)} 6)Objecto=(Object)newFoo(); 7)Foofoo=(Foo)o; System.out.println(foo.i); 9)} 10)}whatisresult?
A.compileerroratline6
B.compileerroratline7
C.printout3
Answer:C publicclassFooBar{ publicstaticvoidmain(String[]args){ inti=0,j=5; 4)tp:for(;;i++){ for(;;--j) if(i>j)breaktp; } System.out.println("i="+i+",j="+j); } }whatistheresult?
A.i=1,j=-1B.i=0,j=-1C.i=1,j=4D.i=0,j=4
E.compileerroratline4
Answer:B publicclassFoo{ publicstaticvoidmain(String[]args){ try{System.exit(0);} finally{System.out.println("Finally");} } }whatistheresult?
A.printoutnothing
B.printout"Finally"
Answer:A
system.exit(0)hasexitwhichfourtypesofobjectscanbethrownuse"throws"?
A.ErrorB.Event
C.Object
D.Excption
E.Throwable
F.RuntimeException Answer:A,D,E,F分析:throw,例如:thrownewIllegalAccessException("demo");是一个动作。
而throws则是异常块儿的声明。所以感觉题目应该是“throw” 1)publicclassTest{ 2)publicstaticvoidmain(String[]args){ 3)unsignedbyteb=0; 4)b--; 5) 6)} 7)}whatisthevalueofbatline5?
A.-1B.255C.127D.compilefailE.compilesucceededbutrunerror
Answer:D publicclassExceptionTest{ classTestExceptionextendsException{} publicvoidrunTest()throwsTestException{} publicvoidtest()/*pointx*/{ runTest(); } }Atpointx,whichcodecanbeaddontomakethecodecompile?
A.throwsExceptionB.catch(Exceptione)
Answer:A Stringfoo="blue"; boolean[]bar=newboolean[1]; if(bar[0]){ foo="green"; }whatisthevalueoffoo?
A.""B.nullC.blueD.green
Answer:C publicclassX{ publicstaticvoidmain(Stringargs[]){ Objecto1=newObject(); Objecto2=o1; if(o1.equals(o2)){ System.out.prinln("Equal"); } } }whatisresult?
Answer:Equal本文内容总结:
原文链接:https://www.cnblogs.com/java-class/archive/2013/05/07/3065446.html