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


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

本文简介:What will happen when you attempt to compile and run the following code



20. What is the result when you compile and run the following code?
public class Test{
public void method(){
for(int i = 0; i < 3; i++) {
       System.out.print(i);
       }
       System.out.print(i);
}
}
A. 0122
B. 0123
C. compile error
D. none of these
C is correct. The code on compilation will give compile time error because the scope of variable i is only within "for" loop.

21. What will happen when you attempt to compile and run the following code?
int Output = 10;
boolean b1 = false;
if((b1 == true) && ((Output += 10) == 20)){
   System.out.println("We are equal " + Output);
}else{
   System.out.println("Not equal! " + Output);
}
A.compile error
B.compile and output of “we are equal 10”
C.compile and output of “not equal!20”
D.compile and output of “not equal!10”  
D is correct, according to short logic operator rule

22. What will be the result of executing the following code?
Given that Test1 is a class.
1. Test1[] t1 = new Test1[10];
2. Test1[][] t2 = new Test1[5][];
3. if (t1[0] == null)
4. {
5.t2[0] = new Test1[10] ;
6.t2[1] = new Test1[10];
7.t2[2] = new Test1[10];
8.t2[3] = new Test1[10];
9.t2[4] = new Test1[10];
10. }
11. System.out.println(t1[0]);
12. System.out.println(t2[1][0]);
A. The code will not compile because the array t2 is not initialized in an unconditional statement before use.
B. The code will compile but a runtime exception will be thrown at line 12.
C. The code will compile but a runtime exception will be thrown at line 11.
D. None of these
D is correct. Though we cannot use local variables without initializing them (compilation error), there is an exception to it. In case of arrays initialization is supposed to be complete when we specify the leftmost dimension of the array. The problem occurs at runtime if we try to access an element of the array which has not been initialized (specification of size). In the question above the array t2 is initialized before use, therefore there will be no problem at runtime also and the lines 11 and 12 will both print null.

23. What will happen when you attempt to compile and run the following code?
class Base{
int i = 99;
public void amethod(){
       System.out.println("Base.amethod()");
    }
    Base(){
     amethod();
     }
}
public class Derived extends Base{
int i = -1;
public static void main(String argv[]){
     Base b = new Derived();
       System.out.println(b.i);
       b.amethod();
   }
   public void amethod(){
       System.out.println("Derived.amethod()");
   }
}
A. Derived.amethod()
-1
Derived.amethod()
B. Derived.amethod()
  99
  Derived.amethod()
C. 99
D. 99
  Derived.amethod()
E. compile time error.
B is correct. The reason is that this code creates an instance of the Derived class but assigns it to a reference of a the Base class. In this situation a reference to any of the fields such as i will refer to the value in the Base class, but a call to a method will refer to the method in the class type rather than its reference handle. But note that if the amethod() was not present in the base class then compilation error would be reported as at compile time, when compiler sees the statement like b.amethod(), it checks if the method is present in the base class or not. Only at the run time it decides to call the method from the derived class.

24. What will be the output on compiling/running the following code?
public class MyThread implements Runnable {
  String myString = "Yes ";
  public void run() {
    this.myString = "No ";
  }
  public static void main(String[] args)  {
    MyThread t = new MyThread();
    new Thread(t).start();
    for (int i=0; i < 10; i++)
     System.out.print(t.myString);
  }
}
A. compile error
B. prints: yes yes yes yes yes yes and so on
C. prints: no no no no no no no no and so on
D. prints: yes no yes no ye no ye no and so on
E. the output cannot be determinated
E is correct. Please note that there will not be any compilation error when the above code is compiled. Also note that calling start() method on a Thread doesn't start the Thread. It only makes a Thread ready to be called. Depending on the operation system and other running threads, the thread on which start is called will get executed. In the above case it is not guaranteed that the thread will be executed(i.e. run() method will be called), always before "for" loop is executed. Thus the output cannot be determined.

25. Multiple objects of MyClass (given below) are used in a program that uses multiple Threads to create new integer count. What will happen when other threads use the following code?
class MyClass{
static private int myCount = 0;
int yourNumber;
private static synchronized int nextCount(){
return ++myCount;   //myCount为static
}
public void getYourNumber(){
yourNumber = nextCount();
}
}
A. the code ill give compilation error
B. the code ill give runtime error
C. each thread will get a unique number
D. the uniqueness of the number different Threads can’t be guaranteed.
C is correct. The use of synchronized ensures that the number generated will not be duplicated, no matter how many Threads are trying to create the number. Thus D is incorrect. A and B are incorrect as the above code will not give any compiletime or runtime error.


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

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