为了账号安全,请及时绑定邮箱和手机立即绑定

ocjp 考试题

标签:
面试

QUESTION 102 Given:

  1. Object [] myObjects = {

  2. new Integer(12),

  3. new String("foo"),

  4. new Integer(5),

  5. new Boolean(true)

  6. };

  7. Arrays.sort(myObjects);

  8. for(int i=0;i<myObjects.length; i++) {

  9. System.out.print(myObjects[i].toString());

  10. System.out.print("");

  11. }

What is the result?

A.   Compilation fails due to anerror in line 23.

B.   Compilation fails due to anerror in line 29.

C.   A ClassCastException occurs inline 29.

D.   A ClassCastException occurs inline 31.

E.   The value of all four objectsprints 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
如果是true,则返回的是assert is on

QUESTION 105 Given:

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

  2. try {

  3. args = null;

  4. args[0] = "test";

  5. System.out.println(args[0]);

  6. } catch (Exception ex) {

  7. System.out.println("Exception");

  8. } catch (NullPointerExceptionnpe) {

  9. System.out.println("NullPointerException");

  10. }

  11. }

What is the result?

A.   test

B.   Exception

C.   Compilation fails.

D.   NullPointerException

Answer: C
首先是小异常处理,然后是大的异常处理!!!!如果返过来,就有问题了,导致编译失败!!!

QUESTION 106
Given:

  1. public void go() {

  2. String o = "";

  3. z:

  4. for(int x = 0; x < 3; x++) {

  5. for(int y = 0; y < 2; y++) {

  6. if(x==1) break;

  7. if(x==2 && y==1) break z;

  8. o = o + x + y;

  9. }

  10. }

  11. System.out.println(o);

  12. }
    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 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),无法运算,注意看属性Integer i,没有给i赋值;

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:

  1. public class Foo {

  2. static int[] a;

  3. static { a[0]=2; }

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

  5. }
    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:

  1. class X { public void foo() { System.out.print("X "); } }


  2. public class SubB extends X {

  3. public void foo() throws RuntimeException {

  4. super.foo();

  5. if (true) throw new RuntimeException();

  6. System.out.print("B ");

  7. }

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

  9. new SubB().foo();

  10. }

  11. }
    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 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数量却不丢失任何功能,意思是各个模块之间的联系很小,互联程度很低,属于低耦合。

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消