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方法如下:

[java] view plaincopy

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

这里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?

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类的子类~~

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

时间: 2024-09-28 10:52:26

OCJP(1Z0-851) 模拟题分析(七)的相关文章

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

Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有疏漏,还请大家对我的分析提出质疑. QUESTION 235Given:5. class Payload {6. private int weight;7. public Payload (int w) { weight = w; }8. public void setWeight(int w) {

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

Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有疏漏,还请大家对我的分析提出质疑. QUESTION 134 Given:11. class Snoochy {12. Boochy booch;13. public Snoochy() { booch = new Boochy(this); }14. }15.16. class Boochy {1

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

Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有疏漏,还请大家对我的分析提出质疑. QUESTION 61Given:1. public class TestString1 {2. public static void main(String[] args) { 3. String str = "420";4. str += 42;5.

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

Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有疏漏,还请大家对我的分析提出质疑. QUESTION 261Given:3. class Employee {4. String name; double baseSalary;5. Employee(String name, double baseSalary) {6. this.name = n

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

Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有疏漏,还请大家对我的分析提出质疑. QUESTION 167Given:1. import java.util.*;2. public class WrappedString {3. private String s;4. public WrappedString(String s) { this.

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

Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有疏漏,还请大家对我的分析提出质疑. QUESTION 31 Given:1. interface A { public void aMethod(); }2. interface B { public void bMethod(); }3. interface C extends A,B { pub

阿里云云计算认证ACP模拟考试练习题第6套模拟题分享(共10套)

阿里云认证考试包含ACA.ACP.ACE三种认证类型,报名考试最多的是ACP认证考试,本人整理了100道全真阿里云ACP认证考试模拟试题,适合需要参加阿里云ACP认证考试的人复习,模拟练习.此为第6套模拟题分享. 阿里云云计算认证ACP模拟考试练习题6 认证级别 云计算 大数据 云安全 中间件 助理工程师(ACA) 云计算助理工程师认证报名入口 大数据助理工程师认证报名入口 云安全助理工程师认证报名入口 专业工程师(ACP) 云计算工程师认证报名入口 大数据工程师认证报名入口 大数据分析师认证报

一道模拟题

问题:把英文单词表示的数字转换为阿拉伯数字,要求数字不超过整形范围,数字形如abc,def,hrg. 第一行表示有几组数据,第二行输入英文. 输出:相应的阿拉伯数字. 例如:input: 3 eleven one hundred and two output: 11 102 分析:要注意百万和千要断位,还有要从高位往低位查找,注意分情况讨论. 1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 st

Gym 100625C 密文匹配-模拟题-(map)

题意:已知n个明文和一个密文,推出可能的匹配关系,然后输出字符串ss的密文. 分析: 一个模拟题,当时想偏了,还想着要同一字母可能在任意位置,然后要记录每个字母的位置,找密文的相应位置必须是同一字母,balabala的,不知道什么鬼. 其实就是简单的对应关系,不用管位置啥的,只管同一字母对应的密文是一样的就行了.26个字母,枚举一遍就是了.对应关系匹配啥的用map是最好不过的了.小tirck是,如果已知25个字母,那么剩下的一个也就知道了. 代码: #include<iostream> #in