日韩欧美另类久久久精品_亚洲大色堂人在线无码_国产三级aⅴ在线播放_在线无码aⅴ精品动漫_国产精品精品国产专区

Java經(jīng)典筆試題

  這些題目對(duì)我的筆試幫助很大,有需要的朋友都可以來(lái)看看,在筆試中能遇到的題目基本上下面都會(huì)出現(xiàn),雖然形式不同,當(dāng)考察的基本的知識(shí)點(diǎn)還是相同的。

  Simulated Test of SCJP for JAVA2 PlatFORM (only for training))

  網(wǎng)上可以找到很多,因?yàn)槲沂寝D(zhuǎn)載ICXO網(wǎng)站的,但是上面的有很多可能有由于頁(yè)面原因,每個(gè)題目我都做了測(cè)試,出現(xiàn)錯(cuò)誤的我就稍微做了下修正,希望和大家一起研究和探討,在分析中肯定有不足和謬誤的地方還請(qǐng)大蝦們能夠給予及時(shí)的糾正,特此感謝。

  1.

  public class ReturnIt{

  returnType methodA(byte x, double y){ //line 2

  return (short)x/y*2;

  }

  }

  what is valid returnType for methodA in line 2?

  答案:返回double類型,因?yàn)?short)x將byte類型強(qiáng)制轉(zhuǎn)換為short類型,與double類型運(yùn)算,將會(huì)提升為double類型.

  2.

  1) class Super{

  2) public float getNum(){return 3.0f;}

  3) }

  4)

  5) public class Sub extends Super{

  6)

  7) }

  which method, placed at line 6, will cause a compiler error?

  A. public float getNum(){return 4.0f;}

  B. public void getNum(){}

  C. public void getNum(double d){}

  D. public double getNum(float d){return 4.0d;}

  Answer:B

  A屬于方法的重寫(重寫只存在于繼承關(guān)系中),因?yàn)樾揎椃蛥?shù)列表都一樣.B出現(xiàn)編譯錯(cuò)誤,如下:

  Sub.java:6: Sub 中的 getNum() 無(wú)法覆蓋 Super 中的 getNum();正在嘗試使用不

  兼容的返回類型

  找到: void

  需要: float

  public void getNum(){}

  ^

  1 錯(cuò)誤

  B既不是重寫也不是重載,重寫需要一樣的返回值類型和參數(shù)列表,訪問(wèn)修飾符的限制一定要大于被重寫方法的訪問(wèn)修飾符(public>protected>default>private);

  重載:必須具有不同的參數(shù)列表;

  可以有不同的返回類型,只要參數(shù)列表不同就可以了;

  可以有不同的訪問(wèn)修飾符;

  把其看做是重載,那么在java中是不能以返回值來(lái)區(qū)分重載方法的,所以b不對(duì).

  3.

  public class IfTest{

  public static void main(String args[]){

  int x=3;

  int y=1;

  if(x=y)

  System.out.println("Not equal");

  else

  System.out.println("Equal");

  }

  }

  what is the result?

  Answer:compile error 錯(cuò)誤在與if(x=y) 中,應(yīng)該是x==y; =是賦值符號(hào),==是比較操作符

  4. public class Foo{

  public static void main(String args[]){

  try{return;}

  finally{ System.out.println("Finally");}

  }

  }

  what is the result?

  A. print out nothing

  B. print out "Finally"

  C. compile error

  Answer:B java的finally塊會(huì)在return之前執(zhí)行,無(wú)論是否拋出異常且一定執(zhí)行.

  5.public class Test{

  public static String output="";

  public static void foo(int i){

  try {

  if(i==1){

  throw new Exception();

  }

  output +="1";

  }

  catch(Exception e){

  output+="2";

  return;

  }

  finally{

  output+="3";

  }

  output+="4";

  }

  public static void main(String args[]){

  foo(0);

  foo(1);

  24)

  }

  }

  what is the value of output at line 24? Answer:13423 如果你想出的答案是134234,那么說(shuō)明對(duì)return的理解有了混淆,return是強(qiáng)制函數(shù)返回,本題就是針對(duì)foo(),那么當(dāng)執(zhí)行到return的話,output+="4"; 就不再執(zhí)行拉,這個(gè)函數(shù)就算結(jié)束拉.

  6. public class IfElse{

  public static void main(String args[]){

  if(odd(5))

  System.out.println("odd");

  else

  System.out.println("even");

  }

  public static int odd(int x){return x%2;}

  }

  what is output?

  Answer:Compile Error

  7. class ExceptionTest{

  public static void main(String args[]){

  try{

  methodA();

  }

  catch(IOException e){

  System.out.println("caught IOException");

  }

  catch(Exception e){

  System.out.println("caught Exception");

  }

  }

  }

  If methodA() throws a IOException, what is the result? (其實(shí)還應(yīng)該加上:import java.io.*;)

  Answer:caught IOException 異常的匹配問(wèn)題,如果2個(gè)catch語(yǔ)句換個(gè)位置,那就會(huì)報(bào)錯(cuò),catch只能是越來(lái)越大,意思就是說(shuō):catch的從上到下的順序應(yīng)該是:孫子異常->孩子異常->父親異常->老祖先異常.這么個(gè)順序.

  8. int i=1,j=10;

  do{

  if(i++>--j) continue;

  }while(i<5); (注意不要丟了這個(gè)分號(hào)呦)

  After Execution, what are the value for i and j?

  A. i=6 j=5

  B. i=5 j=5

  C. i=6 j=4

  D. i=5 j=6

  E. i=6 j=6

  Answer:D

  9. 1)public class X{

  2) public Object m(){

  3) Object o=new Float(3.14F);

  4) Object[] oa=new Object[1];

  5) oa[0]=o;

  6) o=null;

  7) oa[0]=null;

  8) System.out.println(oa[0]);

  9) }

  10) }

  which line is the earliest point the object a refered is definitely elibile

  to be garbage collectioned?

  A.After line 4 B. After line 5 C.After line 6

  D.After line 7 E.After line 9(that is,as the method returns)

  Answer:D

  如果 6) o=null 變成 o=9f ,并且把7)去掉,那么8)將會(huì)輸出什么呢?

  10. 1) interface Foo{

  2) int k=0;

  3) }

  4) public class Test implements Foo{

  5) public static void main(String args[]){

  6) int i;

  7) Test test = new Test();

  8) i = test.k;

  9) i = Test.k;

  10) i = Foo.k;

  11) }

  12) }

  what is the result? Answer:compile successed and i=0 接口中的int k=0雖然沒(méi)有訪問(wèn)修飾符,但在接口中默認(rèn)是static和final的

  11. what is reserved words in java?

  A. run

  B. default

  C. implement

  D. import

  Answer:B,D

  12. public class Test{

  public static void main(String[] args){

  String foo=args[1];

  Sring bar=args[2];

  String baz=args[3];

  }

  }

  java Test Red Green Blue

  what is the value of baz?

  A. baz has value of ""

  B. baz has value of null

  C. baz has value of Red

  D. baz has value of Blue

  E. baz has value of Green

  F. the code does not compile

  G. the program throw an exception

  Answer:G

  分析:感覺(jué)原應(yīng)該多一些語(yǔ)句吧,至少應(yīng)該有紅綠藍(lán)的賦值語(yǔ)句之類的,才能叫java Test Red Green Blue 才能有后面的選項(xiàng),所以現(xiàn)在感覺(jué)很奇怪,不過(guò)就這個(gè)樣子吧.這個(gè)問(wèn)題在于:數(shù)組參數(shù)的理解,編譯程序沒(méi)有問(wèn)題,但是運(yùn)行這個(gè)程序就會(huì)出現(xiàn)問(wèn)題,因?yàn)閰?shù)args沒(méi)有給他分配空間那么他的長(zhǎng)度應(yīng)該是0,下面卻用拉args[1]........等等的語(yǔ)句,那么定會(huì)出現(xiàn)越界錯(cuò)誤.

  錯(cuò)誤如下:Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1

  at Test.main(Test.java:4)

  13. int index=1;

  int foo[]=new int[3];

  int bar=foo[index];

  int baz=bar+index;

  what is the result?

  A. baz has a value of 0

  B. baz has value of 1

  C. baz has value of 2

  D. an exception is thrown

  E. the code will not compile

  Answer:B

  分析:《thinking in java》中的原話:若類的某個(gè)成員是基本數(shù)據(jù)類型,即使沒(méi)有進(jìn)行初始化,java也會(huì)確保它獲得一個(gè)默認(rèn)值,如下表所示:

基本類型 默認(rèn)值
boolean false
char '/u0000'(null)
byte (byte)0
short (short)0
int 0
long 0L
float 0.0f
double 0.0d

  千萬(wàn)要小心:當(dāng)變量作為類的成員使用時(shí),java才確保給定其默認(rèn)值,。。。。。(后面還有很多話,也很重要,大家一定要看完成,要不然還是不清楚)

  14. which three are valid declaraction of a float?

  A. float foo=-1;

  B. float foo=1.0;

  C. float foo=42e1;

  D. float foo=2.02f;

  E. float foo=3.03d;

  F. float foo=0x0123;

  Answer:A,D,F 分析:B錯(cuò)誤,因?yàn)?.0在java中是double類型的,C,E錯(cuò)誤同樣道理,都是double類型的

  15. public class Foo{

  public static void main(String args[]){

  String s;

  System.out.println("s="+s);

  }

  }

  what is the result?

  Answer:compile error 分析:需要對(duì)s進(jìn)行初始化,和13題是不是矛盾呢:不矛盾,因?yàn)樗皇腔绢愋停膊皇穷惖某蓡T,所以不能套用上述的確保初始化的方法。

本文已影響6827
上一篇:中軟融鑫2014校招java開發(fā)工程師筆試題 下一篇:數(shù)據(jù)庫(kù)經(jīng)典筆試題

相關(guān)文章推薦

|||||