标签:getname extend button line extends arc Plan near char
public class GC {
private Object o;
private voiddoSomethingElse(Object obj) { o = obj; }
public void doSomething() {
Object o = new Object();
doSomethingElse(o);
o = new Object();
doSomethingElse(null);
o = null;
}
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
用关键字new实例化一个对象A时,首先会调用父类的构造方法,然后再为A分配空间,完毕后就返回对象A的一个引用。
在这题中,第5行Object o = new Object();首先在堆栈中为Object类的一个实例分配空间,并且默认初始化为null,然后返回其引用并将其值赋给了 o o是存在堆栈中的。 假设这个实例是A 这样一来,o 便引用了A。
第6行:doSomethingElse(o) 调用此方法后,第5行中的o的值传递给了第2行中的o,所以第2行中的o和第5行的o引用了同一个对象实例,也就是都引用A
第7行:o=new OBject(); 这里又实例化类Object并返回了一个新的引用。局部变量o引用类Object的另一个实例 假设这个实例是B
现在呢,局部变量o引用A 全局变量o引用B
第8行:doSomethingElse(null) 这里把null传给了全局变量o B呢就没有被引用了,一个实例没有被引用,也就失去了存在的意义。所以在这里,B就可能会被垃圾回收机制回收,释放其在堆栈中所占用的空间。(可能会被回收,是因为垃圾回收机制是间隔一定时间后执行一次,系统才回收没有被引用的实例,在回收之前,而且程序在运行,那它是一直存在内存空间中的)
第9行和第8行一个道理,局部变量o也没引用A了
public static void test(Stringstr) {
int check = 4;
if (check = str.length()) {
System.out.print(str.charAt(check-= 1) +", ");
} else {
System.out.print(str.charAt(0)+ ", ");
}
} and the invocation:
test("four");
test("tee");
What is the result?
A. r, t, t,
B. r, e, o,
C. Compilation fails.//第13行应该为==号
D. An exception is thrown atruntime.
Answer: C
QUESTION 35 Given: 31. class Foo { 32. public int a = 3; 33. public void addFive() { a += 5; System.out.print("f "); } 34. } 35. class Bar extends Foo { 36. public int a = 8; 37. public void addFive() { this.a += 5; System.out.print("b " ); } 38. }
Invoked with: Foo f = new Bar();
f.addFive();
System.out.println(f.a); //此处代码,相当于只打印了f的属性; What is the result? A. b 3 B. b 8 C. b 13 D. f 3 E. f 8 F. f 13 G. Compilation fails. H. An exception is thrown at runtime.
属性是没有多态的只有方法有,在方法中调用的是Bar中的a
QUESTION 84
Given:
命令行参数 -D<propertyName>=value 在虚拟机的系统属性中设置属性名/值对,运行在此虚拟机之上的应用程序可用System.getProperty(“propertyName”)得到value的值。 如果value中有空格,则需要用双引号将该值括起来,如-Dname=”space string”。 该参数通常用于设置系统级全局变量值,如配置文件路径,应为该属性在程序中任何地方都可访问。
static Properties getProperties() 功能:Determines the current system properties.
String getProperty(String key) 功能:Searches for the property with the specified key in this property list.
QUESTION 85
Given:
^ 是异或,第一次两边都是true,不会输出1,第二次两边都是false,不会输出2
QUESTION 88 Given:
public class Plant {
private String name;
publicPlant(String name) { this.name = name; }
public String getName() {return name; }
}
public class Tree extends Plant{
public void growFruit() { }
public void dropLeaves() { }
Which statement is true?
A. The code will compile withoutchanges.
B. The code will compile if publicTree() { Plant(); } is added to the Tree class.
C. The code will compile if publicPlant() { Tree(); } is added to the Plant class.
D. The code will compile if publicPlant() { this("fern"); } is added to the Plant class.
E. The code will compile if publicPlant() { Plant("fern"); } is added to the Plant class.
Answer: D
Section: (none)
Explanation/Reference: Explanation:父类只有一个带参数的构造方法,子类必须直接继该方法,可以增加子类的带参构造方法或者
增加父类的默认构造方法。
QUESTION 90
Given:
public enum Title {
MR("Mr."),MRS("Mrs."), MS("Ms.");
private final String title;
private Title(String t) { title= t; }
public String format(Stringlast, String first) {
return title + " " +first + " " + last;
}
}
public static voidmain(String[] args) {
System.out.println(Title.MR.format("Doe","John"));
What is the result?
A. Mr. John Doe
B. An exception is thrown atruntime.
C. Compilation fails because of anerror in line 12.D. Compilation fails because of an error in line 15. E.Compilation fails because of an error in line 20.
Answer: A
Title:枚举类型,MR是枚举的常量,对应的代码是private final String title;
则16行代码返回时候的代码是MR. first(后面的参数John),last(Doe第一个参数)答案为A
QUESTION 95
Click the Exhibit button. What is the result?
A. 4321
B. 0000
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 18.
Answer: D
Person中没有默认的无参数的构造函数,所以Employee中的构造函数必须显式调用super(id);
QUESTION 96
Given:
HashSet不允许有重复,加两次和加一次一样。第14行remove会失败,因为哈希表中没有47。
remove(47)这行代码不会成功!!!!没有47数据在set里面。
QUESTION 97 Given:
public class Score implementsComparable<Score> {
private int wins, losses;
public Score(int w, int l) {wins = w; losses = l; }
public int getWins() { returnwins; }
public int getLosses() { returnlosses; }
public String toString() {
return "<" + wins+ "," + losses + ">";
}
// insert code here
Which method will complete this class?
A. public int compareTo(Objecto){/more code here/}
B. public int compareTo(Scoreother){/more code here/}
C. public int compare(Scores1,Score s2){/more code here/}
D. public int compare(Objecto1,Object o2){/more code here/}
Answer:B
omparable接口只有一个方法,public int CompareTo(T other)+
QUESTION 98
A programmer has an algorithm thatrequires a java.util.List that provides an efficient implementation of add(0,object), but does NOT need to support quick random access. What supports theserequirements?
可以有add方法,但是不能随机访问,哪个支持?
A. java.util.Queue
B. java.util.ArrayList
C. java.util.LinearList
D. java.util.LinkedList
Answer: D
QUESTION 99
Given:
Answer: B
由于subs被限制为s的视图view,只能是在608~611之间。如果超出了,会抛出一个Java.lang.IllegalArgumentException异常。
如果是下列程序:
[java] view plain copy
import java.util.TreeSet;
public class Explorer3 {
public static void main(String[] args) {
TreeSet<Integer> s = new TreeSet<Integer>();
TreeSet<Integer> subs = new TreeSet<Integer>();
for(int i = 606; 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);
}
}
则输出是:[606, 608, 609, 610, 612] [608, 609, 610]
而按照以下更改:
[java] view plain copy
import java.util.TreeSet;
public class Explorer3 {
public static void main(String[] args) {
TreeSet<Integer> s = new TreeSet<Integer>();
TreeSet<Integer> subs = new TreeSet<Integer>();
for(int i = 606; i < 613; i++)
if(i%2 == 0) s.add(i);
subs = (TreeSet)s.subSet(608, true, 611, true);
s.add(609);
System.out.println(s + " " + subs);
}
}
输出依然是:[606, 608, 609, 610, 612] [608, 609, 610]
可以说subs是s的一部分,谁更改都会影响到另一个。
QUESTION 100
Given:
Answer: DF
标签:getname extend button line extends arc Plan near char
原文地址:http://blog.51cto.com/2096101/2136779