您的位置:首页 > 编程语言 > Java开发

不看OCJP考题你永远不知道自己的JAVA基础有多差(五)

2012-07-06 11:04 295 查看
上期答案
第十六题:
当前目录是一个空目录,假设当前用户拥有对此目录读和写的权限,并执行如下代码

11.import java.io.*;

12.public class DOS {

13. public static voidmain(String[] args){

14. File dir = new File("dir");

15. dir.mkdir();

16. File f1 = new File(dir "f1.txt");

17. try {

18. f1.createNewFile();

19. } catch (IOException e) { ; }

20. File newDir = new File("newDir");

21. dir.renameTo(newDir);

22. }

23. }

下列哪种情况是正确的?

A. 编译失败

B. 当前目录下创建了一个名为“dir”的空目录

C. 当前目录下创建了一个名为“newdir”的空目录

D. 当前目录下创建了一个名为“dir”的目录,在这个目录中包含了一个名为f1.txt的文件

E. 当前目录下创建了一个名为“newdir”的目录,在这个目录中包含了一个名为f1.txt的文件

答案 E

知识点
文件操作,File类,没啥说的,基本操作而已

第十七题:

11.class Converter {

12. public static voidmain(String[] args){

13. Integer i = args[0];

14. int j = 12;

15. System.out.println("Itis " + (j==i) + " that j==i.");

16. }

17. }

当程序员试图通过执行命令“java Converter 12”来编译并运行该程序时会出现哪种结果?

A. Itis true that j==i.

B. Itis false that j==i.

C. 抛出一个异常

D. 编译失败,因为第13行存在错误

答案 D

知识点
args[0]是String型的,不能给Integer型的J赋值

第十八题:

11.String test ="Test A. Test B. Test C.";

12. //insert code here

13.String[] result =test.split(regex);

在第12行插入下列哪个正则表达式可以将test分割为"TestA" "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[ \.] +";

答案 E

知识点
正则表达式,首先“.”需要“\”来转译,之后,如果没注意看可能会看漏前两个点后面的空格,\s表示匹配任意的空格,因此答案是E。这里多说一句,正则表达式我相信是很多人的烦恼,网上我看到一篇文章介绍的很好很相信,由浅入深一步步的讲,有兴趣的可以去看看,收藏也挺好的随时备查。正则表达式谁都恶心,能一下子就学会并且记住的人只有超人,但是我个人认为,正则表达式起码比英文单词好背吧,呵呵。对了,网上那篇文章叫《正则表达式30分钟入门教程》,百度一搜就有了,这篇文章不是我写的,作者我也不认识,请大家不要怀疑我在打广告。

第十九题:
5.import java.util.Date;

6.importjava.text.DateFormat;

21.DateFormatdf;

22.Date date = newDate();

23. // 此处插入代码

24.String s =df.format(date);

在第23行填入代码使得上述代码可以被编译通过

A. df =new DateFormat();

B. df =Date.getFormat();

C. df =date.getFormat();

D. df=DateFormat.getFormat();

E. df=DateFormat.getInstance();

答案 E

知识点
DateFormat 是日期/时间格式化子类的抽象类,要格式化一个当前语言环境下的日期,可使用某个静态工厂方法:

myString =DateFormat.getDateInstance().format(myDate);

这个问题照我看来就是为了考试而出的题目,基本没有使用价值。建议大家使用DateFormat的子类SimpleDateFormat

第二十题

有一个名为Repetition的类:

1.package utils;

2

3.public class Repetition{

4. publicstatic String twice(String s) { return s + s; }

5. }

又有另一个名为Demo的类

1. // 此处插入代码

2

3.public class Demo {

4. public static void main(String[] args) {

5. System.out.println(twice("pizza"));

6. }

7. }

在类Demo的第一行插入那条代码可以打印出pizzapizza?

A.import utils.*;

B.static import utils.*;

C.importutils.Repetition.*;

D.static importutils.Repetition.*;

E.importutils.Repetition.twice();

F.import staticutils.Repetition.twice;

G.static importutils.Repetition.twice;

答案 F

知识点:
静态导入,不明白的看张孝祥老师的JAVA基础强化,里面有专门的讲解

第二十一题:

3.interface Animal { voidmakeNoise(); }

4.class Horse implementsAnimal {

5. Longweight = 1200L;

6. public void makeNoise() {System.out.println("whinny"); }

7. }

8.public class Icelandicextends Horse {

9. publicvoid makeNoise() {System.out.println("vinny"); }

10. public static voidmain(String[] args){

11. Icelandic i1 = newIcelandic();

12. Icelandic i2 = newIcelandic();

13. Icelandic i3 = new Icelandic();

14. i3 = i1; i1 = i2; i2 = null;i3 = i1;

15. }

16. }

当程序运行到第15行是,一共产生多少个可以被垃圾回收器回收的对象

A. 0

B. 1

C. 2

D. 3

E. 4

F. 6

答案 E

这个题目我也迷糊了,还是论坛上的葛奎同学回答了我的问题,特此感谢
在子类Icelandic被实例化的同时父类Horse也同样被实例化了,因此是4个
我也觉得这是唯一可行的解释,但是,问题有来了,大家都知道,JAVA中所有的类都是Object类的子类,那是不是还同时实例化了2个Object的实例,那么问题的答案就应该是6了呢?显然不是,但为什么不是呢?谁知道,JVM有很多怪异的行为,我的一位前辈大牛曾经说过“越高级的编程语言越诡异”,因此此君至今还在用C和汇编做嵌入式开发。那钱赚的,老了去了。
--------------------------------------------------------分割线-----------------------------------------------
本期问题:
第二十二题
下面的类被定义在两个不同的文件中

1. package util;

2. public classBitUtils {

3. private static void process(byte[] b) {}

4. }

1. package app;

2. public classSomeApp {

3. public static void main(String[] args) {

4. byte[] bytes = new byte[256];

5. // insert code here

6. }

7. }

在SomeApp的第五行插入那条代码可以允许主方法调用BitUtils类的process方法?

A. process(bytes);

B.BitUtils.process(bytes);

C.app.BitUtils.process(bytes);

D.util.BitUtils.process(bytes);

E. importutil.BitUtils.*; process(bytes);

F.无法实现.

第二十三题

13. public classPass {

14. public static void main(String []args){

15. int x = 5;

16. Pass p = new Pass();

17. p.doStuff(x);

18. System.out.print(" main x =" + x);

19. }

20

21. void doStuff(int x) {

22. System.out.print("doStuff x = " + x++);

23. }

24. }

程序的运行结果是?

A. 编译失败.

B. 运行时抛出异常.

C. doStuff x = 6main x = 6

D. doStuff x = 5 main x = 5

E. doStuff x = 5 main x = 6

F. doStuff x = 6 main x = 5

第二十四题

1. public class GC {

2. privateObject o;

3. private void doSomethingElse(Object obj){ o = obj; }

4. public void doSomething() {

5. Objecto = new Object();

6. doSomethingElse(o);

7. o = new Object();

8. doSomethingElse(null);

9. o = null;

10. }

11. }

如果方法doSomething被调用,在代码的哪一行之后,在第五行创建的Object对象变为可被garbage collector所回收的对象

A. Line 5

B. Line 6

C. Line 7

D. Line 8

E. Line 9

F. Line 10

第二十五题

11. public staticvoid test(String str) {

12. int check = 4;

13. if (check = str.length()) {

14. System.out.print(str.charAt(check-= 1) +" ");

15. } else {

16. System.out.print(str.charAt(0)+ " ");

17. }

18. }

并且调用

21.test("four");

22. test("tee");

23.test("to");

结果是什么?

A. r t t

B. r e o

C. 编译失败.

D.运行时抛出异常.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: