锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
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);
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涓巆onstructor鏈変竴涓負(fù)絀烘椂錛屼袱涓殑access 鐩稿悓錛屼絾鍙互鑷瀹氫箟錛屼嬌涔嬩笉鍚岋紝濡傦細(xì)
聽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);
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涓殑鏃╃粦瀹氫笌鏅氱粦瀹氾細(xì)
聽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(鈥淗ello鈥?;
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(鈥淎鈥?;
6. }
7. catch (Exception ex) {
8. System.out.print(鈥淐鈥?;
9. }
10. finally {
11. System.out.print(鈥淏鈥?;
12. }
13. System.out.print(鈥淒鈥?;
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 鈥淭ested鈥?
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
-----------------
淇敼鍚庯細(xì)
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!
class CardBoard {
Short story = 5;
CardBoard go(CardBoard cb) {
cb = null;
return cb;
}
public static void main(String[] args) {
CardBoard c1 = new CardBoard();
CardBoard c2 = new CardBoard();
CardBoard c3 = c1.go(c2);
c1 = null;
// do Stuff
}
}
When // doStuff is reached, how many objects are eligible for GC?
A. 0
B. 1
C. 2
D. Compilation fails.
E. It is not possible to know.
F. An exception is thrown at runtime.
---------------------------------------------
> The object originally pointed to by c1 is eligible,
> because you have no reference to it. You still have
> a reference to the object pointed to by c2, because
> Java is pass by value, so in "go", the parameter is
> set to null, not c2. c3 never gets an object--it is
> set to null immediately, because null is the return
> value of c1.go(c2).