Java经典试题
Java 经典试题(1)
Simulated Test of SCJP for JAVA2 PlatFORM (only for training)
1.
1) public class ReturnIt{
2) returnType methodA(byte x, double y){
3) return (short)x/y*2;
4) }
5) }
what is valid returnType for methodA in line 2?
Answer: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
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
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
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
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?
Answer:caught IOException
exception can catch once only
if change the sequence of catch
catch(IOException e) can't execute and compiler will report rongyu error
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. B.i=5 j=5
resized
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;
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

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。