1.     public class GC {

2.     private Object o;

3.     private voiddoSomethingElse(Object obj) { o = obj; }

4.     public void doSomething() {

5.     Object o = new Object();

6.     doSomethingElse(o);

7.     o = new Object();

8.     doSomethingElse(null);

9.     o = null;

10. }

11. }

When the doSomething method is called,after which line does the Object created in line 5 become available for garbagecollection?

A. Line 5 B. Line 6 C. Line 7 D. Line 8 E.Line 9

F. Line 10

Answer: D

  1. 用关键字new实例化一个对象A时,首先会调用父类的构造方法,然后再为A分配空间,完毕后就返回对象A的一个引用。
  2. 在这题中,第5Object o = new Object();首先在堆栈中为Object类的一个实例分配空间,并且默认初始化为null,然后返回其引用并将其值赋给了 o o是存在堆栈中的。 假设这个实例是A 这样一来,o 便引用了A
  3.  
  4. 6行:doSomethingElse(o) 调用此方法后,第5行中的o的值传递给了第2行中的o,所以第2行中的o和第5行的o引用了同一个对象实例,也就是都引用A
  5.  
  6. 7行:o=new OBject(); 这里又实例化类Object并返回了一个新的引用。局部变量o引用类Object的另一个实例 假设这个实例是B
  7.  
  8. 现在呢,局部变量o引用A 全局变量o引用B
  9.  
  10. 8行:doSomethingElse(null) 这里把null传给了全局变量o B呢就没有被引用了,一个实例没有被引用,也就失去了存在的意义。所以在这里,B就可能会被垃圾回收机制回收,释放其在堆栈中所占用的空间。(可能会被回收,是因为垃圾回收机制是间隔一定时间后执行一次,系统才回收没有被引用的实例,在回收之前,而且程序在运行,那它是一直存在内存空间中的)
  11.  
  12. 9行和第8行一个道理,局部变量o也没引用A
  1.  
  2. 11. public static void test(Stringstr) {
  3.  
  4. 12. int check = 4;
  5.  
  6. 13. if (check = str.length()) {
  7.  
  8. 14. System.out.print(str.charAt(check-= 1) +", ");
  9.  
  10. 15. } else {
  11.  
  12. 16. System.out.print(str.charAt(0)+ ", ");
  13.  
  14. 17. }
  15.  
  16. 18. } and the invocation:
  17.  
  18. 21. test("four");
  19.  
  20. 22. test("tee");
  21.  
  22. 23. test("to");
  23.  
  24. What is the result?
  25.  
  26. A.   r, t, t,
  27.  
  28. B.   r, e, o,
  29.  
  30. C.   Compilation fails.//第13行应该为==号
  31.  
  32. D.   An exception is thrown atruntime.
  33.  
  34. Answer: C
  35.  
  36. QUESTION 35
  37. Given:
  38. 31. class Foo {
  39. 32. public int a = 3;
  40. 33. public void addFive() { a += 5; System.out.print("f "); }
  41. 34. }
  42. 35. class Bar extends Foo {
  43. 36. public int a = 8;
  44. 37. public void addFive() { this.a += 5; System.out.print("b " ); }
  45. 38. }
  46.  
  47. Invoked with: Foo f = new Bar();
  48.  
  49. f.addFive();
  50.  
  51. System.out.println(f.a); //此处代码,相当于只打印了f的属性;
  52. What is the result?
  53. A. b 3
  54. B. b 8
  55. C. b 13
  56. D. f 3
  57. E. f 8
  58. F. f 13
  59. G. Compilation fails.
  60. H. An exception is thrown at runtime.
  61.  
  62. 属性是没有多态的只有方法有,在方法中调用的是Bar中的a
  63.  
  64. QUESTION 84Given:11. public class Commander {12. public static void main(String[] args) {13. String myProp = /* insert code here */14. System.out.println(myProp);15. }16. }and the command line: java -Dprop.custom=gobstopper Commander Which two, placed on line 13, willproduce the output gobstopper? (Choose two.)A. System.load("prop.custom");B. System.getenv("prop.custom");C. System.property("prop.custom");D. System.getProperty("prop.custom");E. System.getProperties().getProperty("prop.custom");Answer: DE
  65.  
  66. 命令行参数   -D<propertyName>=value
  67. 在虚拟机的系统属性中设置属性名/值对,运行在此虚拟机之上的应用程序可用System.getProperty(“propertyName”)得到value的值。
  68. 如果value中有空格,则需要用双引号将该值括起来,如-Dname=”space string”。
  69. 该参数通常用于设置系统级全局变量值,如配置文件路径,应为该属性在程序中任何地方都可访问。
  70.  
  71. static Properties getProperties() 功能:Determines the current system properties.
  72.  
  73. String getProperty(String key)  功能:Searches for the property with the specified key in this property list.

QUESTION 85
Given:
3. public class Spock {
4. public static void main(String[] args) {
5. Long tail = 2000L;
6. Long distance = 1999L;
7. Long story = 1000L;
8. if((tail > distance) ^ ((story * 2) == tail))
9. System.out.print("1");
10. if((distance + 1 != tail) ^ ((story * 2) == distance))
11. System.out.print("2");
12. }
13. }
What is the result?
A. 1
B. 2
C. 12
D. Compilation fails.
E. No output is produced.
F. An exception is thrown at runtime.
; i < 613; i++)

  • if(i%2 == 0) s.add(i);
  • subs = (TreeSet)s.subSet(608, true, 611, true);
  • subs.add(609);
  • System.out.println(s + " " + subs);
  • }
  • }