OCJP标准题库试题之————第二次

QUESTION 13
Click the Exhibit button. Which statement is true about the classes and interfaces in the exhibit?

这里写图片描述

A. Compilation will succeed for all classes and interfaces.
B. Compilation of class C will fail because of an error in line 2.
C. Compilation of class C will fail because of an error in line 6.
D. Compilation of class AImpl will fail because of an error in line 2.
Answer: C
Section: (none)

我们要明白一个知识点:继承重写的返回值。可以是父类但不能是更高一级别的父类。子类的抛的异常不能是父类的异常。可以是向下抛出子类的异常。

我们看到了,AImpl实现了A接口。C类继承 B类,但是C类重写B类的execute方法的时候,有问题,C类重写方法的返回值是B类中execute的返回值的父类。这是错误的。编译不能通过的。由此从选项中,分析:A:所有的类和接口都编译成功 显然是不对的。

B:C类编译失败。错误在第二行。根据我们分析错误实在第6行。B×

C:编译失败。错误在第六行。C√

D:编辑AImpl会失败,因为错误在第二行。D×

QUESTION 14
Which two code fragments correctly create and initialize a static array of int elements? (Choose two.)
A. static final int[] a = { 100,200 };
B. static final int[] a;
static { a=new int[2]; a[0]=100; a[1]=200; }
C. static final int[] a = new int[2]{ 100,200 };
D. static final int[] a;
static void init() { a = new int[3]; a[0]=100; a[1]=200; }
Answer: AB
Section: (none)

分析:这里考察的是对象的初始化,常见的初始化的方法:1.构造方法,2.初始化块,3静态块。他们的运行优先性是 3》2》1。

A:A项是生命即初始化,正确
B:先声明,后初始化,采用的是静态块初始化,B√
C:标注长度,画蛇添足。C×
D:根本没有这样的写法

QUESTION 15
Given:
10. interface Foo { int bar(); }
11. public class Sprite {
12. public int fubar( Foo foo ) { return foo.bar(); }
13. public void testFoo() {
14. fubar(
15. // insert code here
16. );
17. }
18. }
Which code, inserted at line 15, allows the class Sprite to compile?
A. Foo { public int bar() { return 1; }
B. new Foo { public int bar() { return 1; }
C. new Foo() { public int bar() { return 1; }
D. new class Foo { public int bar() { return 1; }
Answer: C

分析:这里考察的是匿名类的使用。让我们在15行写代码让这个类能编译。显然选择C。只有C让通过编译。一个接口没有实现类,而另一个方法的参数又是这个接口,调用里面的方法。然后又调用了这个方法。这时候就需要new这个接口。并且在里面实现该接口的方法。

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

这里有一个锯齿数组:内容 第一行 1,2 第二行 3,4,5.第三行,6,7,8,9. 求Y【2】【1】. 数字从0开始。所以我们要找的值是第三行第二列的7.选择E

QUESTION 17
Given:
22. StringBuilder sb1 = new StringBuilder("123");
23. String s1 = "123";
24. // insert code here
25. System.out.println(sb1 + " " + s1);
Which code fragment, inserted at line 24, outputs "123abc 123abc"?
A. sb1.append("abc"); s1.append("abc");
B. sb1.append("abc"); s1.concat("abc");
C. sb1.concat("abc"); s1.append("abc");
D. sb1.concat("abc"); s1.concat("abc");
E. sb1.append("abc"); s1 = s1.concat("abc");
F. sb1.concat("abc"); s1 = s1.concat("abc");
G. sb1.append("abc"); s1 = s1 + s1.concat("abc");
H. sb1.concat("abc"); s1 = s1 + s1.concat("abc");
Answer: E

分析:考察StringBuffer的append方法和String的contact方法:append追加字符串。contact方法:一个字符串拼接另一个字符串,返回拼接的结果,不修改原来的值。根据这个知识我们分析每个选项的sb1+s1的结果

A:String类型没有append方法
B:123abc+123 B×
C:同A
D:Stringbuffer没有concat方法
E:正确
F:同D
G:123abc+123123abc 也不对
H:同D

QUESTION 18
Given that the current directory is empty, and that the user has read and write permissions, and the
following:
11. import java.io.*;
12. public class DOS {
13. public static void main(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. }
Which statement is true?
A. Compilation fails.
B. The file system has a new empty directory named dir.
C. The file system has a new empty directory named newDir.
D. The file system has a directory named dir, containing a file f1.txt.
E. The file system has a directory named newDir, containing a file f1.txt.
Answer: E

分析题目:编译正确:15行:创建了一个叫做dir的目录,18行在dir目录创建了一个f1.txt的文件。21行将dir目录改名为newDir。所以钻则E。文件系统有个叫做newdir的目录里面有个f1.txt的文件

QUESTION 19
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
Section: (none)

分析。main函数开始接受了字符串数组。12.字符串不会自动转为integer的。所以编译错误。

QUESTION 20
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
Section: (none)

考察 java的正则表达式。显然分析是用.和空格来分割这个字符串的。.指任意字符。s标识space空格。当然需要转义。\有特殊的用途所以就双斜线。

QUESTION 21
Given:
5. import java.util.Date;
6. import java.text.DateFormat;
21. DateFormat df;
22. Date date = new Date();
23. // insert code here
24. String s = df.format(date);
Which code fragment, inserted at line 23, allows the code to compile?
A. df = new DateFormat();
B. df = Date.getFormat();
C. df = date.getFormat();
D. df = DateFormat.getFormat();
E. df = DateFormat.getInstance();
Answer: E

格式化:日期我们要把 格式化日期工具创建出来。DateFormat创建不是通过new实现的。选择E

Given a class Repetition:
1. package utils;
2.
3. public class Repetition {
4. public static String twice(String s) { return s + s; }
5. } and given another class Demo: 1. // insert code here
2.
3. public class Demo {
4. public static void main(String[] args) {
5. System.out.println(twice("pizza"));
6. }
7. }
Which code should be inserted at line 1 of Demo.java to compile and run Demo to print "pizzapizza"?
A. import utils.*;
B. static import utils.*;
C. import utils.Repetition.*;
D. static import utils.Repetition.*;
E. import utils.Repetition.twice();
F. import static utils.Repetition.twice;
G. static import utils.Repetition.twice;
Answer: F

这里考察的是静态导入。一个类的某个方法. import +static+包名+方法名字

import static utils.Repetition.twice
QUESTION 23
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
Section: (none)

这里考察的类路径。一个叫bob的人。有个象棋程序。现在要去更新原来的程序。但是不知道这个程序放哪了。现在能运行原来的程序。让我们猜测这个程序在哪?java -classpath /test:/home/bob/downloads/*.jar games.Chess。通过这里。程序运行,加载test下的包。和/home/bob/downloads/下的所有jar文件。那么程序可能就在test下和downloads下。有因为。调用是 games.Chess,运行程序。downloads目录是一堆jar包。而调用在games目录下。所以可能就在/test/games/Chess.class这里。选择C

其它题目参考 http://www.cnblogs.com/husam/p/3880475.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一缕阳光直射你的心扉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值