scjp考题1(详尽答案)(6)

2008-02-23 09:24:53来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折


if(b.booleanValue()){
System.out.println("Yes : " b);
}else{
System.out.println("No : " b);
}
A.The code will not compile.
B.It will print – Yes: true
C.It will print – Yes: TRUE
D.It will print – No: false
E.It will print – No: FALSE
B is the correct choice. The wrapper class Boolean has the following constructor -public Boolean(String s) It allocates a Boolean object representing the value true if the string argument is not null and is equal, ignoring case, to the string "true". Otherwise, allocate a Boolean object representing the value false.E.g.
new Boolean("TRUE") produces a Boolean object that represents true.
new Boolean("anything") produces a Boolean object that represents false.
The internal toString() representation of this object produces the boolean value string in lower case, hence it prints "Yes : true" instead of "Yes : TRUE".

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 )

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:JavaBean 进行数据验证(1)

下一篇:javax.servlet.http.HttpSession翻译