﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>语源科技BlogJava-fxfeiyi</title><link>http://www.blogjava.net/fxfeiyi/</link><description>JavaBlog</description><language>zh-cn</language><lastBuildDate>Mon, 13 Apr 2026 09:01:25 GMT</lastBuildDate><pubDate>Mon, 13 Apr 2026 09:01:25 GMT</pubDate><ttl>60</ttl><item><title>SCJP 经典问题集锦</title><link>http://www.blogjava.net/fxfeiyi/archive/2006/08/04/61853.html</link><dc:creator>fxfeiyi</dc:creator><author>fxfeiyi</author><pubDate>Fri, 04 Aug 2006 13:26:00 GMT</pubDate><guid>http://www.blogjava.net/fxfeiyi/archive/2006/08/04/61853.html</guid><description><![CDATA[
		<p>
				<font size="2">以下为个人考scjp备考过程中积累的几个问题，值得深入研究。<br /><br />QUESTION NO: 14 <br />Given: <br />1. package test1; <br />2. public class Test1 { <br />3. static int x = 42; <br />4. } <br />1. package test2; <br />2. public class Test2 extends test1.Test1 { <br />3. public static void main(String[] args) { <br />4. System.out.println(“x = “ + x); <br />5. } <br />6. } <br />What is the result? <br /> <br />A.  x = 0 <br />B.  x = 42 <br />C.  Compilation fails because of an error in line 2 of class Test2. <br />D.  Compilation fails because of an error in line 3 of class Test1. <br />E.  Compilation fails because of an error in line 4 of class Test2. <br /> <br /> <br />Answer: C <br /><br />-------------------------------------------------------------------------------<br /><br />QUESTION NO: 35 <br />Given: <br />10. public Object m() { <br />11. Object o = new Float(3.14F); <br />12. Object [] oa = new Object[1]; <br />13. oa[0] = o; <br />14. o = null; <br />15. return oa[0]; <br />16. } <br />When is the Float object, created in line 11, eligible for garbage collection? <br /> <br />A.  Just after line 13. <br />B.  Just after line 14. <br />C.  Never in this method. <br />D.  Just after line 15 (that is, as the method returns). <br /> <br /> <br />Answer: C <br />The correct answer to this question is C. The object is never garbage collected simply becau<br />the method returns it. Think about it, the message that receives the object might depend on it<br />so it must be sure that the object received by the method wont be garbage collected. Only in <br />this situation a local object  wont be eligible for garbage collection. Otherwise, a local object<br />is eligible for garbage collection as soon as the method ends. <br /><br />--------------------------------------------------------------------------------------<br />QUESTION NO: 43 <br />Given: <br />1. class TestA { <br />2. TestB b; <br />3. TestA() { <br />4. b = new TestB(this); <br />5. } <br />6. } <br />7. class TestB { <br />8. TestA a; <br />9. TestB(TestA a) { <br />10. this.a = a; <br />11. } <br />12. } <br />13. class TestAll { <br />14. public static void main (String args[]) { <br />15. new TestAll().makeThings(); <br />16. // ...code continues on <br />17. } <br />18. void makeThings() { <br />19. TestA test = new TestA(); <br />20. } <br />21. } <br />Which two statements are true after line 15, before main completes? (Choose two) <br /> A.  Line 15 causes a stack overflow. <br />B.  An exception is thrown at runtime. <br />C.  The object referenced by a is eligible for garbage collection. <br />D.  The object referenced by b is eligible for garbage collection. <br />E.  The object referenced by a is not eligible for garbage collection. <br />F.  The object referenced by b is not eligible for garbage collection. <br /> <br /> <br />Answer: C, D <br />This is a typical example of the island of isolation. On line 15, the two objects TestA and <br />TestB have a reference to one an other. Therefore, the correct answers are C. and D. A key <br />point to remember is that an object that is referenced by another object can be eligible for <br />garbage collection if the two objects form an island of isolated objects.   <br /> <br />------------------------------------------------------------------------------------------------<br /><br />B.  <strong>The default constructor has the same access as its class. </strong><br /><br />class与constructor有一个为空时，两个的access 相同，但可以自行定义，使之不同，如：<br /><br /> class TestConstrutor {<br /><br />       public TestConstrutor(){};<br />       private TestConstrutor(String s){};<br />        public static void main(String[] args) {<br />                  new TestConstrutor();<br />    }<br /><br />}<br /><br />------------------------------------------------------------------------------------------------<br /><br />QUESTION NO: 54 <br />You want to limit access to a method of a public class to members of <strong>the same class</strong>. <br />Which access accomplishes this objective? <br /> <br />A.  public <br /><strong>B.  private</strong><br />C.  protected <br />D.  transient <br />E.  default access <br /> <br /> <br />Answer: B <br /><br />------------------------------------------------------------------------------------------------<br />QUESTION NO: 65 <br />Given: <br />1. public class Test { <br />2. public static void main(String Args[]) { <br />3. int i =1, j = 0; <br />4. switch(i) { <br />5. case 2: j +=6; <br />6. case 4: j +=1; <br />7. default: j +=2; <br />8. case 0: j +=4; <br />9. } <br />10. System.out.println(“j =” +j); <br />11. } <br />12. } <br />What is the result? <br /> <br />A.  0 <br />B.  2 <br />C.  4 <br />D.  6 <br />E.  9 <br />F.  13 <br />  <br />Answer: D <br /><br />public class TestSwitch {<br />  public static void main(String Args[]) { <br />  int i =1, j = 0; <br />  switch(i) { <br />  case 2: j +=6; <br />  case 4: j +=1; <br />  default: j +=2; <br />  case 0: j +=4;  <br />  } <br />  System.out.println("j =" +j); <br />  } <br />} <br />//j=6<br />//default<br />//case 0<br />//system.out</font>
				<br />-------------------------------<br />QUESTION NO: 66 <br />Given: <br />1. class A { <br />2. } <br />3. class Alpha { <br />4. private A myA = new A(); <br />5.  <br />6. void dolt( A a ) { <br />7. a = null; <br />8. } <br />9. void tryIt() { <br />10. dolt( myA ); <br />11. } <br />12. } <br />Which two statements are correct? (Choose two) <br /> <br />A.  There are no instanced of A that will become eligible for garbage collection. <br />B.  Explicitly setting myA to null marks that instance to be eligible for garbage collection. <br />C.  Any call on tryIt() causes the private instance of A to be marked for garbage <br />collection. <br />D.  Private instances of A become eligible for garbage collection when instances of Alpha <br />become eligible for garbage collection. <br /> <br /> Answer: B, D<br /> <br />-------------------------------<br /><br />java中的早绑定与晚绑定：<br /><br /> class Super { <br />  public int i = 0; <br />  private void method1(){//private is final early binding<br />   System.out.println("super's method1()");<br />  }<br />  public void method2(){<br />  System.out.println("super's method2()");<br />  method1();<br />  }<br />  public Super getThis(){<br />   return this;<br />  }<br />    public Super(){}//must have,though TestSwitch not call()<br />  public Super(String text) { <br />    i = 1; <br />  } <br /> } <br />  <br /> public class TestSwitch extends Super { <br /> public void method1(){<br />   System.out.println("TestSwitch's method1()");<br />   }<br /> public Super getThis(){<br />     return this;<br />  }<br />  public TestSwitch(String text) { <br />    i = 2; <br />    method1();//can't call super.method1()<br />  } <br />  public static void main(String args[]) { <br />   Super sub = new TestSwitch("Hello"); <br />   System.out.println("sub's i is: "+sub.i); <br />   sub.method2();<br />   System.out.println("getThis is: "+sub.getThis().toString());<br />   <br />  } <br /> } <br />---------------------------------<br />TestSwitch's method1()<br />sub's i is: 2<br />super's method2()<br />super's method1()<br />getThis is: <a href="mailto:TestSwitch@35ce36">TestSwitch@35ce36</a><br />---------------------------------<br />Given: <br />1. class Super { <br />2. public int i = 0; <br />3.  <br />4. public Super(String text) { <br />5. i = 1; <br />6. } <br />7. } <br />8.  <br />9. public class Sub extends Super { <br />10. public Sub(String text) { <br />11. i = 2; <br />12. } <br />13.  <br />14. public static void main(String args[]) { <br />15. Sub sub = new Sub(“Hello”); <br />16. System.out.println(sub.i); <br />17. } <br />18. } <br />What is the result? <br /> <br />A.  0 <br />B.  1 <br />C.  2 <br />D.  Compilation fails. <br /> <br /> <br />Answer: C <br />This code is perfectly legal and the answer is C. <br />-------------------------------------------------<br />QUESTION NO: 71 <br />Given: <br />1. public class X { <br />2. public static void main(String [] args) { <br />3. try { <br />4. badMethod(); <br />5. System.out.print(“A”); <br />6. } <br />7. catch (Exception ex) { <br />8. System.out.print(“C”); <br />9. } <br />10. finally { <br />11. System.out.print(“B”); <br />12. } <br />13. System.out.print(“D”); <br />14. } <br />15. public static void badMethod() { <br />16. throw new Error(); <br />17. } <br />18. } <br />What is the result? <br /> <br />A.  ABCD <br />B.  Compilation fails. <br />C.  C is printed before exiting with an error message. <br />D.  BC is printed before exiting with an error message. <br />E.  BCD is printed before exiting with an error message. <br /> <br /> <br />Answer: B <br />The correct answer is : B is printed and then an error message is printed. The exception catch <br />can not catch an Error because this class does not extend Exception but it implements <br />throwable. <br /><font color="#ff0033"><font color="#0066ff">B</font>Exception in thread "main" java.lang.Error<br />at X.badMethod(X.java:17)<br />at X.main(X.java:5)<br /></font><br /> ----------------------------<br />QUESTION NO: 77 <br />Given: <br />1. interface Beta {} <br />2.  <br />3. class Alpha implements Beta { <br />4. String testIt() { <br />5. return “Tested”; <br />6. } <br />7. } <br />8.  <br />9. public class Main1 { <br />10. static Beta getIt() { <br />11. return new Alpha(); <br />12. } <br />13. public static void main( String[] args ) { <br />14. Beta b = getIt(); <br />15. System.out.println( b.testIt() ); <br />16. } <br />17. } <br />What is the result? <br /> <br />A.  Tested <br />B.  Compilation fails. <br />C.  The code runs with no output. <br />D.  An exception is thrown at runtime. <br /> <br />Answer: B <br />-----------------<br />修改后：<br /><br />interface Beta1 {<br />  abstract String testIt();<br />  } </p>
		<p>class Alpha implements Beta1 { <br />  public String testIt() { <br />   return "Tested"; <br />  } <br />} <br /> <br />public class Main1 { <br />   static Beta1 getIt() { <br />    return new Alpha(); <br />   } <br />    public static void main( String[] args ) { <br />        Beta1 b = getIt(); <br />     System.out.println( b.testIt() ); <br />    } <br /> } <br /><br />F:\Eclipse_d\workspace\scjp&gt;javac Main1.java<br />F:\Eclipse_d\workspace\scjp&gt;java Main1<br />Tested<br />-------------------------------<br /><br />public class Outer{ <br /> public void someOuterMethod() { <br />  new Inner();<br /> } <br /> public static void getInner(){<br />  //new Inner();<br />  new Outer().new Inner();<br /> }<br /> public class Inner{<br />  public Inner(){<br />   System.out.println("Inner class!");<br />  }  <br /> } <br />public static void main( String[]argv ) { <br /> <br /> Outer o = new Outer(); <br /> o.someOuterMethod();<br /> Outer.Inner inn=new Outer().new Inner();<br /> System.out.println("inner's toString(): "+inn.toString());<br /> System.out.println("inner's getClass(): "+inn.getClass());<br /> System.out.println("inner's hashCode(): "+inn.hashCode());<br /> System.out.println("inner's getClass().getName(): "+inn.getClass().getName());<br /> System.out.println("inner's getClass().hashCode(): "+inn.getClass().hashCode());<br /> System.out.println("inner's getClass().getModifiers: "+inn.getClass().getModifiers());<br /> getInner();<br />} <br /> } <br />---<br />Inner class!<br />Inner class!<br />inner's toString(): <a href="mailto:Outer$Inner@757aef">Outer$Inner@757aef</a><br />inner's getClass(): class Outer$Inner<br />inner's hashCode(): 7699183<br />inner's getClass().getName(): Outer$Inner<br />inner's getClass().hashCode(): 14285251<br />inner's getClass().getModifiers: 1<br />Inner class!</p>
<img src ="http://www.blogjava.net/fxfeiyi/aggbug/61853.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/fxfeiyi/" target="_blank">fxfeiyi</a> 2006-08-04 21:26 <a href="http://www.blogjava.net/fxfeiyi/archive/2006/08/04/61853.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>垃圾回收经典问题</title><link>http://www.blogjava.net/fxfeiyi/archive/2006/08/04/61855.html</link><dc:creator>fxfeiyi</dc:creator><author>fxfeiyi</author><pubDate>Fri, 04 Aug 2006 13:19:00 GMT</pubDate><guid>http://www.blogjava.net/fxfeiyi/archive/2006/08/04/61855.html</guid><wfw:comment>http://www.blogjava.net/fxfeiyi/comments/61855.html</wfw:comment><comments>http://www.blogjava.net/fxfeiyi/archive/2006/08/04/61855.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/fxfeiyi/comments/commentRss/61855.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/fxfeiyi/services/trackbacks/61855.html</trackback:ping><description><![CDATA[
		<p>2. Given:</p>
		<p>class CardBoard {<br />Short story = 5;<br />CardBoard go(CardBoard cb) {<br />cb = null;<br />return cb;<br />}<br />public static void main(String[] args) {<br />CardBoard c1 = new CardBoard();<br />CardBoard c2 = new CardBoard();<br />CardBoard c3 = c1.go(c2);<br />c1 = null;<br />// do Stuff<br />}<br />}</p>
		<p>When // doStuff is reached, how many objects are eligible for GC?</p>
		<p>A. 0<br />B. 1<br />C. 2<br />D. Compilation fails.<br />E. It is not possible to know.<br />F. An exception is thrown at runtime.<br />---------------------------------------------<br />&gt; The object originally pointed to by c1 is eligible,<br />&gt; because you have no reference to it. You still have<br />&gt; a reference to the object pointed to by c2, because<br />&gt; Java is pass by value, so in "go", the parameter is<br />&gt; set to null, not c2. c3 never gets an object--it is<br />&gt; set to null immediately, because null is the return<br />&gt; value of c1.go(c2).<br /></p>
<img src ="http://www.blogjava.net/fxfeiyi/aggbug/61855.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/fxfeiyi/" target="_blank">fxfeiyi</a> 2006-08-04 21:19 <a href="http://www.blogjava.net/fxfeiyi/archive/2006/08/04/61855.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>