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 {
17. Snoochy snooch;
18. public Boochy(Snoochy s) { snooch = s; }
19. } And the statements:
21. public static void main(String[] args) {
22. Snoochy snoog = new Snoochy();
23. snoog = null;
24. // more code here
25. }
Which statement is true about the objects referenced by snoog, snooch, and booch immediately after line
23 executes?
A. None of these objects are eligible for garbage collection.
B. Only the object referenced by booch is eligible for garbage collection.
C. Only the object referenced by snoog is eligible for garbage collection.
D. Only the object referenced by snooch is eligible for garbage collection.
E. The objects referenced by snooch and booch are eligible for garbage collection.
Answer: E

23行结束,snooch 和booch都没引用任何对象,可以被垃圾回收器回收。


QUESTION 135
Given:
3. public class Batman {
4. int squares = 81;
5. public static void main(String[] args) {
6. new Batman().go();
7. }
8. void go() {
9. incr(++squares);
10. System.out.println(squares);
11. }
12. void incr(int squares) { squares += 10; }
13. }
What is the result?
A. 81
B. 82
C. 91
D. 92
E. Compilation fails.
F. An exception is thrown at runtime.
Answer: B

一定要记得java的参数传递全部都是引用~~不变哦~~


QUESTION 136
Given classes defined in two different files:
1. package util;
2. public class BitUtils {
3. private static void process(byte[] b) {}
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. app.BitUtils.process(bytes);
D. util.BitUtils.process(bytes);
E. import util.BitUtils.*; process(bytes);
F. SomeApp cannot use the process method in BitUtils.
Answer: F

process的访问修饰符不允许这样做


QUESTION 137
A UNIX user named Bob wants to replace his chess program with a new one, but he is not sure where the
old one is installed. Bob is currently able to run a Java chess program starting from his home directory /
home/bob using the command: java -classpath /test:/home/bob/downloads/*.jar games.Chess Bob's
CLASSPATH is set (at login time) to:
/usr/lib:/home/bob/classes:/opt/java/lib:/opt/java/lib/*.jar What is a possible location for the Chess.class file?
A. /test/Chess.class
B. /home/bob/Chess.class
C. /test/games/Chess.class
D. /usr/lib/games/Chess.class
E. /home/bob/games/Chess.class
F. inside jarfile /opt/java/lib/Games.jar (with a correct manifest)
G. inside jarfile /home/bob/downloads/Games.jar (with a correct manifest)
Answer: C

围棋程序问题,重复的题目~~考察路径classpath的。/home/bob/downloads/*.jar的写法无效~~


QUESTION 138

Click the Exhibit button. What is the output of the program shown in the exhibit?



A. 300-100-100-100-100
B. 300-300-100-100-100
C. 300-300-300-100-100
D. 300-300-300-300-100
Answer: B

参数传递传的是引用~~


QUESTION 139

Given the following directory structure: 

bigProject

 |--source

 | |--Utils.java

 |

 |--classes

 |-- 

And the following

command line invocation: javac -d classes source/Utils.java Assume the current directory is bigProject,
what is the result?
A. If the compile is successful, Utils.class is added to the source directory.
B. The compiler returns an invalid flag error.
C. If the compile is successful, Utils.class is added to the classes directory.
D. If the compile is successful, Utils.class is added to the bigProject directory.
Answer: C

-d classes 是把生成的Utils.class文件放到classes目录中的Unix命令~~


QUESTION 140
Given:
3. interface Fish { }
4. class Perch implements Fish { }
5. class Walleye extends Perch { }
6. class Bluegill { }
7. public class Fisherman {
8. public static void main(String[] args) {
9. Fish f = new Walleye();
10. Walleye w = new Walleye();
11. Bluegill b = new Bluegill();
12. if(f instanceof Perch) System.out.print("f-p ");
13. if(w instanceof Fish) System.out.print("w-f ");
14. if(b instanceof Fish) System.out.print("b-f ");
15. }
16. }
What is the result?
A. w-f
B. f-p w-f
C. w-f b-f
D. f-p w-f b-f
E. Compilation fails.
F. An exception is thrown at runtime.
Answer: B

a instanceof  b,指的是a对象是否是b类或其子类的一个实例对象,或者说a对象是否是实现了b接口的一个实例对象。

f对象是Perch子类的实例对象,w对象实现了Fish接口,b对象没有实现Fish接口。


QUESTION 141
Given:
1. public class Breaker2 {
2. static String o = "";
3. public static void main(String[] args) {
4. z:
5. for(int x = 2; x < 7; x++) {
6. if(x==3) continue;
7. if(x==5) break z;
8. o = o + x;
9. }
10. System.out.println(o);
11. }
12. }
What is the result?
A. 2
B. 24
C. 234
D. 246
E. 2346
F. Compilation fails.
Answer: B



QUESTION 142
Given:
11. public void testIfA() {
12. if (testIfB("True")) {
13. System.out.println("True");
14. } else {
15. System.out.println("Not true");
16. }
17. }
18. public Boolean testIfB(String str) {
19. return Boolean.valueOf(str);
20. }
What is the result when method testIfA is invoked?
A. True
B. Not true
C. An exception is thrown at runtime.
D. Compilation fails because of an error at line 12.
E. Compilation fails because of an error at line 19.
Answer: A

valueOf方法: The Boolean returned represents a true value if the string argument is not null and is equal, ignoring case, to the string "true".


QUESTION 143
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



QUESTION 144
Given:
31. // some code here
32. try {
33. // some code here
34. } catch (SomeException se) {
35. // some code here
36. } finally {
37. // some code here
38. }
Under which three circumstances will the code on line 37 be executed? (Choose three.)
A. The instance gets garbage collected.
B. The code on line 33 throws an exception.
C. The code on line 35 throws an exception.
D. The code on line 31 throws an exception.
E. The code on line 33 executes successfully.
Answer: BCE

finally有几种情况可以执行:正常执行,发生异常被catch块处理,当然,在catch块内发生的异常也行。


QUESTION 145
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



QUESTION 146
Given:
11. static void test() {
12. try {
13. String x = null;
14. System.out.print(x.toString() + " ");
15. }
16. finally { System.out.print("finally "); }
17. }
18. public static void main(String[] args) {
19. try { test(); }
20. catch (Exception ex) { System.out.print("exception "); }
21. }
What is the result?
A. null
B. finally
C. null finally
D. Compilation fails.
E. finally exception
Answer: E

我写了一点测试了一下,发现如果字符串为空(null),那么toString()函数会抛出NullPointerException异常,而不去转换直接输出的时候不会。

package com.xujin;

public class Test {
	public static void main(String[] args) {


		String s = null;
		System.out.println(s);//null
		//System.out.println(s.toString());//NullPointerException
		System.out.println(s + "hello~~");//nullhello~~
			
	}	
}



QUESTION 147
Given:
10. interface Foo {}
11. class Alpha implements Foo {}
12. class Beta extends Alpha {}
13. class Delta extends Beta {
14. public static void main( String[] args ) {
15. Beta x = new Beta();
16. // insert code here
17. }
18. }
Which code, inserted at line 16, will cause a java.lang.ClassCastException?
A. Alpha a = x;
B. Foo f = (Delta)x;
C. Foo f = (Alpha)x;
D. Beta b = (Beta)(Alpha)x;
Answer: B

x是Beta类的实例对象,不能强制转换成子类Delta类的对象。

:

QUESTION 148
Given:
33. try {
34. // some code here
35. } catch (NullPointerException e1) {
36. System.out.print("a");
37. } catch (Exception e2) {
38. System.out.print("b");
39. } finally {
40. System.out.print("c");
41. }
If some sort of exception is thrown at line 34, which output is possible?
A. a
B. b
C. c
D. ac
E. abc
Answer: D

要么ac,要么bc


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

case default:错了,应该是default:


QUESTION 150

Click the Exhibit button. Given: ClassA a = new ClassA(); a.methodA(); What is the result?



A. Compilation fails.
B. ClassC is displayed.
C. The code runs with no output.
D. An exception is thrown at runtime.
Answer: D

问题出在第24行,classC还没有new一个,就直接用函数getValue(),NullPointerException会被抛出


QUESTION 151
Given:
11. static void test() throws RuntimeException {
12. try {
13. System.out.print("test ");
14. throw new RuntimeException();
15. }
16. catch (Exception ex) { System.out.print("exception "); }
17. }
18. public static void main(String[] args) {
19. try { test(); }
20. catch (RuntimeException ex) { System.out.print("runtime "); }
21. System.out.print("end ");
22. }
What is the result?
A. test end
B. Compilation fails.
C. test runtime end
D. test exception end
E. A Throwable is thrown by main at runtime.
Answer: D

19. try { test(); },首先输出13. System.out.print("test ");然后会抛出throw new RuntimeException();,这个异常16行的catch可以处理,,输出exception ,然后执行21行~~


QUESTION 152
Given:
1. public class Plant {
2. private String name;
3. public Plant(String name) { this.name = name; }
4. public String getName() { return name; }
5. }
1. public class Tree extends Plant {
2. public void growFruit() { }
3. public void dropLeaves() { }
4. }
Which statement is true?
A. The code will compile without changes.
B. The code will compile if public Tree() { Plant(); } is added to the Tree class.
C. The code will compile if public Plant() { Tree(); } is added to the Plant class.
D. The code will compile if public Plant() { this("fern"); } is added to the Plant class.
E. The code will compile if public Plant() { Plant("fern"); } is added to the Plant class.
Answer: D

Plant 类缺少无参数类型的构造器。


QUESTION 153
Given:
10. class Line {
11. public static class Point {}
12. }
13.
14. class Triangle {
15. // insert code here
16. }
Which code, inserted at line 15, creates an instance of the Point class defined in Line?
A. Point p = new Point();
B. Line.Point p = new Line.Point();
C. The Point class cannot be instatiated at line 15.
D. Line l = new Line() ; l.Point p = new l.Point();
Answer: B



QUESTION 154
Given:
10. class Nav{
11. public enum Direction { NORTH, SOUTH, EAST, WEST }
12. }
13. public class Sprite{
14. // insert code here
15. }
Which code, inserted at line 14, allows the Sprite class to compile?
A. Direction d = NORTH;
B. Nav.Direction d = NORTH;
C. Direction d = Direction.NORTH;
D. Nav.Direction d = Nav.Direction.NORTH;
Answer: D



QUESTION 155
Given:
10. interface Data { public void load(); }
11. abstract class Info { public abstract void load(); }
Which class correctly uses the Data interface and Info class?
A. public class Employee extends Info implements Data { public void load() { /*do something*/ }
}
B. public class Employee implements Info extends Data { public void load() { /*do something*/ }
}
C. public class Employee extends Info implements Data { public void load(){ /*do something*/ }
public void Info.load(){ /*do something*/ }
}
D. public class Employee implements Info extends Data { public void Data.load(){ /*do something*/ }
public void load(){ /*do something*/ }
}
E. public class Employee implements Info extends Data { public void load(){ /*do something*/ }
public void Info.load(){ /*do something*/ }
}
F. public class Employee extends Info implements Data{
public void Data.load() { /*do something*/ }
public void Info.load() { /*do something*/ }
}
Answer: A



QUESTION 156
Given:
11. public class Rainbow {
12. public enum MyColor {
13. RED(0xff0000), GREEN(0x00ff00), BLUE(0x0000ff);
14. private final int rgb;
15. MyColor(int rgb) { this.rgb = rgb; }
16. public int getRGB() { return rgb; }
17. };
18. public static void main(String[] args) {
19. // insert code here
20. }
21. }
Which code fragment, inserted at line 19, allows the Rainbow class to compile?
A. MyColor skyColor = BLUE;
B. MyColor treeColor = MyColor.GREEN;
C. if(RED.getRGB() < BLUE.getRGB()) { }
D. Compilation fails due to other error(s) in the code.
E. MyColor purple = new MyColor(0xff00ff);
F. MyColor purple = MyColor.BLUE + MyColor.RED;
Answer: B

枚举常量不能实例化,只能在定义的时候制定。所以没有E这种情况。


QUESTION 157
Given:
10. class One {
11. void foo() { }
12. }
13. class Two extends One {
14. //insert method here
15. }
Which three methods, inserted individually at line 14, will correctly complete class Two? (Choose three.)
A. int foo() { /* more code here */ }
B. void foo() { /* more code here */ }
C. public void foo() { /* more code here */ }
D. private void foo() { /* more code here */ }
E. protected void foo() { /* more code here */ }
Answer: BCE

重写~~



QUESTION 159
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类没有实现接口A,C类无错,D类没有实现函数void m1();,而是重载了~~abstract抽象类可以不去实现接口。

感谢 ljt0000mf提出错来~~


QUESTION 160
Given:
1. class Alligator {
2. public static void main(String[] args) {
3. int []x[] = {{1,2}, {3,4,5}, {6,7,8,9}};
4. int [][]y = x;
5. System.out.println(y[2][1]);
6. }
7. }
What is the result?
A. 2
B. 3
C. 4
D. 6
E. 7
F. Compilation fails.
Answer: E


QUESTION 162
Given:
12. NumberFormat nf = NumberFormat.getInstance();
13. nf.setMaximumFractionDigits(4);
14. nf.setMinimumFractionDigits(2);
15. String a = nf.format(3.1415926);
16. String b = nf.format(2);
Which two statements are true about the result if the default locale is Locale.US? (Choose two.)
A. The value of b is 2.
B. The value of a is 3.14.
C. The value of b is 2.00.
D. The value of a is 3.141.
E. The value of a is 3.1415.
F. The value of a is 3.1416.
G. The value of b is 2.0000.
Answer: CF

setMaximumFractionDigits是设置小数点后的数字的最max个数~~

setMinimumFractionDigits是设置小数点后的数字的最min个数~~

setMinimumIntegerDigits是设置小数点前的数字的最min个数~~


QUESTION 163
Given:
11. String test = "a1b2c3";
12. String[] tokens = test.split("\\d");
13. for(String s: tokens) System.out.print(s + " ");
What is the result?
A. a b c
B. 1 2 3
C. a1b2c3
D. a1 b2 c3
E. Compilation fails.
F. The code runs with no output.
G. An exception is thrown at runtime.
Answer: A

d代表digital数字~~


QUESTION 164
Given:
11. class Converter {
12. public static void main(String[] args) {
13. Integer i = args[0];
14. int j = 12;
15. System.out.println("It is " + (j==i) + " that j==i.");
16. }
17. }
What is the result when the programmer attempts to compile the code and run it with the command java
Converter 12?
A. It is true that j==i.
B. It is false that j==i.
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 13.
Answer: D

13. Integer i = args[0];,无法将String类型的对象付给Integer,应该是13. Integer i = Integer.parseInt( args[0]);


QUESTION 165
Given:
1. public class BuildStuff {
2. public static void main(String[] args) {
3. Boolean test = new Boolean(true);
4. Integer x = 343;
5. Integer y = new BuildStuff().go(test, x);
6. System.out.println(y);
7. }
8. int go(Boolean b, int i) {
9. if(b) return (i/7);
10. return (i/49);
11. }
12. }
What is the result?
A. 7
B. 49
C. 343
D. Compilation fails.
E. An exception is thrown at runtime.
Answer: B

b为true,执行第九行,343/7 是 49~~


QUESTION 166
Given:
12. String csv = "Sue,5,true,3";
13. Scanner scanner = new Scanner( csv );
14. scanner.useDelimiter(",");
15. int age = scanner.nextInt();
What is the result?
A. Compilation fails.
B. After line 15, the value of age is 5.
C. After line 15, the value of age is 3.
D. An exception is thrown at runtime.
Answer: D

nextInt读取的下一个是Sue不是Int类型的,抛出个异常~~



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