一个朋友准备去考OCP Java认证,即原来的SCJP。心血来潮也想测测自己什么水平。找了本McGraw.Hill.OCP.Java.SE.6.Programmer.Practice.Exams,开盘就是两套自测题。14个题目,给了42分钟,按书中说法是过了8个就可以去考了。掐上秒表,开工了。等我做完,一看时间才10分钟,不由有些得意。也没再检查,直接对起答案,结果仅对了7个。虽然懊恼,但总得搞清楚自己错在哪里。
1. 问下面这段程序的输出结果?
public class Bunnies {
static int count = 0;
Bunnies() {
while (count < 10)
new Bunnies(++count);
}
Bunnies(int x) {
super();
}
public static void main(String[] args) {
new Bunnies();
new Bunnies(count);
System.out.println(count++);
}
}
A. 9
B. 10
C. 11
d. 12
E. Compilation fails.
F. An exception is thrown at runtime.
再一细看,没把我给气死。明明算出来是10,却选了C,又是粗心大意。这题就是考自增自减嘛,++放在变量前与变量后的区别。另外就是在构造方法里是可以用new关键字的。
2. 问下面这段程序的输出结果?
public class Twine {
public static void main(String[] args) {
String s = "";
StringBuffer sb1 = new StringBuffer("hi");
StringBuffer sb2 = new StringBuffer("hi");
StringBuffer sb3 = new StringBuffer(sb2);
StringBuffer sb4 = sb3;
if (sb1.equals(sb2)) s += "1 ";
if (sb2.equals(sb3)) s += "2 ";
if (sb3.equals(sb4)) s += "3 ";
String s2 = "hi";
String s3 = "hi";
String s4 = s3;
if (s2.equals(s3)) s += "4 ";
if (s3.equals(s4)) s += "5 ";
System.out.println(s);
}
}
A. 1 3
B. 1 5
C. 1 2 3
D. 1 4 5
E. 3 4 5
F. 1 3 4 5
G. 1 2 3 4 5
H. Compilation fails.
StringBuffer 并没有重载equals方法,不要想当然的以为比较的是字符串的值。正解:E。
3. 下面哪些是正确的?
A. All classes of Exception extend Error.
B. All classes of Error extend Exception.
C. All Errors must be handled or declared.
D. All classes of Exception extend Throwable.
E. All Throwables must be handled or declared.
F. All Exceptions must be handled or declared.
G. RuntimeExceptions need never be handled or declared.
这题错的实在有些不应该。我咋就选成FG了呢?这两个答案明显是矛盾的啊。F说所有的异常都必须被处理,G却讲运行时异常是个例外。作者给42分钟还是挺有道理的,仔细检查是很有必要的。正解:DG。
4. 问运行结果:java Birthdays Draumur?
public class Birthdays {
public static void main(String[] args) {
Map<Friends, String> hm = new HashMap<Friends, String>();
hm.put(new Friends("Charis"), "Summer 2009");
hm.put(new Friends("Draumur"), "Spring 2002");
Friends f = new Friends(args[0]);
System.out.println(hm.get(f));
}
}
class Friends {
String name;
Friends(String n) {
name = n;
}
}
A. null
B. Draumur
C. Spring 2002
D. Compilation fails.
E. The output is unpredictable.
F. An exception is thrown at runtime.
G. Friends@XXXX (where XXXX is a representation of a hashcode)
再看此题时,猛然想起Think In Java里提到过,要作为HashMap的Key必须重载equals()和hashCode()方法,HashMap基于它们来判断两个对象是否相等。这个Friends 没有重载equals()和hashCode(),因而直接以对象的引用作为Key,而不是Name,当然用hm.get(f)就什么也得不到啦。正解:A。
5. 下面哪些是正确的?
A. Compilation succeeds.
B. Compilation fails due to an error on line 6.
C. Compilation fails due to an error on line 7.
D. Compilation fails due to an error on line 8.
E. Compilation fails due to an error on line 9.
F. Compilation fails due to an error on line 10.
G. Compilation fails due to an error on line 11.
这题主要考泛型不支持多态。正解:BDEFG。
6. 下面哪行代码插入打下图42行处可以正常编译运行?
37. boolean b = false;
38. int i = 7;
39. double d = 1.23;
40. float f = 4.56f;
41.
42. // insert code here
A. System.out.printf(" %b", b);
B. System.out.printf(" %i", i);
C. System.out.format(" %d", d);
D. System.out.format(" %d", i);
E. System.out.format(" %f", f);
考的是格式化参数,忘却了就记忆一下http://hi.baidu.com/giml/blog/item/c6b1d0fa4a5bded9b48f31dd.html 。
正解:ADE。
7. 下面哪个是正确的?
public class MyPancake implements Pancake {
public static void main(String[] args) {
List<String> x = new ArrayList<String>();
x.add("3");
x.add("7");
x.add("5");
List<String> y = new MyPancake().doStuff(x);
y.add("1");
System.out.println(x);
}
List<String> doStuff(List<String> z) {
z.add("9");
return z;
}
}
interface Pancake {
List<String> doStuff(List<String> s);
}
A. [3, 7, 5]
B. [3, 7, 5, 9]
C. [3, 7, 5, 9, 1]
D. Compilation fails.
E. An exception is thrown at runtime.
这题设置了一个陷阱,接口里的方法默认都是Public的,子类实现后,访问控制权限只能放大不能缩小,所以要在doStuff方法声明处加上Public。正解:D。
看看自己犯的错误,除了第五个和第六个,确实不是很清楚,其他都是可以避免的。相关源代码:
http://javaforge.com/svn/RoaringTiger/trunk/OCP 。
转载于:https://blog.51cto.com/lovespss/534047