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


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

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




*****   ============================
3.7 What is displayed when the following piece of code is compiled and executed?
class Test {
   public static void main(String [] args) {
           Base b = new Subclass();
           System.out.println(b.x);
           System.out.println(b.method());
       }
}
class Base {
   int x = 2;
   int method(){
           return x;
   }
}
class Subclass extends Base {
   int x = 3;
   int method() {
   return x;
   }
}
A. Nothing. The code fails to compile because the object b is not created in a valid way.
B. 2 3
C. 2 2
D. 3 3
E. 3 2子类引用赋给父类,运行时按父类变量赋值,而方法则用子类的
Answer: B

**      =======================
3.8 What will happen when you attempt to compile and run the following code?
    (Assume that the code is compiled and run with assertions enabled.)
1.  public class AssertTest
2.  {
3.      public static void main(String args[])
4.      {
5.          for(int i=0;i <10;i++)
6.          {
7.              try
8.              {
9.                  assert i%2==0 : i--;
10.                 System.out.println("Even number : " + i);
11.             }
12.             catch(AssertionError ae)
13.             {
14.                 System.out.println("Odd number : " + ++i);
15.             }
16.         }
17.     }
18. }
A. It will print odd and even numbers from 0 to 9 correctly (0 even and 1 odd)
B. It will print odd and even numbers from 0 to 9 incorrectly (0 odd and 1 even)
C. Compilation error at line 9
D. Compilation error at line 10
E. It will result in an infinite loop
Answer: A

**      ====================
3.9 What will happen when you attempt to compile and run the following code?
(Assume that the code is compiled and run with assertions enabled)
1.  public class AssertTest
2.  {
3.      public static void main(String args[])
4.      {
5.          float f1 = Float.NaN;
6.          float f2 = f1;
7.          float f3 = 1.2f;
8.          
9.          try
10.         {
11.             assert (f2 == f1) : f2 = 2;
12.             f3 = 1.5f;
13.         }catch(AssertionError ae)
14.         {
15.             f3++;
16.         }
17.        
18.         f3 += f2;
19.         System.out.println("f3 = " + f3);
20.     }
21. }
A. Compilation error at line 5
B. Compilation error at line 7
C. It will print - f3 = 3.5
D. It will print - f3 = 4.2
E. It will print - f3 = NaN
Answer: D

****    =================
3.10 Which of the following will compile without errors?
A. Byte b = new Byte(123);
B. Byte b = new Byte("123");
C. Byte b = new Byte();//Constructor Summary        
Byte(byte value)
          Constructs a newly allocated Byte object that represents the specified byte value.        
Byte(String s)
          Constructs a newly allocated Byte object that represents the byte value indicated by the String parameter.
  b = 123;
D. Byte b = new Byte((int)123.4);
Answer: B

****    =====================
3.11 What will happen when we try to compile the following code?
1.  public void WhichArray( Object x )
2.  {
3.          if( x instanceof int[] )
4.      {
5.              int[] n = (int[]) x ;
6.              for( int i = 0 ; i < n.length ; i++ )
7.          {
8.                  System.out.println("integers = " + n[i] );
9.              }
10.         }    
11.         if( x instanceof String[] )
12.     {
13.             System.out.println("Array of Strings");
14.         }
15.     }
A. The compiler objects to line 3 comparing an Object with an array.
B. The compiler objects to line 5 casting an Object to an array of int primitives.
C. The compiler objects to line 11 comparing an Object to an array of Objects.
D. It compiles without error.
Answer: D

****    ==================
3.12 Which of the following statements about threading are true?
A. You can only obtain a mutually exclusive lock on methods in a class that extends Thread or implements runnable.
B. You can obtain a mutually exclusive lock on any object.
C. You can't obtain a mutually exclusive lock on any object.
D. Thread scheduling algorithms are platform dependent.
Answer: BD


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

评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
  • java认证考试 java考试题 java程序员考试 java工程师考试
如果你觉得一篇文章有用,你可以在每篇后面参与评论,或者查看其他人的评论,请保证你的评论对大家友好。
点这里评论
或者您可以来资源论坛参与讨论,一切都是免费的,不过可能需要麻烦您注册一下。
点这里讨论
把你的文章登陆在这里,让大家来分享你的文章。请立即登陆发表!
点这里投稿