Java 經典筆試題
文章轉載自:http://blog.csdn.net/hdwt/article/details/1294558
這些題目對我的筆試幫助很大,有需要的朋友都可以來看看,在筆試中能遇到的題目基本上下面都會出現,雖然形式不同,當考察的基本的知識點還是相同的。
Simulated Test of SCJP for JAVA2 PlatFORM (only for training))
網上可以找到很多,因為我是轉載ICXO網站的,但是上面的有很多可能有由於頁面原因,每個題目我都做了測試,出現錯誤的我就稍微做了下修正,希望和大家一起研究和探討,在分析中肯定有不足和謬誤的地方還請大蝦們能夠給予及時的糾正,特此感謝
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型別,因為(short)x將byte型別強制轉換為short型別,與double型別運算,將會提升為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屬於方法的重寫(重寫只存在於繼承關係中),因為修飾符和引數列表都一樣.B出現編譯錯誤,如下:
Sub.java:6: Sub 中的 getNum() 無法覆蓋 Super 中的 getNum();正在嘗試使用不
相容的返回型別
找到: void
需要: float
public void getNum(){}
^
1 錯誤
B既不是重寫也不是過載,重寫需要一樣的返回值型別和引數列表,訪問修飾符的限制一定要大於被重寫方法的訪問修飾符(public>protected>default>private);
過載:必須具有不同的引數列表;
可以有不同的返回型別,只要引數列表不同就可以了;
可以有不同的訪問修飾符;
把其看做是過載,那麼在java中是不能以返回值來區分過載方法的,所以b不對.
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 錯誤在與if(x=y) 中,應該是x==y; =是賦值符號,==是比較操作符
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塊會在return之前執行,無論是否丟擲異常且一定執行.
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,那麼說明對return的理解有了混淆,return是強制函式返回,本題就是針對foo(),那麼當執行到return的話,output+="4"; 就不再執行拉,這個函式就算結束拉.
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? (其實還應該加上:import java.io.*;)
Answer:caught IOException 異常的匹配問題,如果2個catch語句換個位置,那就會報錯,catch只能是越來越大,意思就是說:catch的從上到下的順序應該是:孫子異常->孩子異常->父親異常->老祖先異常.這麼個順序.
8. int i=1,j=10;
do{
if(i++>--j) continue;
}while(i<5); (注意不要丟了這個分號呦)
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)將會輸出什麼呢?
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雖然沒有訪問修飾符,但在介面中預設是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
分析:感覺原應該多一些語句吧,至少應該有紅綠藍的賦值語句之類的,才能叫java Test Red Green Blue 才能有後面的選項,所以現在感覺很奇怪,不過就這個樣子吧.這個問題在於:陣列引數的理解,編譯程式沒有問題,但是執行這個程式就會出現問題,因為引數args沒有給他分配空間那麼他的長度應該是0,下面卻用拉args[1]........等等的語句,那麼定會出現越界錯誤.
錯誤如下: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》中的原話:若類的某個成員是基本資料型別,即使沒有進行初始化,java也會確保它獲得一個預設值,如下表所示:
基本型別 | 預設值 |
boolean | false |
char | '/u0000'(null) |
byte | (byte)0 |
short | (short)0 |
int | 0 |
long | 0L |
float | 0.0f |
double | 0.0d |
千萬要小心:當變數作為類的成員使用時,java才確保給定其預設值,。。。。。(後面還有很多話,也很重要,大家一定要看完成,要不然還是不清楚)
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錯誤,因為1.0在java中是double型別的,C,E錯誤同樣道理,都是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 分析:需要對s進行初始化,和13題是不是矛盾呢:不矛盾,因為它不是基本型別,也不是類的成員,所以不能套用上述的確保初始化的方法。
16. 1) public class Test{
2) public static void main(String args[]){
3) int i =0xFFFFFFF1;
4) int j=~i;
5)
6) }
7) }
which is decimal value of j at line 5?
A. 0 B.1 C.14 D.-15 E. compile error at line 3 F. compile error at line 4
Answer:C 分析:int是32位的(範圍應該在-231~231-1),按位取反後,後4位是1110,前面的全部是0,所以肯定是14
17. float f=4.2F;
Float g=new Float(4.2F);
Double d=new Double(4.2);
Which are true?
A. f==g B. g==g C. d==f D. d.equals(f) E d.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()的source code:
public boolean equals(Object obj) { return (this == obj); }
注意:“return (this == obj)” this與obj都是物件引用,而不是物件本身。所以equals()的預設實現就是比較“物件引用”是否一致,所以要比較兩個物件本身是否一致,須自己編寫程式碼覆蓋Object類裡的equals()的方法。來看一下String類的equals方法程式碼:
public boolean equals(Object anObject){
if(this == anObject){
return true;
}
if(anObject instanceof String){
String anotherString = (String)anObject;
int n = count;
if(n == anotherString.count){
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while(n-- != 0){
if(v1[i++] != v2[j++])
return false;
}
return true;
}
}
return false;
}
18. public class Equals{
public static void add3(Integer i){
int val = i.intValue();
val += 3;
i = new Integer(val);
}
public static void main(String args[]){
Integer i=new Integer(0);
add3(i);
System.out.println(i.intValue());
}
}
what is the result?
A. compile fail B.print out "0" C.print out "3"
D.compile succeded but exception at line 3
Answer:B 分析:java只有一種引數傳遞方式,那就是值傳遞.(大家可以看我轉載的另一個同名文章,會讓大家豁然開朗)
19. public class Test{
public static void main(String[] args){
System.out.println(6^3);
}
}
what is output? Answer:5 分析: ^ is yi huo(計算機器上是Xor) ;異或的邏輯定義:真^真=假
真^假=真 假^真=真 假^假=假
20. public class Test{
public static void stringReplace(String text){
text=text.replace('j','l');
}
public static void bufferReplace(StringBuffer text){
text=text.append("c");
}
public static void main(String args[]){
String textString=new String("java");
StringBuffer textBuffer=new StringBuffer("java");
stringReplace(textString);
bufferReplace(textBuffer);
System.out.println(textString+textBuffer);
}
}
what is the output?
Answer:javajavac
分析:根據我轉載的一篇文章<Java只有一種引數傳遞方式,那就是傳值>可以得出答案,不過還有幾個類似的題目,用該文章解釋不通,因為本人對java傳遞引數也一直沒有弄明白,所以,還請大蝦多多指教.
21. public class ConstOver{
public ConstOver(int x, int y, int z){}
}
which two overload the ConstOver constructor?
A.ConstOver(){}
B.protected int ConstOver(){} //not overload ,but no a error
C.private ConstOver(int z, int y, byte x){}
D.public void ConstOver(byte x, byte y, byte z){}
E.public Object ConstOver(int x, int y, int z){}
Answer:A,C
分析:測試不通過的首先是B,E,因為要求有返回值,這2個選項沒有,要想通過編譯那麼需要加上返回值,請注意如果加上返回值,單純看選項是沒有問題拉,可以針對題目來說,那就是錯之又錯拉,對於構造器來說是一種特殊型別的方法,因為它沒有返回值.對於D選項在<java程式設計思想 第3版>91頁有詳細的介紹,空返回值,經管方法本身不會自動返回什麼,但可以選擇返回別的東西的,而構造器是不會返回任何東西的,否則就不叫構造器拉.
22. public class MethodOver{
public void setVar(int a, int b, float c){}
}
which overload the setVar?
A.private void setVar(int a, float c, int b){}
B.protected void setVar(int a, int b, float c){}
C.public int setVar(int a, float c, int b){return a;}
D.public int setVar(int a, float c){return a;}
Answer:A,C,D 分析:方法的過載,根據概念選擇,B是錯誤的,因為他們有相同的引數列表,所以不屬於過載範圍.
23. class EnclosingOne{
public class InsideOne{}
}
public class InnerTest{
public static void main(String args[]){
EnclosingOne eo=new EnclosingOne();
//insert code here
}
}
A.InsideOne ei=eo.new InsideOne();
B.eo.InsideOne ei=eo.new InsideOne();
C.InsideOne ei=EnclosingOne.new InsideOne();
D.InsideOne ei=eo.new InsideOne();
E.EnclosingOne.InsideOne ei=eo.new InsideOne();
Answer:E
24. What is "is a" relation?
A.public interface Color{}
public class Shape{private Color color;}
B.interface Component{}
class Container implements Component{ private Component[] children; }
C.public class Species{}
public class Animal{private Species species;}
D.interface A{}
interface B{}
interface C implements A,B{} //syntex error
Answer:B 我沒有明白這個題目的意思,有人能告訴我嘛?
25. 1)package foo;
2)
3)public class Outer{
4) public static class Inner{
5) }
6)}
which is true to instantiated Inner class inside Outer?
A. new Outer.Inner()
B. new Inner()
Answer:B
if out of outerclass A is correct 分析:在Outer內部,B方式例項化內部類的方法是正確的,如果在Outer外部進行inner的例項化,那麼A方法是正確的.
26. class BaseClass{
private float x=1.0f;
private float getVar(){return x;}
}
class SubClass extends BaseClass{
private float x=2.0f;
//insert code
}
what are true to override getVar()?
A.float getVar(){
B.public float getVar(){
C.public double getVar(){
D.protected float getVar(){
E.public float getVar(float f){
Answer:A,B,D 分析:返回型別和引數列表必須完全一致,且訪問修飾符必須大於被重寫方法的訪問修飾符.
27. public class SychTest{
private int x;
private int y;
public void setX(int i){ x=i;}
public void setY(int i){y=i;}
public Synchronized void setXY(int i){
setX(i);
setY(i);
}
public Synchronized boolean check(){
return x!=y;
}
}
Under which conditions will check() return true when called from a different class?
A.check() can never return true.
B.check() can return true when setXY is callled by multiple threads.
C.check() can return true when multiple threads call setX and setY separately.
D.check() can only return true if SychTest is changed allow x and y to be set separately.
Answer:C
分析:答案是C,但是我想不出來一個測試程式來驗證C答案.希望高手們給我一個測試的例子吧,萬分感謝..........
28. 1) public class X implements Runnable{
2) private int x;
3) private int y;
4) public static void main(String[] args){
5) X that =new X();
6) (new Thread(that)).start();
7) (new Thread(that)).start();
}
9) public synchronized void run(){
10) for(;;){
11) x++;
12) y++;
13) System.out.println("x="+x+",y="+y);
14) }
15) }
16) }
what is the result?
A.compile error at line 6
B.the program prints pairs of values for x and y that are always the same on the same time
Answer:B 分析:我感覺會出現不相等的情況,但是我說不出為什麼會相等。執行緒方面,還有好多路要走啊,咳
29. class A implements Runnable{
int i;
public void run(){
try{
Thread.sleep(5000);
i=10;
}catch(InterruptedException e){}
}
public static void main(String[] args){
try{
A a=new A();
Thread t=new Thread(a);
t.start();
17)
int j=a.i;
19)
}catch(Exception e){}
}
}
what be added at line line 17, ensure j=10 at line 19?
A. a.wait(); B. t.wait(); C. t.join(); D.t.yield(); E.t.notify(); F. a.notify(); G.t.interrupt();
Answer:C
30. Given an ActionEvent, how to indentify the affected component?
A.getTarget();
B.getClass();
C.getSource(); //public object
D.getActionCommand();
Answer:C
31. import java.awt.*;
public class X extends Frame{
public static void main(String[] args){
X x=new X();
x.pack();
x.setVisible(true);
}
public X(){
setLayout(new GridLayout(2,2));
Panel p1=new Panel();
add(p1);
Button b1=new Button("One");
p1.add(b1);
Panel p2=new Panel();
add(p2);
Button b2=new Button("Two");
p2.add(b2);
Button b3=new Button("Three");
p2.add(b3);
Button b4=new Button("Four");
add(b4);
}
}
when the frame is resized,
A.all change height B.all change width C.Button "One" change height
D.Button "Two" change height E.Button "Three" change width
F.Button "Four" change height and width
Answer:F
32. 1)public class X{
2) public static void main(String[] args){
3) String foo="ABCDE";
4) foo.substring(3);
5) foo.concat("XYZ");
6) }
7) }
what is the value of foo at line 6?
Answer:ABCDE
33. How to calculate cosine 42 degree?
A.double d=Math.cos(42);
B.double d=Math.cosine(42);
C.double d=Math.cos(Math.toRadians(42));
D.double d=Math.cos(Math.toDegrees(42));
E.double d=Math.toRadious(42);
Answer:C
34. public class Test{
public static void main(String[] args){
StringBuffer a=new StringBuffer("A");
StringBuffer b=new StringBuffer("B");
operate(a,b);
System.out.pintln(a+","+b);
}
public static void operate(StringBuffer x, StringBuffer y){
x.append(y);
y=x;
}
}
what is the output?
Answer:AB,B 分析:這道題的答案是AB,B,網上有很多答案給錯啦,大家注意啊。
35. 1) public class Test{
2) public static void main(String[] args){
3) class Foo{
4) public int i=3;
5) }
6) Object o=(Object)new Foo();
7) Foo foo=(Foo)o;
System.out.println(foo.i);
9) }
10) }
what is result?
A.compile error at line 6
B.compile error at line 7
C.print out 3
Answer:C
36. public class FooBar{
public static void main(String[] args){
int i=0,j=5;
4) tp: for(;;i++){
for(;;--j)
if(i>j)break tp;
}
System.out.println("i="+i+",j="+j);
}
}
what is the result?
A.i=1,j=-1 B. i=0,j=-1 C.i=1,j=4 D.i=0,j=4
E.compile error at line 4
Answer:B
37. public class Foo{
public static void main(String[] args){
try{System.exit(0);}
finally{System.out.println("Finally");}
}
}
what is the result?
A.print out nothing
B.print out "Finally"
Answer:A
system.exit(0) has exit
38. which four types of objects can be thrown use "throws"?
A.Error
B.Event
C.Object
D.Excption
E.Throwable
F.RuntimeException
Answer:A,D,E,F
分析:throw,例如:throw new IllegalAccessException("demo");是一個動作。
而throws則是異常塊兒的宣告。所以感覺題目應該是“throw”
39. 1)public class Test{
2) public static void main(String[] args){
3) unsigned byte b=0;
4) b--;
5)
6) }
7) }
what is the value of b at line 5?
A.-1 B.255 C.127 D.compile fail E.compile succeeded but run error
Answer:D
40. public class ExceptionTest{
class TestException extends Exception{}
public void runTest() throws TestException{}
public void test() /* point x */ {
runTest();
}
}
At point x, which code can be add on to make the code compile?
A.throws Exception B.catch (Exception e)
Answer:A
41. String foo="blue";
boolean[] bar=new boolean[1];
if(bar[0]){
foo="green";
}
what is the value of foo?
A."" B.null C.blue D.green
Answer:C
42. public class X{
public static void main(String args[]){
Object o1=new Object();
Object o2=o1;
if(o1.equals(o2)){
System.out.prinln("Equal");
}
}
}
what is result?
Answer:Equal