Exam : 1Z0-851

Java Standard Edition 6 Programmer Certified Professional Exam

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

 

QUESTION 101

Given:
12. import java.util.*;
13. public class Explorer1 {
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(609);
21. System.out.println(s + " " + subs);
22. }
23. }
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. [608, 609, 610, 612] [608, 610]
D. [608, 609, 610, 612] [608, 609, 610]
E. [606, 608, 609, 610, 612] [608, 610]
F. [606, 608, 609, 610, 612] [608, 609, 610]
Answer: F

重复的题目,subset方法返回view,而不是copy

QUESTION 102
Given:
23. Object [] myObjects = {
24. new Integer(12),
25. new String("foo"),
26. new Integer(5),
27. new Boolean(true)
28. };
29. Arrays.sort(myObjects);
30. for(int i=0; i<myObjects.length; i++) {
31. System.out.print(myObjects[i].toString());
32. System.out.print(" ");
33. }
What is the result?
A. Compilation fails due to an error in line 23.
B. Compilation fails due to an error in line 29.
C. A ClassCastException occurs in line 29.
D. A ClassCastException occurs in line 31.
E. The value of all four objects prints in natural order.
Answer: C

sort方法比较的必须是可以转化成相同的而且实现了Comparable借口的相同对象。

QUESTION 103
Given:
1. public class Donkey {
2. public static void main(String[] args) {
3. boolean assertsOn = false;
4. assert (assertsOn) : assertsOn = true;
5. if(assertsOn) {
6. System.out.println("assert is on");
7. }
8. }
9. }
If class Donkey is invoked twice, the first time without assertions enabled, and the second time with
assertions enabled, what are the results?
A. no output
B. no output
assert is on
C. assert is on
D. no output
An AssertionError is thrown.
E. assert is on
An AssertionError is thrown.
Answer: D

考察assert,如果是断言有效的情况,即assert(false),就会抛出一个AssertionError

QUESTION 104
Given:
11. Float pi = new Float(3.14f);
12. if (pi > 3) {
13. System.out.print("pi is bigger than 3. ");
14. }
15. else {
16. System.out.print("pi is not bigger than 3. ");
17. }
18. finally {
19. System.out.println("Have a nice day.");
20. }
What is the result?
A. Compilation fails.
B. pi is bigger than 3.
C. An exception occurs at runtime.
D. pi is bigger than 3. Have a nice day.
E. pi is not bigger than 3. Have a nice day.
Answer: A

finally不是用在这种地方,仅仅在try/catch/finally内使用。

QUESTION 105
Given:
11. public static void main(String[] args) {
12. try {
13. args = null;
14. args[0] = "test";
15. System.out.println(args[0]);
16. } catch (Exception ex) {
17. System.out.println("Exception");
18. } catch (NullPointerException npe) {
19. System.out.println("NullPointerException");
20. }
21. }
What is the result?
A. test
B. Exception
C. Compilation fails.
D. NullPointerException
Answer: C

NullPointerException 是Exception 的子类,在编译器中会有无法达到(not available)的警告。

QUESTION 106
Given:
22. public void go() {
23. String o = "";
24. z:
25. for(int x = 0; x < 3; x++) {
26. for(int y = 0; y < 2; y++) {
27. if(x==1) break;
28. if(x==2 && y==1) break z;
29. o = o + x + y;
30. }
31. }
32. System.out.println(o);
33. }
What is the result when the go() method is invoked?
A. 00
B. 0001
C. 000120
D. 00012021
E. Compilation fails.
F. An exception is thrown at runtime.
Answer: C

考察break,当x=0,y=0,o=00;

当x=0,y=1,o=0001;

当x=1,y=0,break掉内循环,o=0001;

当x=2,y=0,o=000120;

当x=2,y=1,break掉断点处的外循环,o=000120;

QUESTION 107
Given:
12. public class Test {
13. public enum Dogs {collie, harrier};
14. public static void main(String [] args) {
15. Dogs myDog = Dogs.collie;
16. switch (myDog) {
17. case collie:
18. System.out.print("collie ");
19. case harrier:
20. System.out.print("harrier ");
21. }
22. }
23. }
What is the result?
A. collie
B. harrier
C. Compilation fails.
D. collie harrier
E. An exception is thrown at runtime.
Answer: D

switch的参数可以是枚举常量,没有break,从17行一直往下执行。

QUESTION 108

Click the Exhibit button. Given:

31. public void method() {
32. A a = new A();
33. a.method1();
34. }
Which statement is true if a TestException is thrown on line 3 of class B?
A. Line 33 must be called within a try block.
B. The exception thrown by method1 in class A is not required to be caught.
C. The method declared on line 31 must be declared to throw a RuntimeException.
D. On line 5 of class A, the call to method2 of class B does not need to be placed in a try/catch block.
Answer: B

由于抛出的是一个RuntimeException,不需要处理,所以不需要try块,也不需要throws,B对。

而A类中抛出的是一个继承自Exception的TestException,必须处理。

QUESTION 109
Given:
1. public class Boxer1{
2. Integer i;
3. int x;
4. public Boxer1(int y) {
5. x = i+y;
6. System.out.println(x);
7. }
8. public static void main(String[] args) {
9. new Boxer1(new Integer(4));
10. }
11. }
What is the result?
A. The value "4" is printed at the command line.
B. Compilation fails because of an error in line 5.
C. Compilation fails because of an error in line 9.
D. A NullPointerException occurs at runtime.
E. A NumberFormatException occurs at runtime.
F. An IllegalStateException occurs at runtime.
Answer: D

第五行5. x = i+y;,还没有为i赋值(此时i为null),无法运算。

QUESTION 110
Given:
11. static class A {
12. void process() throws Exception { throw new Exception(); }
13. }
14. static class B extends A {
15. void process() { System.out.println("B"); }
16. }
17. public static void main(String[] args) {
18. new B().process();
19. }
What is the result?
A. B
B. The code runs with no output.
C. Compilation fails because of an error in line 12.
D. Compilation fails because of an error in line 15.
E. Compilation fails because of an error in line 18.
Answer: A

QUESTION 111
Given:
1. public class Venus {
2. public static void main(String[] args) {
3. int [] x = {1,2,3};
4. int y[] = {4,5,6};
5. new Venus().go(x,y);
6. }
7. void go(int[]... z) {
8. for(int[] a : z)
9. System.out.print(a[0]);
10. }
11. }
What is the result?
A. 1
B. 12
C. 14
D. 123
E. Compilation fails.
F. An exception is thrown at runtime.
Answer: C

int[]... z表示可变参数,而可变的是数组的个数。一共有两个数组,输出每个数组的第一个元素。

QUESTION 112
Given:
10. public class Foo {
11. static int[] a;
12. static { a[0]=2; }
13. public static void main( String[] args ) {}
14. }
Which exception or error will be thrown when a programmer attempts to run this code?
A. java.lang.StackOverflowError
B. java.lang.IllegalStateException
C. java.lang.ExceptionInInitializerError
D. java.lang.ArrayIndexOutOfBoundsException
Answer: C

还没new呢,就赋值。初始化错误。

QUESTION 113
Given:
11. class X { public void foo() { System.out.print("X "); } }
12.
13. public class SubB extends X {
14. public void foo() throws RuntimeException {
15. super.foo();
16. if (true) throw new RuntimeException();
17. System.out.print("B ");
18. }
19. public static void main(String[] args) {
20. new SubB().foo();
21. }
22. }
What is the result?
A. X, followed by an Exception.
B. No output, and an Exception is thrown.
C. Compilation fails due to an error on line 14.
D. Compilation fails due to an error on line 16.
E. Compilation fails due to an error on line 17.
F. X, followed by an Exception, followed by B.
Answer: A

多态性,首先是调用的SubB里面的foo()函数,而执行SubB里面的foo()函数首先就要super.foo();即调用父类的foo()函数输出X,后来就会抛出一个异常,回到调用该抛出异常的方法的地方。不会输出B

QUESTION 121
A company has a business application that provides its users with many different reports:
receivables reports, payables reports, revenue projects, and so on. The company has just purchased
some new, state-of-the-art, wireless printers, and a programmer has been assigned the task of enhancing
all of the reports to use not only the company's old printers, but the new wireless printers as well. When the
programmer starts looking into the application, the programmer discovers that because of the design of the
application, it is necessary to make changes to each report to support the new printers. Which two design
concepts most likely explain this situation? (Choose two.)
A. Inheritance
B. Low cohesion
C. Tight coupling
D. High cohesion
E. Loose coupling
F. Object immutability
Answer: BC

Low cohesion低内聚 Tight coupling高耦合:以上两个都是软件工程该避免的。

QUESTION 122
Given:
2. public class Hi {
3. void m1() { }
4. protected void() m2 { }
5. }
6. class Lois extends Hi {
7. // insert code here
8. }
Which four code fragments, inserted independently at line 7, will compile? (Choose four.)
A. public void m1() { }
B. protected void m1() { }
C. private void m1() { }
D. void m2() { }
E. public void m2() { }
F. protected void m2() { }
G. private void m2() { }
Answer: ABEF

子类重写,访问修饰符不能reduce

QUESTION 124
Given:
3. class Employee {
4. String name; double baseSalary;
5. Employee(String name, double baseSalary) {
6. this.name = name;
7. this.baseSalary = baseSalary;
8. }
9. }
10. public class SalesPerson extends Employee {
11. double commission;
12. public SalesPerson(String name, double baseSalary, double commission) {
13. // insert code here
14. }
15. }
Which two code fragments, inserted independently at line 13, will compile? (Choose two.)
A. super(name, baseSalary);
B. this.commission = commission;
C. super();
this.commission = commission;
D. this.commission = commission;
super();
E. super(name, baseSalary);
this.commission = commission;
F. this.commission = commission;
super(name, baseSalary);
G. super(name, baseSalary, commission);
Answer: AE

Employee 类仅有带参数的构造器,没有默认构造器,这要注意。所以不能出现super();而且还必须显式调用父类的带参数的构造器, super(name, baseSalary);必须出现在构造器的第一句话。

QUESTION 125
A team of programmers is reviewing a proposed API for a new utility class. After some discussion, they
realize that they can reduce the number of methods in the API without losing any functionality. If they
implement the new design, which two OO principles will they be promoting?
A. Looser coupling
B. Tighter coupling
C. Lower cohesion
D. Higher cohesion
E. Weaker encapsulation
F. Stronger encapsulation
Answer: A

内聚:一个模块内各个元素彼此结合的紧密程度
耦合:一个软件结构内不同模块之间互连程度的度量

这里减少API数量却不丢失任何功能,意思是各个模块之间的联系很小,互联程度很低,属于低耦合。

QUESTION 126
Given:
1. class ClassA {
2. public int numberOfInstances;
3. protected ClassA(int numberOfInstances) {
4. this.numberOfInstances = numberOfInstances;
5. }
6. }
7. public class ExtendedA extends ClassA {
8. private ExtendedA(int numberOfInstances) {
9. super(numberOfInstances);
10. }
11. public static void main(String[] args) {
12. ExtendedA ext = new ExtendedA(420);
13. System.out.print(ext.numberOfInstances);
14. }
15. }
Which statement is true?
A. 420 is the output.
B. An exception is thrown at runtime.
C. All constructors must be declared public.
D. Constructors CANNOT use the private modifier.
E. Constructors CANNOT use the protected modifier.
Answer: A

QUESTION 127
Given:
5. class Building { }
6. public class Barn extends Building {
7. public static void main(String[] args) {
8. Building build1 = new Building();
9. Barn barn1 = new Barn();
10. Barn barn2 = (Barn) build1;
11. Object obj1 = (Object) build1;
12. String str1 = (String) build1;
13. Building build2 = (Building) barn1;
14. }
15. }
Which is true?
A. If line 10 is removed, the compilation succeeds.
B. If line 11 is removed, the compilation succeeds.
C. If line 12 is removed, the compilation succeeds.
D. If line 13 is removed, the compilation succeeds.
E. More than one line must be removed for compilation to succeed.
Answer: C

这个强制类型转换 (String) build1;不会成功

QUESTION 128
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 129
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

t.start()是让线程t开始,已经开始了,不能再次t.start()。会抛出非法状态异常,IllegalThreadStateException。

QUESTION 130
Which two statements are true? (Choose two.)
A. It is possible for more than two threads to deadlock at once.
B. The JVM implementation guarantees that multiple threads cannot enter into a deadlocked state.
C. Deadlocked threads release once their sleep() method's sleep duration has expired.
D. Deadlocking can occur only when the wait(), notify(), and notifyAll() methods are used incorrectly.
E. It is possible for a single-threaded application to deadlock if synchronized blocks are used incorrectly.
F. If a piece of code is capable of deadlocking, you cannot eliminate the possibility of deadlocking by
inserting invocations of Thread.yield().
Answer: AF

A:超过两个线程死锁是可能发生的。

B:JVM并没有保证多个线程不进入死锁状态。

C:死锁的线程就会释放仅仅和资源有关,资源少了就会死锁,和sleep无关,sleep仅仅是让线程睡眠。

D:不对,除了这些方法,还有别的,如await,signal,signalAll。

E:单线程的应用怎么会死锁,有没有别的线程来抢资源。

F:如果一段代码可以死锁,你不能通过插入Thread.yield()消除死锁的可能性。

QUESTION 132
Given classes defined in two different files:
1. package util;
2. public class BitUtils {
3. public static void process(byte[] b) { /* more code here */ }
4. }
1. package app;
2. public class SomeApp {
3. public static void main(String[] args) {
4. byte[] bytes = new byte[256];
5. // insert code here
6. }
7. }
What is required at line 5 in class SomeApp to use the process method of BitUtils?
A. process(bytes);
B. BitUtils.process(bytes);
C. util.BitUtils.process(bytes);
D. SomeApp cannot use methods in BitUtils.
E. import util.BitUtils.*; process(bytes);
Answer: C

QUESTION 133
A developer is creating a class Book, that needs to access class Paper. The Paper class is deployed in a
JAR named myLib.jar. Which three, taken independently, will allow the developer to use the Paper class
while compiling the Book class? (Choose three.)
A. The JAR file is located at $JAVA_HOME/jre/classes/myLib.jar.
B. The JAR file is located at $JAVA_HOME/jre/lib/ext/myLib.jar..
C. The JAR file is located at /foo/myLib.jar and a classpath environment variable is set that includes /foo/
myLib.jar/Paper.class.
D. The JAR file is located at /foo/myLib.jar and a classpath environment variable is set that includes /foo/
myLib.jar.
E. The JAR file is located at /foo/myLib.jar and the Book class is compiled using javac -cp /foo/myLib.jar/
Paper Book.java.
F. The JAR file is located at /foo/myLib.jar and the Book class is compiled using javac -d /foo/myLib.jar
Book.java
G. The JAR file is located at /foo/myLib.jar and the Book class is compiled using javac -classpath /foo/
myLib.jar Book.java
Answer: BDG

OCJP(1Z0-851) 模拟题分析(四)over的更多相关文章

  1. OCJP(1Z0-851) 模拟题分析(一)11

    Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有 ...

  2. OCJP(1Z0-851) 模拟题分析(七)-->214

    Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有 ...

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

    Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有 ...

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

    Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有 ...

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

    Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有 ...

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

    Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有 ...

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

    Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有 ...

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

    Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有 ...

  9. NOIP2018提高组模拟题(四)

    能量(energy) Description ​ 有一块能量田,它的形状是 n*m的矩形,每一个格子上都有一个能量值 a[x][y] (可正可负).一块矩形田的能量定义为它的每个格子的能量值之和. ​ ...

随机推荐

  1. C/C++相对论——C++中为什么要使用异常?

    C++中为什么要使用异常? 很多人也许知道C++中的异常机制,很多人也许不知道.很多人知道C中常用的assert,也知道在编译时候指定NODEBUG来忽略它. 对于C语言,使用正常的if-else即是 ...

  2. 将Apache加入到linux系统service

    将Apache加入到linux系统service 将apache加入到linux系统服务,用service命令来控制apache的启动和停止. 本文由乌合之众瞎写http://www.cnblogs. ...

  3. PHP 文件下载流程

    前台HTML: 添加download属性,不打开download.php页面 <a style='color:blue' href='download.php' download='data/C ...

  4. VB中 ByRef与ByVal区别

    函数调用的参数传递有"值传递"和"引用传递"两种传递方式.如果采用"值传递",在函数内部改变了参数的值,主调程序的对应变量的值不会改变:如果 ...

  5. join

    一句话 join(param) 是把  array 连城一个字符串,中间用 param隔开

  6. poj 2403

    http://poj.org/problem?id=2403 题意:就是给你m个单词,以及n段对话.每一个单词都有所对应的价值.求对话中的价值总和 题解:很简单,就是用单词和价值对应起来,然后再寻找就 ...

  7. jQuery获取循环中的选中单选按钮radio的值

    1.<input type="radio" name="testradio" value="jquery获取radio的值" /> ...

  8. 搭建Solr集群的推荐方案

    之前介绍过2篇SolrCloud的部署流程,第一个是使用安装脚本的方式进行抽取安装,启动比较方便,但是会创建多个目录,感觉比较乱:第二个是官方教程上提供的方法,使用比较简单,直接释放压缩包即可,并且启 ...

  9. jQuery 调用jsonp实现与原理

    jQuery 调用jsonp实现与原理 您的评价:        收藏该经验     阅读目录 1.客户端代码 2.服务器端 通过jQuery实现JSONP 一般的ajax是不能跨域请求的,因此需要使 ...

  10. Django~urls.py--->views.py

    The 'r' in front of each regular expression string is optional but recommended. It tells Python that ...