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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值