ocjp 考试题1

  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

用关键字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了

  1. public static void test(Stringstr) {

  2. int check = 4;

  3. if (check = str.length()) {

  4. System.out.print(str.charAt(check-= 1) +", ");

  5. } else {

  6. System.out.print(str.charAt(0)+ ", ");

  7. }

  8. } and the invocation:

  9. test("four");

  10. test("tee");

  11. test("to");

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:

  1. public class Commander {
  2. public static void main(String[] args) {
  3. String myProp = / insert code here /
  4. System.out.println(myProp);
  5. }
  6. }
    and the command line: java -Dprop.custom=gobstopper Commander Which two, placed on line 13, will
    produce 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

命令行参数 -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:

  1. public class Spock {
  2. public static void main(String[] args) {
  3. Long tail = 2000L;
  4. Long distance = 1999L;
  5. Long story = 1000L;
  6. if((tail > distance) ^ ((story * 2) == tail))
  7. System.out.print("1");
  8. if((distance + 1 != tail) ^ ((story * 2) == distance))
  9. System.out.print("2");
  10. }
  11. }
    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.
    Answer: E

^ 是异或,第一次两边都是true,不会输出1,第二次两边都是false,不会输出2

QUESTION 88 Given:

  1. public class Plant {

  2. private String name;

  3. publicPlant(String name) { this.name = name; }

  4. public String getName() {return name; }

  5. }

  6. public class Tree extends Plant{

  7. public void growFruit() { }

  8. public void dropLeaves() { }

  9. }

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:

  1. public enum Title {

  2. MR("Mr."),MRS("Mrs."), MS("Ms.");

  3. private final String title;

  4. private Title(String t) { title= t; }

  5. public String format(Stringlast, String first) {

  6. return title + " " +first + " " + last;

  7. }

  8. }

  9. public static voidmain(String[] args) {

  10. System.out.println(Title.MR.format("Doe","John"));

  11. }

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:

  1. import java.util.*;
  2. public class Mapit {
  3. public static void main(String[] args) {
  4. Set<Integer> set = new HashSet<Integer>();
  5. Integer i1 = 45;
  6. Integer i2 = 46;
  7. set.add(i1);
  8. set.add(i1);
  9. set.add(i2); System.out.print(set.size() + " ");
  10. set.remove(i1); System.out.print(set.size() + " ");
  11. i2 = 47;
  12. set.remove(i2); System.out.print(set.size() + " ");
  13. }
  14. }
    What is the result?
    A. 2 1 0
    B. 2 1 1
    C. 3 2 1
    D. 3 2 2
    E. Compilation fails.
    F. An exception is thrown at runtime.
    Answer: B

HashSet不允许有重复,加两次和加一次一样。第14行remove会失败,因为哈希表中没有47。

remove(47)这行代码不会成功!!!!没有47数据在set里面。

QUESTION 97 Given:

  1. public class Score implementsComparable<Score> {

  2. private int wins, losses;

  3. public Score(int w, int l) {wins = w; losses = l; }

  4. public int getWins() { returnwins; }

  5. public int getLosses() { returnlosses; }

  6. public String toString() {

  7. return "<" + wins+ "," + losses + ">";

  8. }

  9. // insert code here

  10. }

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:

  1. import java.util.*;
  2. public class Explorer3 {
  3. public static void main(String[] args) {
  4. TreeSet<Integer> s = new TreeSet<Integer>();
  5. TreeSet<Integer> subs = new TreeSet<Integer>();
  6. for(int i = 606; i < 613; i++)
  7. if(i%2 == 0) s.add(i);
  8. subs = (TreeSet)s.subSet(608, true, 611, true);
  9. subs.add(629);
  10. System.out.println(s + " " + subs);
  11. }
  12. }
    What is the result?
    A. Compilation fails.
    B. An exception is thrown at runtime.
    C. [608, 610, 612, 629] [608, 610]
    D. [608, 610, 612, 629] [608, 610, 629]
    E. [606, 608, 610, 612, 629] [608, 610]
    F. [606, 608, 610, 612, 629] [608, 610, 629]

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:

  1. // insert code here
  2. private N min, max;
  3. public N getMin() { return min; }
  4. public N getMax() { return max; }
  5. public void add(N added) {
  6. if (min == null || added.doubleValue() < min.doubleValue())
  7. min = added;
  8. if (max == null || added.doubleValue() > max.doubleValue()) 19. max = added;
  9. }
  10. }
    Which two, inserted at line 11, will allow the code to compile? (Choose two.)
    A. public class MinMax<?> {
    B. public class MinMax<? extends Number> {
    C. public class MinMax<N extends Object> {
    D. public class MinMax<N extends Number> {
    E. public class MinMax<? extends Object> {
    F. public class MinMax<N extends Integer> {

Answer: DF

猜你喜欢

转载自blog.51cto.com/2096101/2136779