SCJP考题4

转载 2007年10月13日 15:00:00

500. Examine the following code which includes an inner class:

public final class Test implements A{

class Inner{

void test(){

if (!Test.this.flag);{

sample();

}

}

private boolean flag = false;

}

publi void sample(){

System.out.println("Sample");

}

public Test(){

(new Inner()).test();

}

public static void main(String args[]){

new Test();

}

}

What is the result?

A. Print out "Sample"

B. Program produces no output but terminates correctly.

C. Program does not terminate.

D. The program will not compile

正确答案: A

 

501. Given the following incomplete method:

1) public void method(){

2)

3)  if(someTestFails()){

4)

5)  }

6)

7) }

You want to make this method throw an IOException if, and only if, the method someTestFails() returns a value of true.

A.  Add at line 2:IOException e;

B.  Add at line 4:throw e;

C.  Add at line 4:throw new IOException();

D.  Add at line 6:throw new IOException();

E.  Modify the method declaration to indicate that an object of type Exception might be thrown.

正确答案: CE

 

502. Which exception  might wait() throw?

Fill in the blank.

正确答案: "InterruptedException" or "IllegalMonitorException"

 

503. Given this code snippet:

try{

tryThis();

return:

}catch(IOException x1){

System.out.println("exception 1");

return;

}catch(Exception x2){

System.out.println("exception 2");

return;

} finally{

System.out.println("finally");

}

What will appear in the standard output if tryThis() throws a NumberFormatException?

Select the one right answer.

A. Nothing

B. "exception 1",followed by "finally"

C. "exception 2",followed by "finally"

D. "exception 1"

E. "exception 2"

正确答案: C

 

504. What letters get written to the standard output with the following code?

class Test{

public static void main(String[] args){

try {

method();

} catch(Exception e){}

}

static void method(){

try {

wrench();

System.out.println("a");

} catch (ArithmeticExeption e){

System.out.println("b");

}finally{

System.out.println("c");

}

Syatem.out.println("d");

}

static void wrench(){

throw new NullPointerException();

}

}

Select all valid answer.

A. "a"

B. "b"

C. "c"

D. "d"

E. none of there

正确答案: C

 

505. consider the following program:

public class Test{

public static void main(String args[]){

boolean a=false;

if(a=true)

System.out.println("Hello");

else

System.out.println("Goodbye");

}

}

What is the result? Select the most appropriate answer.

A. Program produces no output but terminates correctly.

B. Program does not terminate

C. Prints out "Hello"

D. Prints out "Goodbye"

正确答案: C

 

506. Given the following code:

public void fun(){

int i;

try{

i=System.in.read();

System.out.println("Location 1");

} catch(IOException e){

System.out.println("Location 2");

} finally{

System.out.println("Location 3");

}

System.out.println("Location 4");

}

If an IOException occurs, what will be printed?

A. Location 1

B. Location 2

C. Location 3

D. Location 4

正确答案: BCD

 

507. If the method func() is allowed to throw out the IOException, which declaration of this method can used?

A. public int func(int i)

B. public int func(int i) throw IOException

C. public int func(int i) throw Exception

D. public int func(int i) throws IOException

E. public int func(int i) throws Exception

正确答案: DE

 

508. the following code:

class Test{

public static void main(String args[]){

try{

return;

}

catch(Exception e){

System.out.println("Exception");

}

finally{

System.out.println("Finally");

}

}

}

What will be the out put?

A. Compilation Error viod main Can't return any value.

B. Prints Exception.

C. Prints Exception Finally.

D. Prints Finally

正确答案: D

 

509. Give incompleted method:

1) {

2) if(unsafe())

3)  {//do something...}

4) else if (safe())

5)  {//do the other...}

6) }

The method unsafe() will throw an IOException, which completes the method of declaration when added at line one?

A. public IOException methodName()

B. public void methodName()

C. public void methodName() throw IOException

D. public void methodName() throws IOException

E. public void methodName() throws Exception

正确答案: DE

 

 

 

 

510. Give the following method:

public void example(){

try{

unsafe();

System.out.println("Test 1");

}catch(SafeException e){

System.out.println("Test 2");

}finally{

System.out.println("Test 3");

}

System.out.println("Test 4");

}

Which will display if method unsafe() run normally?

A. Test 1

B. Test 2

C. Test 3

D. Test 4

正确答案: ACD

 

 

 

511. What appears in the standard output if the method named problem() in the code below throws an instance of class Exception when the method named trythis() is invoked?

public void trythis(){

try{

System.out.println("1");

problem();

}catch(RuntimeException x){

System.out.println("2");

return;

}catch(Exception x){

System.out.println("3");

return;

}finally{

System.out.println("4");

}

System.out.println("5");

}

Select all valid answers.

A. "1"

B. "2"

C. "3"

D. "4"

E. "5"

正确答案: ACD

 

512. What deyword must appear in a method declaration(followed by the name of the exception )when that method might cause an exception to be thrown and that method does not handle the exception?

Fill in the blank

正确答案: throws

 

513. Analyze the following code.

void looper(){

int x=0;

one:

while(x<10){

two:     非法标号

System.out.println(++x);

if(x > 3)

break two;

}

}

Select all valid answer.

A. This code compiles

B. This code does not compile

C. This code writes the number 0 to the standard output

D. The numbers 1 and 2 to the standard outout

E. The numbers 3 and to the standard outout

F. The numbers 4 to the standard outout

G. The numbers 5 through 9 to the standard outout

H. The numbers 10 to the standard outout

正确答案: B

 

514. What appears in the standard output when the method named testing is invoked?

void testing(){

one:

two:

for(int i=0;i < 3;i++){

three:

for(int j = 10;j < 30; j+=10){

System.out.println(i+j);

if(i > 2)

continue one;

}

}

}

Select all valid answers.

A. 10 and 20

B. 11 and 21

C. 12 and 22

D. 13 and 23

E. 30,31,32,33

正确答案: ABC

 

515. What is written to the standard output as the result of executing the following statements?

Boolean b1 = new Boolean(true);

Boolean b2 = new Boolean(true);

Object obj1 = (Object)b1;

Object obj2 = (Object)b2;

if(obj1==obj2)

if (obj1.equals(obj2))

System.out.println("a");

else

System.out.println("b");

else

if(obj1.equals(obj2))

System.out.println("c");

else

System.out.println("d");

Select the one right answer.

A. a

B. b

C. c

D. d

正确答案: C

 

516. Which of the following statements about try, catch, and finally are true?

Select all valid answers.

A.  A try block must always be followed by a catch block

B.  A try block can be followed either by a catch block or a finally block, or both

C.  A catch block must always be associated with a try block

D.  A finally can never stand on its own (that is, without being associated with try block)

E.  None of these are true

正确答案: BCD

 

517. What will be printed out if you attempt to compile and run the following code?

int i=1;

switch(i){

case 0:

System.out.println("zero");

break;

case 1:

System.out.println("one")

Case 2:

System.out.ptintln("two")

default:

System.out.ptintln("default");

}

Select the one right answer.

A. one

B. one,default

C. one,two,default

D. default

正确答案: C

 

518. What will be printed out if you attempt to compile and run the following code?

int i=9;

switch(i){

default:

System.out.println("default");

case 0:

System.out.println("zero");

break;

case 1:

System.out.println("one");

case 2:

System.out.println("two");

Select the one right answer.

A. default

B. default, zero

C. error default clause not defined

D. no output displayed

正确答案: B

 

519. What will be output by the following code?

public class Test{

public static void main(String argv[]){

int i;

int j;

outer:

for (i=1; i < 3;i++){

if(j==2)continue outer;

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

}

}

}

Select the one right answer.

A. Value for i=1 value for j=1

B. Value for i=2 value for j=1

C. Value for i=2 value for j=2

D.编译错误

正确答案:D     

 没初使化

 

520.What code placed after the comment //For loop

would populate the elements of the array ia[] with values of the variable i?

public class Test{

public static void main(String argv[]){

Test 1=new Test();

1.amethod();

}

public void anethod(){

int ia[]=new int[4];

//Start For loop

{

ia[i]=i;

System.out.println(ia[i]);

}

}

}

Select the one right answer.

A. for(int i=0;i<ia.length()-1;i++)

B. for(int i=0;i<ia.length();i++)

C. for(int i=1;i<4.i++)

D. for(int i=0;i<ia.length;i++)

正确答案: D

 

521. Give the following code:

import java io.*;

public class Test{

public static void main(String argv[]){

Test t=new Test();

t.amethod();

}

public void amethod(){

try{

ioCall();

}catch(IOException ioe){}

}

}

What code would be most likely for the body of the ioCall method?

A. public void ioCall () throws IOException{

DataInputStream din=new DataInputStream(System.in);

din.readChar();

}

B. public void ioCall() throw IOException{

DataInputStream din=new DataInputStream(System.in);

din.readChar();

 }

C. public void ioCall (){

DataInputStream din=new DataInputStream(System.in);

din.readChar();

}

D. public void ioCall IOException(){

DataInputStream din=new DataInputStream(System.in);

din.readChar();

}

正确答案: A

 

522. What will happen when you attempt to compile and run the following code?

public class MySwitch{

public static void main(String argv[]){

MySwitch ms=new MySwitch();

ms.anethod();

}

public void anethod(){

int k=10;

switch(k){

//Put thedefault at the botton, not here

defauly

System.out.println("This is thedefault output");

break;

case 10:

System.out.println("ten");

case 20:

System.out.println("twenty");

break;

}

}

}

Select the one reght answer.

A. None of these options.

B. Compile time error target of switch must be an integral type.

C. Compile and run with output "This is the default output".

D. Compile and run with output of the single line "ten".

正确答案: D

 

523. What will happen when you attempt to compile and run the following code?

public class Test{

static public long i=10;

public static void main(String argv[]){

switch(i){

default:

System.out.println("no value given");

case 1:

System.out.println("one");

case 10:

System.out.println("ten");

case 5):

System.out.println("five");

}

}

}

Select the one right answer.

A. Compile time error

B. Output of "ten"followed by "five"

C. Output of "ten"

D. Compilation and run time error because of location of default

正确答案: A

 

524. Given the following code:

import java io*;

public class Test{

public static void main(String argv[]){

Test P=new Test();

p.fliton();

}

public int fliton(){

try{

FileInputStream din=new FileInputStream("Ttst.java");

din.read();

}catch(IOException ioe){

System.out.println("flytwick");

return 99;

}finally{

System.out.println("fliton");

}

return -1;

}

}

Assuming the Test.java is available to be read which of the following statements are true if you try to compile and run the program?

A. The program will run and output only "flytwick".

B. The program will run and output only "fliton".

C. The program will run and output both "fliton"and"flytwick".

D. The error will occur at compile time because the method fliton attempts to return two values.

正确答案: B

 

525. The thread run() method has the following code, what is the result when the thread runs?

try{

sleep(200); 不会扔出一个IOException

System.out.println("Printing from thread run()method");

}catch(IOException ie){}

Selet the one right answer.

A. Compile time error

B. Prints on the console Printing from thread run() method

C. At line 2 the thread will be stop running and resumes after 200 milliseconds and prints Printing from thread run() method

D. At line 2 the thread will be stop running and resumes exactly 200 milliseconds elapsed

正确答案: A

 

526. Please select the correct answer from the following?

public class Test{

static void throwMethod() throws Exception{

System.out.println("Inside throwMethod.");

throw new IllegalAccessException("demo");

}

public statle void main(String args[]){

try{

throwMethod();

}catch(illegalAccessException e){

System.out.println("Caught"+e);

}

}

}

Select the one right answer.

A. Compilation error

B. Runtime error

C. Compile successfully, nothing is printed

D. inside throwMethod. followed by caught:java.lang.IllegalAccessException:demo

正确答案: d

 

527. What is the output when you execute the following code?

int i=100;

switch(i){

case 100:System.out.println(i);

case 200:System.out.println(i);

case 300:System.out.println(i);

}

Select the one right answer.

A. Nothing is printed

B. Compile time error

C. The values 100,100,100 printed

D. Only 100 is Printed

正确答案: C

 

528. How can you change the break statement below so that it breaks out if the inner and middle loops and continues with the next iteration of the outer loop?

outer: for(int x=0;x < 3; x++){

 middle: for(int y=0;y<3;y++){

if (y==1){

break;

}

}

}

Select the one right answer.

A. continue middle;

B. break middle;

C. break outer;

D. continue

正确答案: B

 

529. What is the result when you compile and run the following code?

public class Test{

static void throwMethod(){

System.out.println("Inside throw Method.");

throw new IllegalAccessException("demo");

}

public static void main(String args[]){

try{

throwMethod();

}

catch(IllegallAccessException e){

System.out.Println("Caught"+e);

}

}

}

Select the one right answer.

A. Compilation error

B. Runtime error

C. Compile successfully, nothing is printed

D. Inside throwMethod. followed by caught: java.lang.IllegalAccessExcption:demo

正确答案: A

 

530. Given the following code:

if(x > 0){

System.out.println("first");}

else

if(x > -3){

System.out.println("second");}

else{

System.out.println("third");}

Which range of x value would print the string "second"?

A. x>0

B. x>-3

C. x<=-3

D. x<=0 & x>-3

正确答案: D

 

531. Given the following code:

public void test(){

try {

oneMethod();

System.out.println("condition 1");

} catch(ArrayIndexOutOfBoundsException e){

System.out.println("condition 2");

}

catch(Exception e){

System.out.println("condition 3");

}finally{

System.out.println("finally");

 }

}

Which will display if oneMethod run normally?

A. condition 1

B. condition 2

C. condition 3

D. finally

正确答案: AD

 

 

 

 

 

532. Given the following code:

public class Test{

void printValue(int m){

do {

System.out.println("The value is "+m);

}

While(--m > 10)

}

public static void main(String arg[]){

int i=10

Test t=newTest();

t.printValue(i);

}

}

Which will be output?

A. The value is 8

B. The value is 9

C. The value is 10

D. The value is 11

正确答案: C

 

533. Given the following code fragment:

1) switch(m){

2) case 0:System.out.println("case 0");

3) case 1:System.out.println("case 1");break;

4) case 2:

5) defarlt:System.out.println("default");

6) }

Which value of m would cause "default" to be the output?

A. 0

B. 1

C. 2

D. 3

正确答案: CD

 

534. Given the incompleted method:

//line 1

{

success = connect();

if (success==-1){

throw new TimedOutException();

}

}

TimedOutException is not a RuntimeException. Which can complete the method of declaration when added at line 1?

A. public void method()

B. public void method() throws Exception

C. public void method() throws TimedOutException

D. public void method() throw TimedOutException

E. public throw TimedOutException viod method()

正确答案: BC

 

535. Which of the following are acceptable to the Java compiler.

Select all correct avswers.

A. if(2==3)System.out.println("Hi");

B. if(2=3)System.out.println("Hi");

C. if(true)System.out.println("Hi");

D. if(2!=3)System.out.println("Hi");

E. if(aString.equals("hello"))System.out.println("Hi");

正确答案:ACDE

 

536. Assuming a method contains code which may raise an Exception (but not a RuntimeException), what is the correct way for a method to indicate that it expects the caller to handle that exception:

Select the most appropriate answer.

A. throw Exception

B. throws Exception

C. new Exception

D. Don't need to specify anything

正确答案:B

 

537. What is the result of executing the following code, using the parameters 4 and 0:

public void divide(int a, int b){

try{

int c=a/b;

}catch(Exception e){

System.out.ptint("Exception");

}finally{

System.out.println("Finally");

}

Select the most appropriate answer.

A. Prints out: Exception Finally

B. Prints out: Finally

C. Prints out: Exception

D. No output

正确答案:A

 

 

 

538. Given the following code what is the effect of a being 5:

public class Test{

public void add(int a){

loop:for(int i= 1; i < 3; i++){

for(int j = 1;j < 3; j++){

if(a==5){

break loop;

}

System.out.println(i*j);

}

}

}

}

Select the most appropriate answer.

A. Generate a runtime error

B. Throw an ArrayIndexOutOfBoundsException

C. Print the values: 1,2,3,4

D. Produces no output

正确答案:D

 

539. What is the result of executing the following code when the value of x is 2?

switch(x){

case 1:

System.out.println(1);

case 2:

case 3:

System.out.println(3);

case 4:

System.out.println(4);

}

Select the most appropriate answer.

A. Nothing is printed out

B. The value 3 is printed out

C. The values 3 and 4 are printed out

D. The values 1,3 and 4 are printed out

正确答案:C

 

540. What is the result of executing the following fragment of code:

boolean flag=false;

if(flag=true){

System.out.println("true");

}else{

System.out.println("false");

}

Select the most appropriate(适合的) answer.

A. true is printed to standard out.

B. false is printed to standard out.

C. An exception is raised

D. Nothing happens

正确答案:A

 

541. Examine the following code:

public class Test{

public static void main(String args[]){

int total=0;                 循环无法执行

for(int i=0,j=10;total>30;++i,--j){

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

total+=(i+j);

}

System.out.println("Total"+total);

 }

}

Select the most appropriate answer.

A. Produce a runtime error

B. Produce a compile time error

C. Print out "Total 0"

D. Gemerate the following as output:

正确答案:C

 

542. Given the following code:

class Test{

public static void main(String args[]){

System.out.println(hai());

}

static int hai(){

try{

return 1;

}

catch(Exception e){

System.out.println("Exception");

}

finally{

System.out.println("Finally");

Return  2;

}

}

}

What will be the out put?

A. Compilation Error void main Can't return any value.

B. Prints Exception.

C. Prints Exception Finally.

D. Prints Finally and 1.

E. Prints Finally and 2.

正确答案:E

 

 

 

 

543. Given the following code:

public static void main(String args[]){

try{

System.out.println("1");

}catch(RuntimeException x){

System.out.println("2");

return:

}catch(Exception x){

System.out.println("3");

return;

}finally{

System.out.println("4");

}

System.out.println("5");

}

If there is no Exception in the try block, what will be the output? Select all that apply.

A. 1

B. 2

C. 3

D. 4

E. 5

正确答案:ADE

 

544. Given the following code:

class Test{

public static void main(String args[]){

try{

mymethod();

}catch(Exception e){

System.out.println("Exception");

}

}

}

How will you declare mymethod() so that it will throw a Exception? Select one?

A. In method declaration give throws Exception and inside the method give throw Exception.

B. In method declaration give throw Exception and inside the method give throws Exception.

C. In method declaration give throw Exception and inside the method give throw Exception.

D. In method declaration give throws Exception and inside the method give throws Exception.

正确答案:A

 

545. Given the following code:

class Test{

public static void main(String args[]){

try{

//ProtocoIException

}catch(IOException e){

System.out.println("I");

}catch(IOException e){

System.out.println("E");

}finally{

System.out.println("F");

}

}

}

If a ProtocoIException occurs in try block. what will be the output? Select all.

A. I

B. E

C. F

D. Compilation Error says that there is no matching catch block

正确答案:AC

 

546. Given the following code:

public static void main(String args[]){

int a=10;

int b=20;

if(a=b)

System.out.println("Not Equal");

else

System.out.println("Equal");

}

What will be the output?

A. Equal

B. Not Equal

C. Compilation Error

D. An Exception will occurs

正确答案:C

 

547. Given the following code:

public static void main(String args[]){

int t=0;

while(1){

if(t++<10)

break;

}

}

What will be the value of t after the while loop?

A. 11

B. 9

C. 10

D. Compilation Error

E. An Exception will occcurs

正确答案:D

 

548. If a method throws an ProtocoIException(subclass of IOException) which of the following can be used in declaring that method?

A. public void method() throws Exception

B. public void method() throw Exception

C. public throws IOException void method()

D. public void method() throws IOException

E. public void method() throws ProtocoIException

正确答案:ADE

 

549. What is the output when following code is run?

1. class Test{

2.  public static void main(String args[]){

3.   int i=0,j=5;

4.   st: for(;;i++){

5.     for(;;j--){

6.      if(i>j)

7.       break st;

8.     }

9.    }

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

11.  }

12.}

Select the one right answer.

A. i=0 j=-1

B. i=1 j=0

C. i=0 j-1

D. i=1 j=-1

E. Compiler error at line 4.

正确答案:A

 

550. What will be the output when following code is run?

1. class Test{

2.  static void foo() throws Exception{

3.   throw new Exception();

4.  }

5.  pubilc static void main(String args[]){

6.   try{

7.    foo();

8.   }catch(Exception e){

9.    System.exit(0);

10.    }

11.    finally{

12.   System.out.println("Infinally");

13.  }

14.  }

15. }

Select the one right answer.

A. Compiler error.

B. Code will compile fine and when run prints Infinally.

C. Nothing will be printed.

D. Runtime exeption.

正确答案:B

 

 
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:SCJP考题4
举报原因:
原因补充:

(最多只允许输入30个字)