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

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

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


public class Test

public static String output="";


public static void foo(int i)
{
try
{
if(i==1)
{
throw new Exception();
}
output +="1";
}
catch(Exception e)
{
output+="2";
return;
}
finally
{
output+="3";
}
output+="4";
}


public static void main(String args[])
{
foo(0);
System.out.print(output + " ");
foo(1);
System.out.println(output);
}
}


Select 1 correct answer:
A. 134 134234 
B. 13 123
C. 134 13423
D. 14 13423
答案:C


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


public class Test

public static void main(String args[])
{
int i=1,j=10;
do{
if(i++>--j) continue;
}while(i<5);
System.out.println(i +" " +j);
}
}


Select 1 correct answer:
A. i=6 j=5 
B. i=5 j=5 
C. i=6 j=4 
D. i=5 j=6 
E. i=6 j=6


答案:D
43. What will be the output of the following code?


interface Foo{
int k=0;
}
public class Test implements Foo{
public static void main(String args[]){
Test test =new Test();
System.out.print(test.k + " ");
System.out.print(Test.k + " ");
System.out.println(Foo.k);
}
}


Select 1 correct answer:
A. 0 0 0
B. A runtime exception is thrown.
C. The code does not compile.


解析:考察對介面的理解:介面中所有的變數都是public static final,並且不允許沒有初始值。
答案:A


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


class Dum
{
private int f = 5;


public void setF(int f)
{
this.f = f;
}


public int getF()
{
return f;
}
}


public class Test{


public static void change(Dum d)
{
d.setF(4581);
}


public static void main(String args[]){
Dum dd = new Dum();
System.out.print(dd.getF() + " ");
change(dd);
System.out.println(dd.getF());
}
}


Select 1 correct answer: 
A. 5 5
B. The code will not compile.
C. Runtime exception will be thrown.
D. 5 4581


答案:D


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


public class Test{
public static void add3(Integer i){
int val=i.intValue();
val+=3;
i=new Integer(val);
}
public static void main(String args[]){
Integer i=new Integer(0);
add3(i);
System.out.println(i.intValue());
}
}


Select 1 correct answer: 
A. The code will not compile. 
B. 0 
C. 3 
D. A runtime exception will be thrown.


答案:B
46. What will be the output of the following code?


public class Test{
public static void stringReplace(String text){
text=text.replace('j','l');
}
public static void bufferReplace(StringBuffer text){
text=text.append("c");
}
public static void main(String args[]){
String textString=new String("java");
StringBuffer textBuffer=new StringBuffer("java");
stringReplace(textString);
bufferReplace(textBuffer);
System.out.println(textString + " " + textBuffer);
}}


Select 1 correct answer: 
A. lava javac
B. lava java
C. java javac
D. The code will not compile.
解析:string是不可變的,故會產生新的引用,但這一引用並不能通過stringReplace反映到外面的text變數上,而StringBuilder是可變的, 對其的改變會改變自己,同時返回自己的引用。
答案:C


47. Given the following code:


class EnclosingOne{
public class InsideOne{}
}
public class InnerTest{
public static void main(String args[]){
EnclosingOne eo=new EnclosingOne();
//insert code here
}
}


Which of the next statements can be placed 
legally in place of //insert code here?


Select all correct answers: 
A. InsideOne ei=eo.new InsideOne();
B. eo.InsideOne ei=eo.new InsideOne();
C. InsideOne ei=EnclosingOne.new InsideOne();
D. InsideOne ei=eo.new InsideOne();
E. EnclosingOne.InsideOne ei=eo.new InsideOne();


解析:考察對巢狀類的操作方法
答案:E


48. Given the following code:


import java.awt.*;
public class Test extends Frame{
public static void main(String[] args){
Test x=new Test();
x.pack();
x.setVisible(true);}


public Test(){
setLayout(new GridLayout(2,2));
Panel p1=new Panel();
add(p1);
Button b1=new Button("One");
p1.add(b1);
Panel p2=new Panel();
add(p2);
Button b2=new Button("Two");
p2.add(b2);
Button b3=new Button("Three");
p2.add(b3);
Button b4=new Button("Four");
add(b4);
}}


What will happen when the frame is resized?


Select all correct answers:
A. All buttons' height is changed. 
B. All buttons' width is changed. 
C. The height of button "One" is changed.
D. The height of button "Two" is changed.
E. The width of button "Four" is changed.
F. Both the width and the height of button "Four" are changed.
答案:EF


49. Which four types of objects can be thrown using "throws"?


A. Error
B. Event
C. Object
D. Exception
E. Throwable
F. RuntimeException
答案:A D E F


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


1 public class X {
2 public static void main(String[] args) {
3 int[] a=new int[1];
4 modify(a);
5 System.out.println(a[0]);
6 }
7 public static void modify(int[] a){
8 a[0]++;}
9 }


Select 1 correct answer:
A. The program runs and prints 0.
B. The program runs and prints 1.
C. The program runs but aborts with an exception.
D. An error "possible undefined variable" at line 4 
   causes compilation to fail;
E. An error "possible undefined variable" at line 8 
   causes compilation to fail;


答案:B


51. What will be the result of trying to compile
    and run the following code?


1 public class X {
2 public static void main(String[] args) {
3 byte b = 127;
4 byte c = 126;
5 byte d = b+c;
6 }
7 }


Select 1 correct answer:
A. The code compiles but line 5 throws a runtime exception.
B. Line 5 fails to compile.
C. Successful compilation.


答案:B 原因在於,數值提升僅允許使用同一性轉換,擴充套件基本轉換或拆箱轉換,而不允許使用收縮基本轉換。而且,在進行二元運算時,會進行數值提升,至少提升到int。

52. What happens when we attempt to compile and 
    run the following code?


1. public class Logic {
2. static long sixteen = 0x0010;
3. static public void main( String args[] ) {
4. long N = sixteen >> 4;
5. System.out.println( "N = " + N );
6. }
7. }


Select 1 correct answer:
A. The compiler will object to line 4 combining a long with an int.
B. The program will compile and run, producing the output "N = 0".
C. The program will compile and run, producing the output "N = 1".
D. A runtime exception will be thrown.
答案:C
53. What will be the output?


public class Test {
public static void main( String[] args ) {
int i = 1;
int j = i++;
if( ( i > ++j ) && ( i++ == j ) ) {
i += j;
}
System.out.println( i + " " + j );
}
}


Select 1 correct answer:
A. 2 1
B. 1 1
C. 2 2
D. 1 2
解析:&&為短路與,如果第一個為false,則不再繼續運算
答案:C
54. Which statement is true for the class java.util.HashSet?


Select all correct answers:
A. The elements in the collection are ordered.
B. The collection is guaranteeded to be immutable.
C. The elements in the collection are guaranteed to be unique.
D. The elements in the collection are accessed using a unique key.
E. The elements in the collection are guaranteed to be synchronized.


答案:D
注意:HashSet不是同步的。


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


public class Test
{
public static void main( String []args ) {
StringBuffer a = new StringBuffer("A");
StringBuffer b = new StringBuffer("B");
operate( a,b );
System.out.println( a + "," + b );
}


public static void operate( StringBuffer x, StringBuffer y ) {
x.append( y );
y = x;
}
}


Select 1 correct answer:
A. A, B
B. The code will not compile.
C. B, A
D. AB, B
答案:D


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


public class Test
{
public static void main(String[] args) {


class Foo
{
public int i=3;
}


Object o = (Object)new Foo();
Foo foo = (Foo)o;
System.out.println( foo.i );
}
}


Select 1 correct answer:
A. Compile time error.
B. Run time error.
C. 3


答案:C


57. Given the following code fragment, what will happen 
    when we try to compile and run the showSmall method?


1. public void printArray( long[] x ){
2. for(int i = 0 ; i < x.length ; i++ ){
3. System.out.println("# " + i + " = " + x[i] );
4. }
5. }
6. int[] small = { 1,2,3,4,5,6,7,8,9,0 };
7. public void showSmall() {
8. printArray( small );
9. }


Select 1 correct answer:
A. The code compiles and the JVM automatically promotes 
   the int array to a long array.
B. The compiler complains that there is no method matching
   the use in line 8.
C. The code compiles but a runtime ClassCastException 
   is thrown in line 8.
解析:考察對型別提升的理解:JVM不能自動將int陣列提升為long陣列。
答案:B


58. What will be the output of the following program? 


class A implements Runnable {
public int i = 1;
public void run() {
System.out.println("in run");
this.i = 10;
System.out.println(i);
}
}


public class Test {
public static void main(String args[]) {
A a = new A();


new Thread(a).start();
int j = a.i;


System.out.println(j);
}
}


Select 1 correct answer:
A. 1
B. 10
C. The code compiles but a runtime is thrown.
D. The program runs but the exact output cannot be determined.
答案:D


59. What happens on trying to compile and run the following code?


1. public class EqualsTest{
2. public static void main( String args[] ){
3. float A = 1.0F / 3.0F ;
4. if( ( A * 3.0) == 1.0F )
       System.out.println( "Equal" );
5. else System.out.println( "Not Equal" );
6. }
7. }


Select 1 correct answer:
A. The program compiles and prints "Not Equal".
B. The program compiles and prints "Equal".
C. The compiler objects to line 3.
D. The compiler objects to using == with primitives in line 4.


答案:A