OCJP(1Z0-851) 模拟题分析(七)

Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam

 以下分析全都是我自己分析或者参考网上的,定有疏漏,还请大家对我的分析提出质疑。


QUESTION 201

Given:
1. public class TestString3 {
2. public static void main(String[] args) {
3. // insert code here
5. System.out.println(s);
6. }
7. }
Which two code fragments, inserted independently at line 3, generate the output 4247? (Choose two.)
A. String s = "123456789";
s = (s-"123").replace(1,3,"24") - "89";
B. StringBuffer s = new StringBuffer("123456789");
s.delete(0,3).replace(1,3,"24").delete(4,6);
C.  StringBuffer s = new StringBuffer("123456789");
s.substring(3,6).delete(1,3).insert(1, "24");
D.StringBuilder s = new StringBuilder("123456789");
s.substring(3,6).delete(1,2).insert(1, "24");
E.StringBuilder s = new StringBuilder("123456789");
s.delete(0,3).delete(1,3).delete(2,5).insert(1, "24");
Answer: BE

substring(int start, int end)函数是StringBuffer类中的一个方法,是返回特定索引(index从start到end-1)的子字符串,还有一个方法是public StringBuffer delete(int start, int end),是删除特定索引(index从start到end-1)的字符。B和E都可以输出4247。


QUESTION 202
Given:
11. String test = "Test A. Test B. Test C.";
12. // insert code here
13. String[] result = test.split(regex);
Which regular expression, inserted at line 12, correctly splits test into "Test A", "Test B", and "Test C"?
A. String regex = "";
B. String regex = " ";
C. String regex = ".*";
D. String regex = "\\s";
E. String regex = "\\.\\s*";
F. String regex = "\\w[ \.] +";
Answer: E

点和空格,选E,就说一个点 和 一个或者没有空格


QUESTION 203
Which statement is true?
A. A class's finalize() method CANNOT be invoked explicitly.
B. super.finalize() is called implicitly by any overriding finalize() method.
C. The finalize() method for a given object is called no more than once by the garbage collector.
D. The order in which finalize() is called on two objects is based on the order in which the two objects
became finalizable.
Answer: C

finalize() 方法仅仅会在垃圾收集之前被调用,如果没有垃圾收集自然就不会被调用。

finalize()方法可以被显式调用,如super.finalize()。子类中重写的finalize()方法不会隐式调用父类的finalize(),需要程序员自己super.finalize()。


QUESTION 204
Given:
11. public class ItemTest {
12. private final int id;
13. public ItemTest(int id) { this.id = id; }
14. public void updateId(int newId) { id = newId; }
15.
16. public static void main(String[] args) {
17. ItemTest fa = new ItemTest(42);
18. fa.updateId(69);
19. System.out.println(fa.id);
20. }
21. }
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. The attribute id in the ItemTest object remains unchanged.
D. The attribute id in the ItemTest object is modified to the new value.
E. A new ItemTest object is created with the preferred value in the id attribute.
Answer: A

final变量一旦赋值,不能修改~~


QUESTION 205
Given:
11. interface DeclareStuff {
12. public static final int EASY = 3;
13. void doStuff(int t); }
14. public class TestDeclare implements DeclareStuff {
15. public static void main(String [] args) {
16. int x = 5;
17. new TestDeclare().doStuff(++x);
18. }
19. void doStuff(int s) {
20. s += EASY + ++s;
21. System.out.println("s " + s);
22. }
23. }
What is the result?
A. s 14
B. s 16
C. s 10
D. Compilation fails.
E. An exception is thrown at runtime.
Answer: D

doStuff()函数的访问修饰符被reduce了~~interface是pubic void doStuff(),到了TestDeclare里面就成了默认的~~


QUESTION 207
Given:
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, 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




QUESTION 208
Given:
1. interface DoStuff2 {
2. float getRange(int low, int high); }
3.
4. interface DoMore {
5. float getAvg(int a, int b, int c); }
6.
7. abstract class DoAbstract implements DoStuff2, DoMore { }
8.
9. class DoStuff implements DoStuff2 {
10. public float getRange(int x, int y) { return 3.14f; } }
11.
12. interface DoAll extends DoMore {
13. float getAvg(int a, int b, int c, int d); }
What is the result?
A. The file will compile without error.
B. Compilation fails. Only line 7 contains an error.
C. Compilation fails. Only line 12 contains an error.
D. Compilation fails. Only line 13 contains an error.
E. Compilation fails. Only lines 7 and 12 contain errors.
F. Compilation fails. Only lines 7 and 13 contain errors.
G. Compilation fails. Lines 7, 12, and 13 contain errors.

!!!一定要注意!这个特例,abstract类在implements接口的时候可以不实现,而是留给其子类去实现!!!!

编译成功!!


QUESTION 211
Given:
1. package com.company.application;
2.
3. public class MainClass {
4. public static void main(String[] args) {}
5. }
And MainClass exists in the /apps/com/company/application directory. Assume the CLASSPATH
environment variable is set to "." (current directory). Which two java commands entered at the command
line will run MainClass? (Choose two.)
A. java MainClass if run from the /apps directory
B. java com.company.application.MainClass if run from the /apps directory
C. java -classpath /apps com.company.application.MainClass if run from any directory
D. java -classpath . MainClass if run from the /apps/com/company/application directory
E. java -classpath /apps/com/company/application:. MainClass if run from the /apps directory
F. java com.company.application.MainClass if run from the /apps/com/company/application directory
Answer: BC




QUESTION 212
Given:
12. import java.util.*;
13. public class Explorer2 {
14. public static void main(String[] args) {
15. TreeSet<Integer> s = new TreeSet<Integer>();
16. TreeSet<Integer> subs = new TreeSet<Integer>();
17. for(int i = 606; i < 613; i++)
18. if(i%2 == 0) s.add(i);
19. subs = (TreeSet)s.subSet(608, true, 611, true);
20. s.add(629);
21. System.out.println(s + " " + subs);
22. }
23. }
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: E




QUESTION 213
Given that the elements of a PriorityQueue are ordered according to natural ordering, and:
2. import java.util.*;
3. public class GetInLine {
4. public static void main(String[] args) {
5. PriorityQueue<String> pq = new PriorityQueue<String>();
6. pq.add("banana");
7. pq.add("pear");
8. pq.add("apple");
9. System.out.println(pq.poll() + " " + pq.peek());
10. }
11. }
What is the result?
A. apple pear
B. banana pear
C. apple apple
D. apple banana
E. banana banana
Answer: D

pq.poll()和 pq.peek(),前者是取出第一个并返回,后者是返回第一个的副本



QUESTION 214
Given a pre-generics implementation of a method:
11. public static int sum(List list) {
12. int sum = 0;
13. for ( Iterator iter = list.iterator(); iter.hasNext(); ) {
14. int i = ((Integer)iter.next()).intValue();
15. sum += i;
16. }
17. return sum;
18. }
What three changes allow the class to be used with generics and avoid an unchecked warning? (Choose
three.)
A. Remove line 14.
B. Replace line 14 with "int i = iter.next();".
C. Replace line 13 with "for (int i : intList) {".
D. Replace line 13 with "for (Iterator iter : intList) {".
E. Replace the method declaration with "sum(List<int> intList)".
F. Replace the method declaration with "sum(List<Integer> intList)".
Answer: ACF




QUESTION 215
Given:
34. HashMap props = new HashMap();
35. props.put("key45", "some value");
36. props.put("key12", "some other value");
37. props.put("key39", "yet another value");
38. Set s = props.keySet();
39. // insert code here What, inserted at line 39, will sort the keys in the props HashMap?
A. Arrays.sort(s);
B. s = new TreeSet(s);
C. Collections.sort(s);
D. s = new SortedSet(s);
Answer: B

Arrays类中的静态方法sort()必须实现Comparable接口的Object对象数组,即public static void sort(Object[] a),这里是给一个Set排序, 不符合。
Collections.sort(s)其参数必须得是实现了Comparable接口的List<T>对象,这里是给一个实现了Set接口的对象排序, 不符合。

SortedSet是一个接口。



QUESTION 216
Given:
11. public class Person {
12. private String name;
13. public Person(String name) {
14. this.name = name;
15. }
16. public boolean equals(Object o) {
17. if ( ! ( o instanceof Person) ) return false;
18. Person p = (Person) o;
19. return p.name.equals(this.name);
20. }
21. }
Which statement is true?
A. Compilation fails because the hashCode method is not overridden.
B. A HashSet could contain multiple Person objects with the same name.
C. All Person objects will have the same hash code because the hashCode method is not overridden.
D. If a HashSet contains more than one Person object with name="Fred", then removing another Person,
also with name="Fred", will remove
them all.
Answer: B

Person类并没有重写hashCode()方法,所以调用Object的hashCode()方法,返回的是存储地址,这些不同的Person对象的存储地址当然不同。

HashSet在将对象存入的时候,首先比较的是hashCode,如果hashCode返回值不一样,那两个元素肯定不同,允许添加新元素。

HashSet的add方法如下:

public boolean add(E e) {
        return map.put(e, PRESENT)==null;
    }

这里PERSENT是个Object类型的空对象,也就是说HashSet类利用了HashMap的put方法~~两者的原理相同。这里的e相当于HashMap中的key~~


QUESTION 217
Given:
3. import java.util.*;
4. public class Hancock {
5. // insert code here
6. list.add("foo");
7. }
8. }
Which two code fragments, inserted independently at line 5, will compile without warnings? (Choose two.)
A. public void addStrings(List list) {
B. public void addStrings(List<String> list) {
C. public void addStrings(List<? super String> list) {
D. public void addStrings(List<? extends String> list) {
Answer: BC

PECS法则,这里的list是消费者,用super~~



QUESTION 218
Given:
1. public class Threads4 {
2. public static void main (String[] args) {
3. new Threads4().go();
4. }
5. public void go() {
6. Runnable r = new Runnable() {
7. public void run() {
8. System.out.print("foo");
9. }
10. };
11. Thread t = new Thread(r);
12. t.start();
13. t.start();
14. }
15. }
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. The code executes normally and prints "foo".
D. The code executes normally, but nothing is printed.
Answer: B

两次start了~~illegalStateException



QUESTION 219
Given:
1. public class TestOne {
2. public static void main (String[] args) throws Exception {
3. Thread.sleep(3000);
4. System.out.println("sleep");
5. }
6. }
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. The code executes normally and prints "sleep".
D. The code executes normally, but nothing is printed.
Answer: C



:

QUESTION 220
Given:
1. public class TestSeven extends Thread {
2. private static int x;
3. public synchronized void doThings() {
4. int current = x;
5. current++;
6. x = current;
7. }
8. public void run() {
9. doThings();
10. }
11.}
Which statement is true?
A. Compilation fails.
B. An exception is thrown at runtime.
C. Synchronizing the run() method would make the class thread-safe.
D. The data in variable "x" are protected from concurrent access problems.
E. Declaring the doThings() method as static would make the class thread-safe.
F. Wrapping the statements within doThings() in a synchronized(new Object()) { } block would make the
class thread-safe.

Answer: E

这里注意x变量是静态的,和doThings()函数是非静态的。

线程安全的问题,如果什么都不做,那么这里doThings()函数可以由不同的TestSeven对象调用来更改x,这样就不能如愿的增加x的值了,就不是线程安全的了。

可以public static synchronized void doThings(),把doThings()函数也变成类级别的,使用类锁;

也可以把x改成private int x,把x弄成对象级别的,x和doThings()函数都是用对象锁。

引用其他人的话:普通同步方法锁定的是与 this 相关联的监视器;而静态同步方法则是与包含该方法的类所对应的 Class 对象相关联的监视器;


QUESTION 221
Which two code fragments will execute the method doStuff() in a separate thread? (Choose two.)
A. new Thread() {
public void run() { doStuff(); }
};
B. new Thread() {
public void start() { doStuff(); }
};
C. new Thread() {
public void start() { doStuff(); }
}.run();
D. new Thread() {
public void run() { doStuff(); }
}.start();
E. new Thread(new Runnable() {
public void run() { doStuff(); }
}).run();
F. new Thread(new Runnable() {
public void run() { doStuff(); }
}).start();
Answer: DF

run()方法只能还是在主线程上执行,而start()方法则是新建一个子线程去执行~~



QUESTION 222
Given:
11. public static void main(String[] args) {
12. Object obj = new int[] { 1, 2, 3 };
13. int[] someArray = (int[])obj;
14. for (int i : someArray) System.out.print(i + " ");
15. }
What is the result?
A. 1 2 3
B. Compilation fails because of an error in line 12.
C. Compilation fails because of an error in line 13.
D. Compilation fails because of an error in line 14.
E. A ClassCastException is thrown at runtime.
Answer: A



QUESTION 224
Given:
11. public static void parse(String str) {
12. try {
13. float f = Float.parseFloat(str);
14. } catch (NumberFormatException nfe) {
15. f = 0;
16. } finally {
17. System.out.println(f);
18. }
19. }
20. public static void main(String[] args) {
21. parse("invalid");
22. }
What is the result?
A. 0.0
B. Compilation fails.
C. A ParseException is thrown by the parse method at runtime.
D. A NumberFormatException is thrown by the parse method at runtime.
Answer: B

第十五行的f,未声明的变量~~13行的f作用域仅在try的{}内部~~


QUESTION 226
Given:
1. interface TestA { String toString(); }
2. public class Test {
3. public static void main(String[] args) {
4. System.out.println(new TestA() {
5. public String toString() { return "test"; }
6. });
7. }
8. }
What is the result?
A. test
B. null
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 1.
E. Compilation fails because of an error in line 4.
F. Compilation fails because of an error in line 5.
Answer: A

很简单的局部内部类~~



QUESTION 227
Given:
11. public interface A { public void m1(); }
12.
13. class B implements A { }
14. class C implements A { public void m1() { } }
15. class D implements A { public void m1(int x) { } }
16. abstract class E implements A { }
17. abstract class F implements A { public void m1() { } }
18. abstract class G implements A { public void m1(int x) { } }
What is the result?
A. Compilation succeeds.
B. Exactly one class does NOT compile.
C. Exactly two classes do NOT compile.
D. Exactly four classes do NOT compile.
E. Exactly three classes do NOT compile.
Answer: C

B类和D类错了;

而abstract类不用实现implementations的类~~交给它的子类实现就OK~~



QUESTION 228
Given:
21. abstract class C1 {
22. public C1() { System.out.print(1); }
23. }
24. class C2 extends C1 {
25. public C2() { System.out.print(2); }
26. }
27. class C3 extends C2 {
28. public C3() { System.out.println(3); }
29. }
30. public class Ctest {
31. public static void main(String[] a) { new C3(); }
32. }
What is the result?
A. 3
B. 23
C. 32
D. 123
E. 321
F. Compilation fails.
G. An exception is thrown at runtime.
Answer: D

子类的构造器默认要调用超类的无参数的构造器~~



QUESTION 229

Click the Exhibit button. What is the result?

OCJP(1Z0-851) 模拟题分析(七)_第1张图片

A. 4321
B. 0000
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 18.
Answer: D

构造函数的问题~~Person类没有默认构造函数



QUESTION 230
Given:
10. class One {
11. public One foo() { return this; }
12. }
13. class Two extends One {
14. public One foo() { return this; }
15. }
16. class Three extends Two {
17. // insert method here
18. }
Which two methods, inserted individually, correctly complete the Three class? (Choose two.)
A. public void foo() {}
B. public int foo() { return 3; }
C. public Two foo() { return this; }
D. public One foo() { return this; }
E. public Object foo() { return this; }
Answer: CD

重写一个方法的时候,返回类型可以是原来的One类,也可以使One类的子类~~


你可能感兴趣的:(java,java,java,java)