fxfeiyi

JavaBlog
posts - 2, comments - 0, trackbacks - 0, articles - 0
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

SCJP 经典问题集锦

Posted on 2006-08-04 21:26 fxfeiyi 阅读(380) 评论(0)  编辑  收藏 所属分类: SCJP

以下为个人考scjp备考过程中积累的几个问题,值得深入研究。

QUESTION NO: 14
Given:
1. package test1;
2. public class Test1 {
3. static int x = 42;
4. }
1. package test2;
2. public class Test2 extends test1.Test1 {
3. public static void main(String[] args) {
4. System.out.println(“x = “ + x);
5. }
6. }
What is the result?
 
A.  x = 0
B.  x = 42
C.  Compilation fails because of an error in line 2 of class Test2.
D.  Compilation fails because of an error in line 3 of class Test1.
E.  Compilation fails because of an error in line 4 of class Test2.
 
 
Answer: C

-------------------------------------------------------------------------------

QUESTION NO: 35
Given:
10. public Object m() {
11. Object o = new Float(3.14F);
12. Object [] oa = new Object[1];
13. oa[0] = o;
14. o = null;
15. return oa[0];
16. }
When is the Float object, created in line 11, eligible for garbage collection?
 
A.  Just after line 13.
B.  Just after line 14.
C.  Never in this method.
D.  Just after line 15 (that is, as the method returns).
 
 
Answer: C
The correct answer to this question is C. The object is never garbage collected simply becau
the method returns it. Think about it, the message that receives the object might depend on it
so it must be sure that the object received by the method wont be garbage collected. Only in
this situation a local object  wont be eligible for garbage collection. Otherwise, a local object
is eligible for garbage collection as soon as the method ends.

--------------------------------------------------------------------------------------
QUESTION NO: 43
Given:
1. class TestA {
2. TestB b;
3. TestA() {
4. b = new TestB(this);
5. }
6. }
7. class TestB {
8. TestA a;
9. TestB(TestA a) {
10. this.a = a;
11. }
12. }
13. class TestAll {
14. public static void main (String args[]) {
15. new TestAll().makeThings();
16. // ...code continues on
17. }
18. void makeThings() {
19. TestA test = new TestA();
20. }
21. }
Which two statements are true after line 15, before main completes? (Choose two)
 A.  Line 15 causes a stack overflow.
B.  An exception is thrown at runtime.
C.  The object referenced by a is eligible for garbage collection.
D.  The object referenced by b is eligible for garbage collection.
E.  The object referenced by a is not eligible for garbage collection.
F.  The object referenced by b is not eligible for garbage collection.
 
 
Answer: C, D
This is a typical example of the island of isolation. On line 15, the two objects TestA and
TestB have a reference to one an other. Therefore, the correct answers are C. and D. A key
point to remember is that an object that is referenced by another object can be eligible for
garbage collection if the two objects form an island of isolated objects.   
 
------------------------------------------------------------------------------------------------

B.  The default constructor has the same access as its class.

class与constructor有一个为空时,两个的access 相同,但可以自行定义,使之不同,如:

 class TestConstrutor {

       public TestConstrutor(){};
       private TestConstrutor(String s){};
        public static void main(String[] args) {
                  new TestConstrutor();
    }

}

------------------------------------------------------------------------------------------------

QUESTION NO: 54
You want to limit access to a method of a public class to members of the same class.
Which access accomplishes this objective?
 
A.  public
B.  private
C.  protected
D.  transient
E.  default access
 
 
Answer: B

------------------------------------------------------------------------------------------------
QUESTION NO: 65
Given:
1. public class Test {
2. public static void main(String Args[]) {
3. int i =1, j = 0;
4. switch(i) {
5. case 2: j +=6;
6. case 4: j +=1;
7. default: j +=2;
8. case 0: j +=4;
9. }
10. System.out.println(“j =” +j);
11. }
12. }
What is the result?
 
A.  0
B.  2
C.  4
D.  6
E.  9
F.  13 
  
Answer: D

public class TestSwitch {
  public static void main(String Args[]) {
  int i =1, j = 0;
  switch(i) {
  case 2: j +=6;
  case 4: j +=1;
  default: j +=2;
  case 0: j +=4;  
  }
  System.out.println("j =" +j);
  }
}
//j=6
//default
//case 0
//system.out

-------------------------------
QUESTION NO: 66
Given:
1. class A {
2. }
3. class Alpha {
4. private A myA = new A();
5. 
6. void dolt( A a ) {
7. a = null;
8. }
9. void tryIt() {
10. dolt( myA );
11. }
12. }
Which two statements are correct? (Choose two)
 
A.  There are no instanced of A that will become eligible for garbage collection.
B.  Explicitly setting myA to null marks that instance to be eligible for garbage collection.
C.  Any call on tryIt() causes the private instance of A to be marked for garbage
collection.
D.  Private instances of A become eligible for garbage collection when instances of Alpha
become eligible for garbage collection. 
 
 Answer: B, D
 
-------------------------------

java中的早绑定与晚绑定:

 class Super {
  public int i = 0;
  private void method1(){//private is final early binding
   System.out.println("super's method1()");
  }
  public void method2(){
  System.out.println("super's method2()");
  method1();
  }
  public Super getThis(){
   return this;
  }
    public Super(){}//must have,though TestSwitch not call()
  public Super(String text) {
    i = 1;
  }
 }
 
 public class TestSwitch extends Super {
 public void method1(){
   System.out.println("TestSwitch's method1()");
   }
 public Super getThis(){
     return this;
  }
  public TestSwitch(String text) {
    i = 2;
    method1();//can't call super.method1()
  }
  public static void main(String args[]) {
   Super sub = new TestSwitch("Hello");
   System.out.println("sub's i is: "+sub.i);
   sub.method2();
   System.out.println("getThis is: "+sub.getThis().toString());
   
  }
 }
---------------------------------
TestSwitch's method1()
sub's i is: 2
super's method2()
super's method1()
getThis is: TestSwitch@35ce36
---------------------------------
Given:
1. class Super {
2. public int i = 0;
3. 
4. public Super(String text) {
5. i = 1;
6. }
7. }
8. 
9. public class Sub extends Super {
10. public Sub(String text) {
11. i = 2;
12. }
13. 
14. public static void main(String args[]) {
15. Sub sub = new Sub(“Hello”);
16. System.out.println(sub.i);
17. }
18. }
What is the result?
 
A.  0
B.  1
C.  2
D.  Compilation fails.
 
 
Answer: C
This code is perfectly legal and the answer is C.
-------------------------------------------------
QUESTION NO: 71
Given:
1. public class X {
2. public static void main(String [] args) {
3. try {
4. badMethod();
5. System.out.print(“A”);
6. }
7. catch (Exception ex) {
8. System.out.print(“C”);
9. }
10. finally {
11. System.out.print(“B”);
12. }
13. System.out.print(“D”);
14. }
15. public static void badMethod() {
16. throw new Error();
17. }
18. }
What is the result?
 
A.  ABCD
B.  Compilation fails.
C.  C is printed before exiting with an error message.
D.  BC is printed before exiting with an error message.
E.  BCD is printed before exiting with an error message.
 
 
Answer: B
The correct answer is : B is printed and then an error message is printed. The exception catch
can not catch an Error because this class does not extend Exception but it implements
throwable. 
BException in thread "main" java.lang.Error
at X.badMethod(X.java:17)
at X.main(X.java:5)

 ----------------------------
QUESTION NO: 77
Given:
1. interface Beta {}
2. 
3. class Alpha implements Beta {
4. String testIt() {
5. return “Tested”;
6. }
7. }
8. 
9. public class Main1 {
10. static Beta getIt() {
11. return new Alpha();
12. }
13. public static void main( String[] args ) {
14. Beta b = getIt();
15. System.out.println( b.testIt() );
16. }
17. }
What is the result?
 
A.  Tested
B.  Compilation fails.
C.  The code runs with no output.
D.  An exception is thrown at runtime. 
 
Answer: B
-----------------
修改后:

interface Beta1 {
  abstract String testIt();
  }

class Alpha implements Beta1 {
  public String testIt() {
   return "Tested";
  }
}
 
public class Main1 {
   static Beta1 getIt() {
    return new Alpha();
   }
    public static void main( String[] args ) {
        Beta1 b = getIt();
     System.out.println( b.testIt() );
    }
 }

F:\Eclipse_d\workspace\scjp>javac Main1.java
F:\Eclipse_d\workspace\scjp>java Main1
Tested
-------------------------------

public class Outer{
 public void someOuterMethod() {
  new Inner();
 }
 public static void getInner(){
  //new Inner();
  new Outer().new Inner();
 }
 public class Inner{
  public Inner(){
   System.out.println("Inner class!");
  }  
 }
public static void main( String[]argv ) {
 
 Outer o = new Outer();
 o.someOuterMethod();
 Outer.Inner inn=new Outer().new Inner();
 System.out.println("inner's toString(): "+inn.toString());
 System.out.println("inner's getClass(): "+inn.getClass());
 System.out.println("inner's hashCode(): "+inn.hashCode());
 System.out.println("inner's getClass().getName(): "+inn.getClass().getName());
 System.out.println("inner's getClass().hashCode(): "+inn.getClass().hashCode());
 System.out.println("inner's getClass().getModifiers: "+inn.getClass().getModifiers());
 getInner();
}
 }
---
Inner class!
Inner class!
inner's toString(): Outer$Inner@757aef
inner's getClass(): class Outer$Inner
inner's hashCode(): 7699183
inner's getClass().getName(): Outer$Inner
inner's getClass().hashCode(): 14285251
inner's getClass().getModifiers: 1
Inner class!