JAVA认证考试:SCJP考题2(详细答案)


作者: | 来源:blogjava | 日期:06-28 | 字体: [ ]

本文简介:Which of the following are valid constructors of the class java.lang.AssertionError


*****   =================
9.4 Which of the following are correct and proper usages of assertions?
A:private void doSomething(int value) {
   assert (value > 0 && value < 1000)  : "The value must be in the range [1 -999]";
   // more code
}
B:public void doSomething(int value) {
   assert (value > 0 && value < 1000)  : "The value must be in the range [1 -999]";
   // more code
}
C:private int doSomething() {
   // compute result
   assert result > 0 : "Incorrect result";
   return result;
}
D:public int doSomething() {
   // compute result
   assert result > 0 : "Incorrect result";
   return result;
}
A. A is a proper usage
B. B is a proper usage
C. C is a proper usage
D. D is a proper usage
Answer: B??????????
Note: This is a very important concept in assertions. Assertions can be used to test preconditions, postconditions and invariants. Java insists that assertions should not be used for testing the preconditions of the public methods. Instead, preconditions on public methods are enforced by explicit checks inside methods resulting in particular, specified exceptions. This is essential for two reasons-
(1) Exceptions can be explicitly mentioned in the throws cause of the method, and checked exceptions must be caught while invoking the method.
(2) Assertions can be disabled while running the code.
However, postcondition checks are best implemented via assertions, whether or not they are specified in public methods.

****    ==================
9.5 Which of the following can be inserted at line 12 so that the code compiles and prints the odd numbers in the given range?
1.  import java.util.ArrayList;
2.  
3.  public class OddNumbers
4.  {
5.      public static void main(String args[])
6.      {
7.          ArrayList oddNumbers = new ArrayList();
8.          for(int i=0;i < 10;i++)
9.          {
10.             if(i%2 == 0)
11.                 continue;
12.             // what should be inserted here?
13.             oddNumbers.add(number);
14.             System.out.println(number);
15.         }
16.     }
17. }
A. int number = i;
B. Integer number = new Integer(i);
C. Byte number = new Byte(i);
D. Integer number = Integer.parseInt("" + i);
E. Object number = new Integer(i);
Answer: BE

**      =====================
9.6 What will happen when you attempt to compile and run the following code?
interface MyInterface{
   int x = 0;
   int myMethod(int x);
}
class MyImplementation implements MyInterface{
   public int myMethod(int x){
       return super.x;
   }
}
public class MyTest{
   public static void main(String args[]) {
       MyInterface mi = new MyImplementation();
       System.out.println(mi.myMethod(10));
   }
}
A. 0
B. 10
C. Compilation error
D. None of the above
Answer: The statement "super.x;" inside myMethod() will cause compilation error as the super class of MyImplementation class (Object class) doesn't define variable x. Please note that MyInterface is not the super class of MyImplementation class as referred by super.x.

****    =========================
9.7 What is displayed when the following piece of code is executed?
class Test extends Thread{
   public void run(){
       System.out.println("1");
       yield();
       System.out.println("2");
       suspend();
       System.out.println("3");
       resume();
       System.out.println("4");
   }
   public static void main(String []args) {
       Test t = new Test();
       t.start();
   }
}
A. 1  2  3  4
B. 1  2  3
C. 1  2
D. Nothing. This is not a valid way to create and start a thread.
E. 1
Answer: C
Note: The code will run, but the thread suspends itself after displaying "2". Threads cannot un-suspend themselves (since they ARE suspended and therefore not running!).

**      =======================
9.8 Consider the following code fragment, and select the correct statements(s).
1.  public class Test extends MyBase implements MyInterface
2.  {
3.          int x = 0;
4.
5.          public Test(int inVal) throws Exception
6.          {
7.                  if (inVal != this.x)
8.                  {
9.                      throw new Exception("Invalid input");
10.                 }
11.         }
12.
13.         public static void main(String args[])
14.         {
15.                 Test t = new Test(4);
16.         }
17. }
A. The code fails to compile at line 1. It is not valid to both implement an interface and extend from a parent class simultaneously.
B. The code fails to compile at line 5. It is not valid for constructors to throw exceptions.
C. The code fails to compile at line 9, because this is not valid way to throw an exception.
D. The code fails to compile at line 15. The compiler complains that there is an uncaught exception.
E. The code fails to compile at line 7, because this is not a valid way to reference variable x.
Answer: D

****    ===================
9.9 The default implementation of equals() method in Object class does not perform "deep" comparison. State true or false.
A. True
B. False
Answer: A


相关考试文章
  • ·SCJP考试题310-025(第二套<4>)92-147/147
    ·SCJP考试题310-025(第二套<2>)19-50/147
    ·SCJP考试题310-025(第二套<3>)51-91/147
    ·SCJP考试题310-025(第二套<1>)18-147
    ·JAVA认证考试:SCJP考题2(详细答案)
    ·JAVA认证考试:SCJP考题1(详细答案)
    ·SCJP认证套题解析之二
    ·SCJP认证套题解析之一
    ·JAVA认证历年真题:SCJP认证套题解析[3]
    ·JAVA认证历年真题:SCJP考试真题和解析[2]
    ·JAVA认证历年真题:SCJP考试真题和解析[1]
    ·JAVA认证历年真题:SCJP认证套题解析[2]
  • 考试热门文章
  • ·JAVA认证历年真题:SCJP考试真题和解析[2]
    ·JAVA认证考试:SCJP考题2(详细答案)
    ·SCJP考试题310-025(第二套<4>)92-147/147
    ·JAVA认证历年真题:SCJP认证套题解析[1]
    ·JAVA认证历年真题:SCJP认证套题解析[3]
    ·JAVA认证考试:SCJP考题1(详细答案)
    ·JAVA认证历年真题:SCJP考试真题和解析[1]
    ·SCJP认证套题解析之一
    ·SCJP考试题310-025(第二套<3>)51-91/147
    ·JAVA认证历年真题:SCJP认证套题解析[2]
    ·SCJP认证套题解析之二
    ·SCJP考试题310-025(第二套<2>)19-50/147

  • 用户名: 新注册) 密码: 匿名评论 [所有评论]

    评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
    顶级栏目
    同级栏目
    下级栏目
    如果你觉得一篇文章有用,你可以在每篇后面参与评论,或者查看其他人的评论,请保证你的评论对大家友好。
    点这里评论
    或者您可以来资源论坛参与讨论,一切都是免费的,不过可能需要麻烦您注册一下。
    点这里讨论
    把你的文章登陆在这里,让大家来分享你的文章。请立即登陆发表!
    点这里投稿
    打印本页
    加入收藏
    关闭窗口