1. 程式人生 > >Sun java認證考試真題答案及部分解析(二)

Sun java認證考試真題答案及部分解析(二)

21. The GenericFruit class declares the following method.


    public void setCalorieContent( float f )


You are writing a class Apple to extend GenericFruit and wish 
to add methods which overload the method in GenericFruit.


Select all of the following which would constitute legal 
declarations of overloading methods:


A. protected float setCalorieContent(String s )
B. protected void setCalorieContent( float x )
C. public void setCalorieContent( double d )
D. public void setCalorieContent(String s ) throws NumberFormatException

答案:ACD

22. What ouput does the following code give?


int i = 1;
i <<= 31;
i >>= 31;
i >>= 1;


int j = 1;
j <<= 31;
j >>= 31;


System.out.println("i = " + i + " j = " + j);


A. i = 1 j = 1
B. i = -1 j = 1 
C. i = 1 j = -1 
D. i = -1 j = -1
答案:D




23. What happens when we try to compile and run code 
    containing the following lines:


1. Float A = new Float( 1.0F );
2. String S = "value is " + A;
3. System.out.println( S );


Select 1 correct answer:
A. The compiler objects to line 2.
B. The program compiles and prints "value is 1.0".
C. A runtime exception occurs in line 2.


答案:B


24. Which of the following statements assign 
    the value 5 to int a?


Select all correct answers:
A. int a = (int)(2.1F + 3.4D);
B. int a = (0x0A >> 1);
C. int a = (0x0A >>> 1);
D. int a = (0x5);
E. int a = (octal)5;


答案:A B C D 
E:錯誤 octal不能用於強制型別轉換




25. Given the following code fragment with a continue to 
    a labeled statement, predict the printed output:


1. int i, j;
2. lab: for( i = 0; i < 6; i++ ){
3. for( j = 5; j > 2; j-- ){
4. if( i == j ) {
5. System.out.print(" " + j );
   continue lab;
6. }
7. }
8. }


A. Output will be 3 4 5.
B. Output will be 3 4.
C. Output will be 3.
D. The statement on line 5 is never reached, 
   so there is no output.


答案:A


26. What will be the output of the following program if
    the int i = 12/x statement in method f2 causes a 
    java.lang.RuntimeException to be thrown?


public class TestException

public static void main (String [] args)
{
f1 ();
System.out.println("f1 complete");

public static void f1 ()

try {
f2();
System.out.println("f2 complete");
}
catch (Throwable t) {}
}
public static void f2 ()
{ int x = 0;
int i = 12/x;
System.out.println("Division by zero..."); 
}
}
Select 1 correct answer:
A. f2 complete
B. f1 complete
C. Division by zero...
D. None of the above.
答案:B


27. Given the following code:


 1. public class MyClass {
 2. public static Object getObject() {
 3. Object obj = new Integer(3);
 4. Object td[][] = new Object[1][2];
 5. td[0][1] = obj;
 6. td[0][0] = obj;
 7. obj = null;
 8. return obj;
 9. }
10. }


Which one of the following statements is true?


A. The class will not compile. 
B. The getObject() method must not be declared as static. 
C. The class compiles, but an exception is received because 
   td is not set to null. 
D. The obj object is eligible for garbage collection after 
   a call to the getObject() method has returned.


答案:D




28. What is the result of trying to compile and run
    the following program?


class Base
{
public static void doIt()
{
System.out.println("Base doit");
}
}


class Sub extends Base
{
public static void doIt()
{
System.out.println("Sub doit");
}
}


class bstest
{
public static void main(String []p)
{
Base b = new Sub();
b.doIt();
}
}


Select 1 correct answer: 
A. Compiles and runs printing "Sub doit"
B. Compiles and runs printing "Base doit"
C. The program fails to compile
D. None of the above


答案:B


29. What will happen if you compile and run the following code?


class Test2
{
static void show()
{
System.out.println("Show method in Test class");
}
}


public class Q2 extends Test2

static void show()
{
System.out.println("Show method in Q2 class");
}


public static void main(String[] args)
{
Test2 t = new Test2();
t.show();
Q2 q = new Q2();
q.show();


t = q;
t.show();


q = (Q2)t;
q.show();
}
}


Select 1 correct answer:
A. Prints: Show method in Test class
           Show method in Q2 class
           Show method in Test class
           Show method in Q2 class
B. Prints: Show method in Test class
           Show method in Q2 class
           Show method in Q2 class
           Show method in Q2 class
C. Prints: Show method in Test class
           Show method in Q2 class
           Show method in Test class
           Show method in Test class


答案:A
原因:static方法按照宣告的型別呼叫。

30. What will the following code print?


Double a = new Double(Double.NaN);
Double b = new Double(Double.NaN);


if( Double.NaN == Double.NaN )
System.out.println("True");
else
System.out.println("False");


if( a.equals(b) )
System.out.println("True");
else
System.out.println("False");


Select 1 correct answer:
A. True
   True 
B. True
   False 
C. False
   True 
D. False
   False


解析:從原始碼中可知,從雙精度浮點數建立Double類時是沒有快取機制的,故a和b是不同的物件。
在java的文件中,明確提到,在大部分情況下,equals的結果和d1.doubleValue() == d2.doubleValue()一致,但是有兩個例外:
如果d1,d2都是Double.NaN,則equals返回true,但是Double.NaN==Double.NaN返回false
如果d1 代表+0.0,d2 代表-0.0,那麼equals返回false,但是+0.0==-0.0會返回true
答案:C

31. What will happen if you compile/run this code?


 1: public class Q10
 2: {
 3: public static void main(String[] args)
 4: {
 5: int i = 10;
 6: int j = 10;
 7: boolean b = false;
 8: 
 9: if( b = i == j)
10: System.out.println("True");
11: else
12: System.out.println("False");
13: }
14: }


Select 1 correct answer:
A. Compilation error at line 9 .
B. Runtime error exception at line 9.
C. Prints "True".
D. Prints "False".


解析:考察對b = i == j的理解,
答案:C
32. What is the output of the following code?


 1: class Test
 2: {
 3: Test(int i)
 4: {
 5: System.out.println("Test(" +i +")");
 6: }
 7: }
 8:
 9: public class Q12
10: {
11: static Test t1 = new Test(1);
12:
13: Test t2 = new Test(2);
14:
15: static Test t3 = new Test(3);
16:
17: public static void main(String[] args)
18: { 
19: Q12 Q = new Q12();
20: }
21: } 


Select 1 correct answer:
A. Test(1)
   Test(2)
   Test(3)


B. Test(3)
   Test(2)
   Test(1)


C. Test(2)
   Test(1) 
   Test(3)


D. Test(1)
   Test(3) 
   Test(2)
解析:考察對類的初始化過程的理解,先執行靜態初始化語句,再執行其餘語句
答案:D
33. What is the output of the following code?


1: int i = 45678;
2: int j = ~i;
3:
4: System.out.println(j);


Select 1 correct answer:
A. Compilation error at line 2.
B. Prints 45677.
C. Prints -45677.
D. Prints -45679.


答案:D


34. Which of the following statements about Java garbage 
    collection is a true statement?


Check all correct answers:
A. The following code will start the garbage collector:
   System.gc();
   Thread.yield();
B. Calling Runtime.getRuntime().gc() will probably start 
   the garbage collection mechanism, but there is no guarantee.
C. The garbage collection Thread has a low priority.
D. The method by which Java determines that a chunk of memory 
   is garbage is up to the implementer of the JVM.


解析:System.gc():只能提醒虛擬機器:程式設計師希望進行一次垃圾回收,但不能保證垃圾回收一定會進行。在文件中寫道,當控制權從對方法的呼叫中返回時,JVM會做最大努力來來從所有的不可達物件中回收空間。
Yield就不太懂了。
D也不確定。
答案:B C 
35. Given the following method definition in a class that 
    otherwise compiles correctly:


public boolean testAns( String ans , int n )
{
boolean rslt;
if( ans.equalsIgnoreCase("YES") &&
n > 5 ) rslt = true;
return rslt;
}


What will be the result of trying to compile the class 
and execute the testAns method with inputs of "no" and 5?


A. A runtime exception will be thrown in the testAns method.
B. A result of false will be returned.
C. A compiler error will prevent compilation.
D. A result of true will be returned.


答案:C 【似乎在eclipse裡面,不初始化就會報錯】
36. What is the result of compiling and running the 
    following code?


class Fruit
{
public Fruit(String color) 
{
System.out.print("Color = " + color);
}
}


class Apple extends Fruit
{
public static void main(String [] args) 
{
Apple m = new Apple();
}
}


Select 1 correct answer:
A. A new Apple object is created.
B. Run time error in main.
C. The code compiles but no object is created.
D. Compile time error in main. No matching constructor is found.
答案:D


37. Given the following code for the Demo class


public class Demo 
{
private String[] userNames;
public Demo()

userNames = new String[10];
}
public void setName( String s, int n )
{
userNames[n] = s;
}
public void showName( int n )
{
System.out.println( "Name is " +
userNames[n]);
}
public String getName( int n )
{
return userNames[n];
}



What would be the result of calling the showName 
method with a parameter of 2 immediately after 
creating an instance of Demo?


Select 1 correct answer:
A. Standard output would show "Name is null".
B. A NullPointerException would be thrown.
C. An ArrayIndexOutOfBoundsException would be thrown.
D. Standard output would show "Name is ".
答案:考察對初始化的理解。String陣列會被初始化為null(不是字串null),在列印的時候會顯示字串null。
答案:A


38. Given that a class, Test, declares a member variable 
    named "Scores" as an array of int as follows:


int Scores[];


Which of the following code fragments would correctly 
initialize the member variable Scores as an array of 4 
int with the value of zero if used in the Test constructor? 


Check all correct answers:
A. int Scores[] = {0,0,0,0} ;
B. Scores = new int[4] ;
C. Scores = new int[4] ;
   for( int i = 0 ; i < 4 ; i++ )
   { Scores[i] = 0 ; }
D. Scores = { 0,0,0,0 };


解析:考察陣列初始化的理解
A:重複宣告,錯誤
D:這種宣告方式只能用在初始化的語句中,而不能初始化了之後再用
答案:B C D
39. Which method placed at line 6 will cause a compiler error?


1. class Super{
2. public float getNum(){return 3.0f;}
3. }
4.
5. public class Sub extends Super{
6.
7. }


Select 1 correct answer:
A. public float getNum(){return 4.0f;}
B. public void getNum(){}
C. public void getNum(double d){}
D. public double getNum(float d){return 4.0d;}


答案:B




40. What will be the output of the following code?


public class Test{
public static void main(String args[]){
try{return;}
finally{ System.out.println("Finally");}

}


Select 1 correct answer:
A. No output. 
B. The output will be "Finally".
C. Compile error.
D. Nothing of the above.


答案:B