笔试之SCJP(4)

来源:互联网 发布:软件项目度量指标 编辑:程序博客网 时间:2024/06/11 06:27

 

1、 

 

Given:
10. public Object m() {
11. Object o = new Float(3.14F);
12. Object [] oa = new Object[1];
13. oa[0] = o;
14. o = null;
15. oa[0] = null;
16. print 'return 0';
17. }
When is the Float object, created in line 11, eligible forgarbage collection?
 
A. Just after line 13.
B. Just after line 14.
C. Just after line 15.
D. Just after line 16 (thatis, as the method returns).
 
 
2、
Given:
1. public class Test {
2. public static void main(String[] args) {
3. int x = 0;
4. assert (x > 0): "assertion failed";
5. System.out.println("finished");
6. }
7. }
What is the result?
 
A. finished
B. Compilation fails.
C. An AssertionError is thrown.
D. An AssertionError is thrown and finished is output.
 
解析: 此问题有个陷阱, 对于断言我们首先要看其在程序中是否已被启用。 如果启用的话那么此题应该选择C。 但是默认的为不启用, 因此选择A
如果第4行写成:

 assert (x > 0) ? "assertion failed" : "assertion passed"; 那么就会编译报错, 因为断言并没有此种形式

 
 
3、
Given:
1. public class X {
2. public static void main(String [] args) {
3. try {
4. badMethod();
5. System.out.print("A");
6. }
7. catch (RuntimeException ex) {
8. System.out.print("B");
9. }
10. catch (Exception ex1) {
11. System.out.print("C");
12. }
13. finally {
14. System.out.print("D");
15. }
16. System.out.print("E");
17. }
18. public static void badMethod() {
19. throw new RuntimeException();
20. }
21. }
What is the result?
 
A. BD B. BCD C. BDE
D. BCDE E. ABCDE

F. Compilation fails.

 

4、

Given:

1. public class OuterClass {

2. private double d1 = 1.0;

3. // insert code here

4. }

Which two are valid if inserted at line 3? (Choose two)

 

A. static class InnerOne {

public double methoda() { return d1; }

}

B. static class InnerOne {

static double methoda() { return d1; }

}

 

 C. private class InnerOne {

public double methoda() { return d1; }

}

D. protected class InnerOne {

static double methoda() { return d1; }

}

E. public abstract class InnerOne {

public abstract double methoda();

}

关于内部类的问题一直没有搞得很明白, 参考《Thinking in java》!

 

  5、

Given:

1. public class Foo {

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

3. System.out.println( "Hello" + args[0] );

4. }

5. }

What is the result if this code is executed with the command line?

java Foo world

 

A. Hello

B. Hello Foo

C. Hello world

D. Compilation fails.

E. The code does not run.

解析:  要先编译,也即要先使用javac命令才可以。

补充: *String[] args: args是“参数”的缩写,可以改成任意的名字。
                       args存贮的是命令行参数,可用于程序中。
 *支持从命令行输入参数:
             String[] args这个字符串数组是保存运行main函数时输入的参数 的,例如 main函数所在的类名为test那么你在cmd运行java test a b c时
             args[0]=a,args[1]=b,args[2]=c,你就可以在你的程序中调用你输入的这些变量。

   当然也可以在IDE中进行次试验, 如果IDE是eclipse,那么你只要在运行的时候点右键,选open  run dialog,然后选arguments选项卡,然后在program arguments中输入两个参数就行了。两个参数间用空格隔开就行了 。

原创粉丝点击