J2SE综合测试题一及答案

匿名网友 匿名网友 发布于: 2015-08-30 00:00:00
阅读 121 收藏 0 点赞 0 评论 0

 

本试卷共计40题,每题可能有一个或多个答案。

 

  1. 下面哪些是合法的标识符?

 

A. BigOlLongStringWithMeaninglessName

B. $int

C. bytes

D. $1

E. final

F. _666888

 

  1. 考虑下面的语句:

Object[] x = new Object[25];

当运行后,下面的哪些说法是正确的?    

A. x[24] 0

B. x[2] = new String(“test”); x[3] = new Box(1,2,3);

C. x[25] 0

D. x[0] null

E. x.length 25

 

  1. 考虑下面的代码:

1. class Q6 {

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

3.         Holder h = new Holder();

4.         h.held = 100;

5.         h.bump(h);

6.         System.out.println(h.held);

7.     }

8. }

9.

10. class Holder {

11.         public int held;

12.         public void bump(Holder theHolder) {

            theHolder = new Holder();

     theHolder.held = 0;

13.             theHolder.held++;

14.         }

15. }

在第 6行打印的值是:

A. 0

B. 1

C. 100

D. 101

 

  1. 下面哪些表达式是合法的?

    A. int x = 6; x = !x;

    B. int x = 6; if (!(x > 3)) {}

    C. int x = 6; x = ~x;

    D. byte b = 256 – 128;

    E. byte b = 25; byte c = b >> 1;

 

  1. 下面哪些代码片段运行时打印出 “Equal”
    1. int x = 100; float y = 100.0F;if (x == y){

System.out.println(“Equal”);}

B. int x = 100; Integer y = new Integer(100);if (x == y)

{ System.out.println(“Equal”);}

C. Integer x = new Integer(100);byte b = 100;Byte y = new

Byte(b);if (x == y) {

System.out.println(“Equal”);}

D. String x = new String(“100”);String y = new

String(“100”);if (x == y) {

System.out.println(“Equal”);}

E. String x = “100”;String y = “100”;if (x == y) {

System.out.println(“Equal”);}

 

  1. 运行下面的代码,打印的结果是:

    1. public class Xor {

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

    3.         byte b = 19;

    4.         byte c = 15;

    5.         b = (byte)(b ^ c);

    6.         System.out.println(“b contains ” + b);

    7.     }

    8. }

 

A. b contains -30

B. b contains 15

C. b contains 245

D. b contains 12

 

  1. 下面哪些声明是合法的?

    A. default String s;

    B. transient int i = 41;

    C. public final static native int w();

    D. abstract double d;

    E. abstract final double hyperbolicCosine();

    F. private abstract class A{};

    G. public class B<T extends Comparable<? super T>>{};

 

  1. 关于下面的程序,哪些说法是正确的?

    1. class StaticStuff

    2 {

    3.     static int x = 10;

    4.

    5.     static { x += 5; }

    6.

    7. public static void main(String args[])

    8. {

    9.     System.out.println(“x = ” + StaticStuff .x);

    10. }

    11.    static

    12.     {x /= 3; }

    13.}

 

A. 5行和12行不能编译,因为该方法没有方法名和返回值。

B. 12 行不能编译,因为只能有一个static初始化块。

C. 代码编译并执行,输出结果x = 10.

D. 代码编译并执行,输出结果 x = 5.

E. 代码编译并执行,输出结果 x = 15.

 

  1. 下面哪些说法是正确的?

    A. 只有简单数据类型能自动转换。改变对象引用类型必须使用类型转换。

    B. 只有对象引用类型能自动转换。改变简单数据类型必须使用类型转换。

    C. 对象引用类型的算术提升需要显式类型转换。

    D. 简单数据类型和对象引用类型都能进行类型转换。

    E. 数字类型的类型转换需要运行时检查。

 

  1. 下面的哪行代码会产生编译错误?

    1. byte b = 58;

    2. char c = ‘5’;

    3. short s = 55;

    4. int i = 555;

    5. float f = 555.5f;

    6. b = s;

    7. i = c;

    8. if (f > b)

    9. f = i;

    10. byte d =128;

    11. Integer iw = i;

 

  1. 下面的代码会编译成功吗?

    1. byte b = 2;

    2. byte b1 = 3;

    3. b = b * b1;

 

A. Yes

B. No

 

  1. 下面哪些说法是正确的?

    A. 对象引用变量可以在赋值时转换,但不能在方法调用时转换。

    B. 对象引用变量可以在方法调用时转换,但不能在赋值时转换。

    C. 对象引用变量可以在方法调用和赋值时转换,但是两者转换的规则不同。

    D. 对象引用变量可以在方法调用和赋值时转换,而且两者转换的规则相同。

    E. 对象引用变量不能被转换。

    F. 对象引用变量既可以是局部变量也可以是实例变量。

 

13,14的类层次图:

    

 

  1. 考虑下面的代码:

    1. Dog rover, fido;

    2. Animal anim;

    3.

    4. rover = new Dog();

    5. anim = rover;

    6. fido = (Dog)anim;

     

下面的哪个说法是正确的?

A. 5行不能编译。

B. 6行不能编译。

C. 代码编译成功,但是在第6行将抛出异常。

D. 代码编译成功并运行。

E. 代码编译成功并运行,但是第6行的类型转换不需要并可以去掉。

 

  1. 考虑下面的代码:

    1. Raccoon rocky;

    2. SwampThing pogo;

    3. Washer w;

    4.

    5. rocky = new Raccoon();

    6. w = rocky;

    7. pogo = w;

    下面的哪个说法是正确的?    

    A. 6行不能编译;必须有一个显式的强制类型转换来将Raccoon转换为Washer

    B. 7行不能编译;必须有一个显式的强制类型转换来将Washer转换为SwampThing

    C. 代码编译成功,但是在第7行将抛出异常,因为在运行时将接口转换为类是不允许的。

    D. 代码编译成功,但是在第7行将抛出异常,因为在运行时类W不能转换为类型SwampThing

 

  1. 考虑下面的代码:

    1. outer: for (int i = 0; i < 2; i++) {

    2.     for (int j = 0; j < 3; j++) {

    3.         if (i == j) {

    4.             continue outer;

    5.         }

    6.         System.out.println(“i = ” + i + ” j = ” + j);

    7.     }

    8. }

    下面哪些行是输出的部分?

    

A. i = 0 j = 0

B. i = 0 j = 1

C. i = 0 j = 2

D. i = 1 j = 0

E. i = 1 j = 1

F. i = 1 j = 2

 

  1. 关于下面的代码段,哪个说法是正确的?

    1. int j = 2;

    2. switch (j) {

    3.     case 2:

    4.         System.out.println(“value is two”);

    5.     case 2 + 1:

    6.         System.out.println(“value is three”);

    7.         break;

    8.     default:

    9.         System.out.println(“value is ” + j);

    10.         break;

    11. }

        

A. 5行非法。

B. switch()中的j 的类型可以是byte, short, int, long

C. 输出结果是: value is two.

D. 输出结果是: value is two value is three.

E. 输出结果是: value is two, value is three, value is 2.

 

  1. 考虑下马的类图和代码段:


1. try {

2.     // assume s is previously defined

3.     URL u = new URL(s); //

4.     // in is an ObjectInputStream

5.     Object o = in.readObject();

6.     System.out.println(“Success”);

7. }

8. catch (MalformedURLException e) {

9.     System.out.println(“Bad URL”);

10. }

11. catch (StreamCorruptedException e) {

12.     System.out.println(“Bad file contents”);

13. }

14. catch (Exception e) {

15.     System.out.println(“General exception”);

16. }

17. finally {

18.      System.out.println(“Doing finally part”);

19. }

20. System.out.println(“Carrying on”);

 

如果第3行的构造函数抛出 MalformedURLException,下面哪些是输出的内容?

A. Success

B. Bad URL

C. Bad file contents

D. General exception

E. Doing finally part

F. Carrying on

 

  1. 考虑下面的类图和代码段:


1. try {

2.     // assume s is previously defined

3.     URL u = new URL(s);

4.     // in is an ObjectInputStream

5.     Object o = in.readObject();

6.     System.out.println(“Success”);

7. }

8. catch (MalformedURLException e) {

9.     System.out.println(“Bad URL”);

10. }

11. catch (StreamCorruptedException e) {

12.     System.out.println(“Bad file contents”);

13. }

14. catch (Exception e) {

15.     System.out.println(“General exception”);

16. }

17. finally {

18.     System.out.println(“Doing finally part”);

19. }

20. System.out.println(“Carrying on”);

 

如果第5行的方法抛出异常OutOfMemoryError,下面哪些是输出?

A. Success

B. Bad URL

C. Bad file contents

D. General exception

E. Doing finally part

F. Carrying on

 

  1. 下面的哪个代码段显示了最合适的抛出异常的方式?假设所有的变量都在其它地方声明,并且在有效的作用域内,且有合适的值。

    A.

    1. Exception e = new IOException(“File not found”);

    2. if (!f.exists()) { // f is a File object

    3.     throw e;

    4. }

    B.

    1. if (!f.exists()) { // f is a File object

2. throw new IOException(“File ” + f.getName() + “not found”);

3. }

C.

1. if (!f.exists()) {

2. throw IOException;

3. }

D.

1. if (!f.exists()) {

2. throw “File not found”;

3. }

E.

1. if (!f.exists()) { // f is a File object

2. throw new IOException();

3. }

 

  1. 考虑下面的类:

1. public class Test1 {

2. public float aMethod(float a, float b) {

3. }

4.

5. }

如果在第4行单独添加下面的某个方法,那么哪些方法是合法的?

A. public int aMethod(int a, int b) { }

B. public float aMethod(float a, float b) { }

C. public float aMethod(float a, float b, int c) throws Exception{}

D. public float aMethod(float c, float d) { }

E. private float aMethod(int a, int b, int c) { }

 

  1. 考虑下面声明在不同源文件中的类:

1. public class Base {

2. public void method(int i) {

3. System.out.println(“Value is ” + i);

4. }

5. }

1. public class Sub extends Base {

2. public void method(int j) {

3. System.out.println(“This value is ” + j);

4. }

5. public void method(String s) {

6. System.out.println(“I was passed ” + s);

7. }

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

9. Base b1 = new Base();

10. Base b2 = new Sub();

11. b1.method(5);

12. b2.method(6);

13. }

14. }

 

当运行Submain方法,将输出什么?

A. Value is 5 Value is 6

B. This value is 5 This value is 6

C. Value is 5 This value is 6

D. This value is 5 Value is 6

E. I was passed 5 I was passed 6

 

  1. 考虑如下类的定义:

1. public class Test extends Base {

2. public Test(int j) {

3. }

4. public Test(int j, int k) {

5. super(j, k);

6. }

7. }

下面哪些调用是合法的?

 

A. Test t = new Test();

B. Test t = new Test(1);

C. Test t = new Test(1, 2);

D. Test t = new Test(1, 2, 3);

E. Test t = (new Base()).new Test(1);

 

  1. 考虑下面类的定义:

1. public class Test extends Base {

2. public Test(int j) {

3. }

4. public Test(int j, int k) {

5. super(j, k);

6. }

7. }

下面哪个构造函数必须显示地在Base类中定义?假定Test Base在同一个包中。

A. Base() { }

B. Base(int j) { }

C. Base(int j, int k) { }

D. Base(int j, int k, int l) { }

 

  1. 考虑如下的定义:

1. public class Outer {

2. public int a = 1;

3. private int b = 2;

4. public void method(final int c) {

5. int d = 3;

6. class Inner {

7. private void iMethod(int e) {

8.     System.out.println(?);

9. }

10. }

11. }

12. }

在第8行哪些变量可以正确地被引用?

A. a

B. b

C. c

D. d

E. e

 

  1. 对字符串s = new String(“xyzzy”), 下面哪个调用会修改s字符串?

A. s.append(“aaa”);

B. s = s.trim();

C. s = s.substring(3);

D. s = s.replace(‘z’, ‘a’);

E. s.concat(s);

F. 上面的答案都不正确

 

  1. 下面的哪个说法正确?

1. String s1 = “abc” + “def”;

2. String s2 = new String(s1);

3String s3 = “abc” + “def”;

4. if (s1 == s3)

5. System.out.println(“== succeeded”);

6. if (s1.equals(s2))

7. System.out.println(“.equals() succeeded”);

 

A. 5 和第7行都执行。

B. 5 执行,但是第7 行不执行。

C. 7行执行,但第5行不执行。.

D. 5行和第7行都不执行。

 

  1. 对下面的代码,哪个说法正确?

1. StringBuffer sbuf = new StringBuffer(“abcde”);

2. StringBuffer sbuf2 = sbuf.insert(3, “xyz”);

 

A. sbuf == sbuf2

B. sbuf != sbuf2

 

  1. 关于类路径(classpath)下面说法正确的是:

1.运行Java程序时如果用到了其它目录中的类必须指定类路径,或者在环境变量中设置类路径。

2class文件的类路径由文件所在的目录和文件名组成。

3class文件的类路径由该类所在的包对应的目录结构的第一层目录的父目录组成。

4jar文件的类路径由该文件所在的目录和文件名组成。

 

  1. 下列说法正确的是:

1. 当一个对象不再有引用变量引用它时,那么垃圾收集器将会回收它。

2. 对象是在内存的堆中分配的。

3. 实例变量在堆中分配,而局部变量在栈中分配。

4. 静态变量在类装载时初始化。

5
不能对泛型类中的参数类型实例化。

 

  1. 下面的程序输出结果是:

public
class C1 {


public
void test1() {

System.out.println(“C1:test1”);

}

 


public
void test2() {

System.out.println(“C1:test2”);

}

 


public
static
void test3() {

System.out.println(“C1:test3”);

}

 


public
static
void main(String[] args) {

C1 c1 = new C3();

c1.test1();

c1.test2();

c1.test3();

}

}

 

class C2 extends C1 {


public
void test2() {

System.out.println(“C2:test2”);

}

}

 

class C3 extends C2 {


public
void test1() {

System.out.println(“C3:test1”);

}

 


public
static
void test3() {

System.out.println(“C3:test3”);

}

}

 

A.     

C3:test1

C2:test2

C1:test3

B.

C1:test1

C2:test2

C1:test3

C.

C3:test1

C1:test2

C3:test3

D.

C1:test1

C2:test2

C3:test3

E.

C1:test1

C1:test2

C1:test3

 

  1. 下列代码的输出结果是

    public class Static {

        static {

            int x = 5;

        }

     

        static int x, y;

     

        public static void main(String args[]) {

            x–;

            myMethod();

            System.out.println(x + y + ++x);

        }

     

        public static void myMethod() {

            y = x++ + ++x;

        }

    }

     

  2. 下列代码的输出结果是

public class Value {

    public int i = 15;

}

 

public class Test {

    public static void main(String argv[]) {

        Test t = new Test();

        t.first();

    }

 

    public void first() {

        int i = 5;

        Value v = new Value();

        v.i = 25;

        second(v, i);

        System.out.println(v.i);

    }

 

    public void second(Value v, int i) {

        i = 0;

        v.i = 20;

        Value val = new Value();

        v = val;

        System.out.println(v.i + ” ” + i);

    }

}

 

  1. 下列代码的输出结果是

class MyParent {

    int x, y;

 

    MyParent(int x, int y) {

        this.x = x;

        this.y = y;

    }

 

    public int addMe(int x, int y) {

        return this.x + x + y + this.y;

    }

 

    public int addMe(MyParent myPar) {

        return addMe(myPar.x, myPar.y);

    }

}

 

class MyChild extends MyParent {

    int z;

 

    MyChild(int x, int y, int z) {

        super(x, y);

        this.z = z;

    }

 

    public int addMe(int x, int y, int z) {

        return this.x + x + this.y + y + this.z + z;

    }

 

    public int addMe(MyChild myChi) {

        return addMe(myChi.x, myChi.y, myChi.z);

    }

 

    public int addMe(int x, int y) {

        return this.x + x + this.y + y;

    }

}

 

public class MySomeOne {

    public static void main(String args[]) {

        MyChild myChi = new MyChild(10, 20, 30);

        MyParent myPar = new MyParent(10, 20);

        int x = myChi.addMe(10, 20, 30);

        int y = myChi.addMe(myChi);

        int z = myPar.addMe(myPar);

        System.out.println(x + y + z);

    }

}

 

  1. 运行下列方法产生的结果是

public void method1() {

        boolean a = true;

        boolean b = false;

        boolean c = true;

        if (a == true)

            if (b == true)

                if (c == true)

                    System.out.println(“Some things are true in this world”);

                else

                    System.out.println(“Nothing is true in this world!”);

            else if (a && (b = c))

                System.out

                        .println(“It’s too confusing to tell what is true and what is false”);

            else

                System.out.println(“Hey this won’t compile”);

    }

A.The code won’t compile.

B.”some things are true in this world” will be printed

C.”hey this won’t compile” will be printed

D.None of these

 

  1. 下列说法正确的是?

A. 枚举既不能是其它类的超类,也不能是其它类的子类。

B. 枚举中的常量是静态的,因此每个枚举常量只实例化一次,实例化时调用枚举中的构造函数。

C.运行时系统和应用程序都可以产生异常。

D. Java虚拟机为每个类型创建唯一的Class对象

E. 方法重载、方法覆盖和接口都实现了java的多态性。

F. 如果要生成内部类实例,那么必须先生成外部类的实例。

 

  1. 哪两个表达式的运算结果是相等的?

A. 32 / 4;

B. (8 >> 2) << 4;

C. 2 ^ 5;

D. 128 >>> 2;

E. (2 << 1) * (32 >> 3);

F. 2 >> 5;

 

  1. 下列代码的运算结果是

1. class Test {

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

3. int x= 0;

4. int y= 0;

5. for (int z = 0; z < 5; z++) {

6. if (( ++x > 2 ) && (++y > 2)) {

7. x++;

8. }

9. }

10. System.out.println(x + ” ” + y);

11. }

12. }

 

  1. 下列代码的运算结果

    1. public class If2 {

    2. static boolean b1, b2;

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

    4. int x = 0;

    5. if ( !b1 ) {

    6. if ( !b2 ) {

    7. b1 = true;

    8. x++;

    9. if ( 5 > 6 ) {

    10. x++;

    11. }

    12. if ( !b1 ) x = x + 10;

    13. else if ( b2 = true ) x = x + 100;

    14. else if ( b1 | b2 ) x = x + 1000;

    15. }

    16. }

    17. System.out.println(x);

    18. }

    19. }

     

  2. 关于构造函数的说法正确的是:

    A. 如果超类的构造函数没有参数,那么子类的构造函数也不能有参数。

    B. 构造函数不能继承。

    C. 构造函数不能被重载。

    D. 构造函数的第一条语句调用super()或 this()方法是合法的。

     

  3. 给出下面的代码:

    1. import java.util.*;

    2. class Ro {

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

    4. Ro r = new Ro();

    5. Object o = r.test();

    6. }

    7.

    8. Object test() {

    9.

    10.

    11. }

    12. }

下面哪些语句插入到9/10行会有编译错误?

A. char [] [] c = new char [2][2];

return c;

B. return (Object) 7;

C. return (Object) (new int [] {1,2,3} );

D. ArrayList a = new ArrayList();

return a;

E. return (Object) “test”;

F. return (Float) 4.3;

答案:

1: A B C D F

2: B D E

3: C

4: B C

5: A B E

6: A

7: B C G

8: D

9: D

10: 6 10

11: B

12: D F

13: D

14: B

15: D

16: D

17: B E F

18: E

19: B

20: A C E

21: C

22: B C

23: A C

24: A B C E

25: F

26: A

27: A

28: 1 3 4

29: 1 2 3 4 5

30: A

31: 3

32: 15 0 20

33: 300

34: D

35: A B C D E F

36: B D

37: 6 3

38: 101

39: B D

40: F

评论列表
文章目录