SCJP认证复习——经典题库

 
我把 147 104 的题综合了一下,从个人的观点做了一些解析。也把其中的一些错误纠正了一些,多数题都通过了上机实测。但个人能力有限,对答案的正确性仍然不敢保证全都正确,所以请各位自行处理。
 
 
1.
Given:
1. public class returnIt {
2. returnType methodA(byte x, double y){
3. return (short) x/y * 2;
4.      }
5. }
 
What is the valid returnType for methodA in line 2?
A.     int
B.     byte
C.     long
D.     short
E.     float
F.     double
 
 
Answer F
注释 :short 类型的 x ,除以 double 类型的 y ,再乘 int 2 ,所以结果是 double 类型的。注意第三行的强制转换,只是转换了 x
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
注意这道题主要考的是方法的 overload override 。对于 overload ,只有参数列表不同,才做为标准,而返回值和访问控制关键字不能做为标准,所以 B 错在方法名相同,但只有返回值不同,这是错的。 C D 是正确的 overload 。对于 override ,则访问控制关键字只能更加公有化,异常只能是超类方法抛出的异常的子类,也可以不抛出。返回类型,参数列表必须精确匹配。所以 A 是正确的 override
 
3.
1)public class Foo{
2)     public static void main(String args[]){
3)     try{return;}
4)     finally{ System.out.println("Finally");}
5)     }
6)     }
what is the result?
A. The program runs and prints nothing.
B. The program runs and prints “Finally”.
C. The code compiles, but an exception is thrown at runtime.
D. The code will not compile because the catch block is missing.
 
Answer:b
try......catch......finally 的问题。程序中如果遇到 return ,则 finally 块先被执行,然后再执行 retrun ,而 finally 块后面的语句将不被执行。如果遇到 System.exit(1) ,则 finally 块及其后的语句都不执行,整个程序退出,还执行什么呀。
 
4.
1) public class Test{
2) public static String output="";
3) public static void foo(int i){
4)   try {
5)        if(i==1){
6)                    throw new Exception();
7)               }
8)         output +="1";
9)       }
10)     catch(Exception e){
11)                     output+="2";
12)                     return;
13)                      }
14)     finally{
15)                output+="3";
16)           }
17)      output+="4";
18) }
19) public static void main(String args[]){
20)              foo(0);
21)              foo(1);
22)    
23)   }
24) }
what is the value of output at line 22?
 
 
Asnwer:13423
执行第一个 foo(0) 时,执行第 8 条语句, output=1 ,然后执行语句 15 output=13 ,然后是 17 条, output=134 ,因为是 static 类型的变量,所以任何对其值的修改都有效。执行第二条 foo(1), 先执行语句 5 ,结果抛出异常,转到 catch 块, output=1342 finally 任何情况下都执行,所以 output=13423 ,然后 return 跳出方法体,所以 output=13423
5
1)public class IfElse{
2)public static void main(String args[]){
3)if(odd(5))
4)System.out.println("odd");
5)else
6)System.out.println("even");
7)}
8)public static int odd(int x){return x%2;}   
9)}
what is output?
 
Answer: 编译错误。
if 中的判断条件的结果必须是 boolean 类型的。注意这里说的是结果 .
 
6 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1)class ExceptionTest{
2)public static void main(String args[]){
3)try{
4)methodA();
5)}catch(IOException e){
6)System.out.println("caught IOException");
7)}catch(Exception e){
8)System.out.println("caught Exception");
9) }
10) }
11)}
If methodA() throws a IOException, what is the result?
 
Answer: caught IOException
如果 methodA() 抛出 IOExecption ,被语句 6 捕获,输出 caught IOException ,然后呢??然后就结束了呗。
7
1)int i=1,j=10;
2)do{
3)          if(i++>--j) continue;
4)}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
程序一直循环,直到 i=4 j=6 时,执行完语句 3 后, i ++ ,这时 i 就等于了 5 continue 后就不能再循环了,所以选 D
8
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 ,所以 o 不能满足条件,当第 7 条语句被执行后,就再也没有对象指向 o 了,所以选 D
9 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@   
   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?
A. Compilation succeeds.
B. An error at line 2 causes compilation to fail.
C. An error at line 9 causes compilation to fail.
D. An error at line 10 causes compilation to fail.
E. An error at line 11 causes compilation to fail.
 
Answer: A
编译通过,通过测试的
10
what is reserved(保留) words in java?
A. run
B. default
C. implement
D. import
 
 
Answer: b D
11
1)public class Test{
2) public static void main(String[] args){
3) String foo=args[1];
4) Sring bar=args[2];
5) String baz=args[3];
6) }
7) }
 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][1][2] ,运行时ArrayIndexOutOfBoundsException 这个异常会被抛出,数组越界。
12
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
数组初始化后默认值是 0 ,所以 baz=0+1=1
13
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
其它的系统都会认为是 double 类型,所以出错。说一下 A C 的区别吧, -1 系统会认为是一个 int 类型,把 int 类型再赋给 float 类型的 foo ,当然没错了,可 C 就不同啦, 42e1 int 类型吗??
14
1)public class Foo{
2) public static void main(String args[]){
3) String s;
4) System.out.println("s="+s);
5) }
6) }
 what is the result?
A. The code compiles and “s=” is printed.
B. The code compiles and “s=null” is printed.
C. The code does not compile because string s is not initialized(初始化).
D. The code does not compile because string s cannot be referenced(引用).
E. The code compiles, but a NullPointerException is thrown when toString is called.
 
Answer:C
只有实例变量系统才给予自动赋默认值的这种待遇
 
15  
 1) public class Test{
 2) public static void main(String args[]){
 3) int i=oxFFFFFFF1;
 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
算一算就知道了。
 
16
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
== 两边类型不同不相等。所以 A C 不等。 equals 只能用于引用类型,不能用于基本类型,所以 D 不对,而且两边类型不兼容的话 , 即使对象的内容一样 , 也不相等,所以 E F 不对。
17 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 
1)public class Test{
2) public static void add3(Integer i){
3)        int val=i.intValue();
4)        val+=3;
5)        i=new Integer(val);
6) }
7) public static void main(String args[]){
8)       Integer i=new Integer(0);
9)       add3(i);
10)       System.out.println(i.intValue());
11) }
12)}
 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
在第五行里,程序又操作了 New ,重新分配了内存空间。所以此 i 非彼 i 啦。
18
1)public class Test{
2) public static void main(String[] args){
3)    System.out.println(6^3); //‘^’为XOR
4)    }
5)      }
 what is output?
 
Answer: 5 算呗。
19
1) public class Test{
2) public static void stringReplace(String text){
3)    text=text.replace('j','l');
4) }
5) public static void bufferReplace(StringBuffer text){
6)     text=text.append("c");
7)    }
8) public static void main(String args[]){   
9)    String textString=new String("java");
10)    StringBuffer textBuffer=new StringBuffer("java");
11)     StringReplace(textString);
12)     bufferReplace(textBuffer);
13) System.out.println(textString+textBuffer);
14)     }
15)     }
 what is the output?
 
Answer: javajavac
textString String 类型的,具有不变性,语句 3 其实是创建了一个新的字符串,而不是修改原来的 textString ,而对于 StringBuffer 类型的对象,则所有修改都是实在的。所以在语句 6 textBuffer 变成了 javac ,所以输出为 javajavac
 
20
1)public class ConstOver{
2) public ConstOver(int x, int y, int z){}
3) }
 which two overload the ConstOver constructor?
 A.ConstOver(){}
 B.protected int ConstOver(){}
 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
主要的问题是 overload ,参数列表必须不同,方法名相同,访问控制无限制。也无异常限制。这道题因为是构造器,所以 B D E 不对,因为构造器不能有返回类型。
21
1)public class MethodOver{
2) public void setVar(int a, int b, float c){}
3) }
 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
overload 无访问控制限制,所以 A 对,顺序也属于参数列表,顺序不同也一样是 overload ,所以 C 正确, D 当然正确了,参数列表明显不同。
22 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1)class EnclosingOne{
2)public class InsideOne{}
3) }
4)public class InnerTest{
5) public static void main(String args[]){
6) EnclosingOne eo=new EnclosingOne();
7) //insert code here
8) }
9)}
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
这里边的一些形式是固定的。
1 )静态方法访问非静态内类:
                     方法为:
       Outer myouter=new Outer(); //这里的 myouter 是创建的外部类的对象。
       Outer.Inner myinner=myouter.new Inner(); // myinner 是内类的对象。
       然后再 myinner.showName(); // showName() 是外类中的非静态方法。
2 )非静态方法访问非静态内类
                     直接创建该内部类的对象: new Inner().showName();
3 )静态方法访问静态内类:
                     也是直接创建该内部类的对象,即 Inner myinner = new Inner() ,或者 Outer.Inner myinner = new Outer.Inner() 也行得通哦。
23
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;}  
 
Answer: b
"is a " 意思为是什么:定义了一个超类和一个子类之间的一种直接关系 : 子类是超类的一种。也即是继承的关系
24 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
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: a,b
25
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
又是 override 的问题,参数列表和返回值以及方法名 ( 好像是费话 ) 必须精确匹配 , 访问控制要更公有化 , 如果抛出异常 , 那么必须异常本身或其子集或什么都不抛 .
26
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
27
Given:
1.     public class SyncTest (
2.     private int x;
3.     private int y;
4.     private synchronized void setX (int i) (x=1;)
5.     private synchronized void setY (int i) (y=1;)
6.     public void setXY(int 1)(set X(i); setY(i);)
7.     public synchronized Boolean check() (return x !=y;)
8.     )
 
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 called by multiple threads
C.     Check() can return true when multiple threads call setX and setY separately.
D.     Check() can only return true if SyncTest is changed to allow x and y to be set separately.
 
Answer:B 
28
Given:
1.     public class SyncTest {
2.     private int x;
3.     private int y;
4.     public synchronized void setX (int i) (x=1;)
5.     public synchronized void setY (int i) (y=1;)
6.     public synchronized void setXY(int 1)(set X(i); setY(i);)
7.     public synchronized Boolean check() (return x !=y;)
8.     )
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 called by multiple threads.
C. Check() can return true when multiple threads call setX and setY separately.
D. Check() can only return true if SyncTest is changed to allow x and y to be set separately.
 
Answer: A
哪一个不加锁,就从哪一个入手,但这道题全都加锁了,所以先 A
29
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.An error at line 11 causes compilation to fail.
B.Errors at lines 6 and 7cause compilation to fail.
C.The program prints pairs of values for x and y that might not always be the same on the same    line (for example, “x=2, y=1”)
D.The program prints pairs of values for x and y that are always the same on the same line (for example, “x=1, y=1”. In addition, each value appears twice (for example, “x=1, y=1” followed by “x=1, y=1”)
E.The program prints pairs of values for x and y that are always the same on the same line (for example, “x=1, y=1”. In addition, each value appears twice (for example, “x=1, y=1” followed by “x=2, y=2”)
 
Answer: E
这道题有问题,当两个线程同名时,输出</
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值