java2习题200道及答案 下载本文

Java 程序设计习题

单项选择题(80)

1. 接口中的成员变量被隐含地声明为 (A)public static final (B)public final (C)public static (D)public abstract

2. 处理按钮点击事件的类需要实现哪个接口 (A)FocusListener (B)ActionListener (C)WindowListener (D)ItemListener

3. 已知:

class A {

public final void m() {

System.out.println(“A.m”); }

}

public class B extends A { public void m() {

System.out.println(“B.m”); super.m(); }

public static void main(String [] args) { new B().m(); } }

那么结果为? (A)A.m (B)B.m (C)B.m A.m (D)编译错误

4. 抛出异常,使用关键字 (A)try (B)throw (C)finally (D)catch

5. 已知:

class Base {

public Base(String s) {

System.out.print(“B”); } }

public class Derived extends Base { public Derived (String s) { System.out.print(“D”); }

public static void main(String [] args) { new Derived (“C”); } }

那么结果为? (A)BD (B)DB (C)C

(D)编译错误

6. Java 中char类型的数据长度为 (A)8位 (B)16位 (C)32位 (D)64位

7. Java 编译输出文件的后缀是 (A).exe (B).java (C).class (D).obj

8. 已知: class C {

public static void main(String[] args) { boolean a = true; boolean b = false; boolean c = true;

if((a & b) | (b & c) & c) System.out.print(“A”); If((a = false) | (a & c) ) | (a | b)) System.out.print(“B”); } }

(A)A (B)B (C)AB

(D)编译错误

9. 下面哪个表达式是错误的 (A)int i = 100;

(B)float f = 100.0;

(C)Object o = “Hello world”; (D)char c = ?\??;

10. 引用类型的数据字段的缺省值为 (A)0.0 (B)0 (C)null (D)false

11. 下面哪个表达式可以获取字符串 s 的最后一个字符 (A)s.length()

(B)s[s.length() - 1]

(C)s.charAt(s.length() - 1) (D)charAt(s, length(s))

12. 数据字段的作用域是 (A)所在的语句块 (B)所在的类 (C)所在的方法 (D)所在的构造函数

13. 如果类中的成员只能被子类或同一包中的类访问,那么声明该成员时应使用下面哪个修饰符

(A)private (B)package (C)protected (D)public

14. import语句的作用是 (A)导入包中成员 (B)创建包

(C)既可导入包成员,也可创建包 (D)访问控制

15. 已知: class C {

public static void main(String[] args) { int a = 1; int b = 2;

System.out.print( “7” + 5 + 4); System.out.print(m() + a + 3); System.out.println(a + b + m()); }

public static String m() { return “6”; }

}

(A) 79613126 (B) 79614336 (C) 75461336 (D) 754613126

16. 接口中的成员方法被隐含地声明为 (A)public static final (B)protected abstact (C)private

(D)public abstract

17. 关于包的描述错误的是

(A)包可以将相关的类和接口组织在一起,便于识别和管理 (B)包中的类可以访问同一包的其它类的私有成员 (C)不同包中的相同命名,不会产生冲突

(D)用public 修饰的顶级类和接口对包外代码可见

18. 下面代码的输出是 class C {

public static void main(String[] args) { int x = 1;

System.out.print((x > 1) & (x++ > 1)); System.out.print( ““);

System.out.print((x > 1) && (x++ > 1)); }

}

(A)false true (B)true false (C)false false (D)true true

19. float[] f = new float[10],则f[0]的值为 (A)0.0

(B)0.0f (C)null (D)false

20. 下面哪个类不能直接读取磁盘文件 (A)FileReader

(B)RandomAccessFile (C)FilterReader (D)FileInputStream

21. 如果类中的成员只能被同一包中的类访问,那么声明该成员时应使用下面哪个修饰符

(A)protected (B)package (C)public

(D)不使用访问修饰符

22. 表达式new StringTokenizer( “Welcome to java”).countTokens()的值是 (A)1 (B)3 (C)13 (D)15

23. JPanel 的缺省布局管理器是 (A)BorderLayout (B)FlowLayout (C)GridLayout (D)SpringLayout

24. JFrame 的缺省布局管理器是 (A)BorderLayout (B)FlowLayout (C)GridLayout (D)BoxLayout

25. 已知: public class C {

public int m(int x) { int r = 1; r += x;

if ((x > 5) && (x < 10)) { r += 2 * x; } else (x <= 4) { r += 3 * x;

} else {

r += 4 * x; }

r += 5 * x; return r; }

public static void main(String [] args) { C o = new C();

System.out.println(“m(11) = “ + o.m(11)); } }

(A)m(11) = 45 (B)m(11) = 56 (C)m(11) = 89 (D)m(11) = 111

26. 已知: 目录结构 dira

|---A.class |---dirb

|---B.class 和源代码 import dira.*; class C { A a; B b; }

那么要使源代码通过编译,需要在源代码中添加 (A)package dira; (B)package dirb; (C)package dira.dirb; (D)package dirb.dira;

27. 如果 java.awt.Container c 的布局为BorderLayout,则 c.add(new Jbutton())的默认位置参数是

(A)BorderLayout.EAST (B)BorderLayout.WEST (C)BorderLayout.CENTER (D)编译错误

28. 已知: class C {

public static void main(String[] args) { String s = “welcome to java”; System.out.println(s.substring(11)); } }

的输出为 (A)java

(B)welcome to (C)ome to java (D)welcome to java

29. 已知: class C {

int i = 1; int j = 2;

public void swap() { int temp = i; i = j; j = temp; }

public static void main(String[] args) { C c = new C(); c.swap();

System.out.println(c.i + “,” + c.j); } }

的输出为 (A)1,1 (B)1,2 (C)2,1 (D)2,2

30. 已知: class B { int i; B(int i) {

this.i = i; } }

class C extends B {

public static void main(String[] args) { C c = new C(1);

System.out.println(c.i); }

}

的输出为 (A)0 (B)1

(C)编译错误 (D)运行时错误

31. 已知: class C { int x; String y; public C() {

this( “1”);

System.out.print(“one “); }

public C(String y) { this(1, “2”);

System.out.print(“two “); }

public C(int x, String y) { this.x = x; this.y = y;

System.out.print(“three “); }

public static void main(String[] args) { C c = new C();

System.out.println(c.x + ““ + c.y); } }

则输出为

(A)1 2 one two three (B)1 2 three two one (C)one two three 1 2 (D)three two one 1 2

32. 已知: class C {

public static void main(String[] args) { int x = 5; C c = new C(); c.m(x);

System.out.print(x); }

public void m(int x) {

System.out.print(x++); } }

则输出为 (A)55 (B)56 (C)65 (D)66

33. 已知: interface I { }

class A implements I { } class B extends A { } class C extends B {

public static void main(String[] args) { B b = new B(); _____________ } }

在横线处添加哪条语句运行时会产生异常 (A)A a = b; (B)I i = b; (C)C c = (C)b; (D)B d = (B)(A)b;

34. 已知: class C {

public static void main(String[] args) { String s = “null”; if(s == null)

System.out.print( “a”); else if(s.length() == 0)

System.out.print( “b”); else

System.out.print( “c”); } }

的输出为 (A)a (B)b (C)c (D)null 35. 已知: class C {

public static void main(String[] args) { BufferedReader br = null; try {

br = new BufferedReader(new FileReader( “in.txt”)); String temp = null;

while((temp = br.________) != null) { System.out.println(temp); }

} catch(Exception e) { } finally {

if(br != null) { br.close(); } } } }

要想打印出 in.txt 的全部内容,需要在横线处填写 (A)read() (B)next() (C)readLine() (D)nextLine()

36. 已知: class Student {

String code, name; int age;

public boolean equals(Object o) { if(!o instance of Student) { return false; }

Student s = (Student)o;

return o.code == code && o.age == age; }

public int hashCode() { _________________ } }

横线处应填写

(A)return code.hashCode();

(B)return code.hashCode() + age * 11;

(C)return code.hashCode() + name.hashCode() + age * 11; (D)return super.hashCode();

37. 已知:

class C {

public static void main(String[] args) { parse( “number”); }

public static void parse(String s) { try {

int i = Integer.parseInt(s); } catch(NumberFormatException e) { i = 0; } finally {

System.out.println(i); } } }

则输出为 (A)0

(B)编译错误

(C)运行时抛出ParseException

(D)运行时抛出NumberFormatException

38. 已知:

public interface I { void m(); }

下面哪个定义是合法的

(A)public interface J extends I {} (B)public interface J implements I {} (C)public interface J inherits I {} (D)public interface J instanceof I {}

39. 已知:

abstract class Shape { private int x, y;

void setLocation(int x, int y) { this.x = x; this.y = y; }

abstract void draw(); }

class Circle extends Shape { void draw() {} }

class Test {

public static void main(String[] args) {

_________________ } }

下面哪段代码在横线处是合法的 (A)Shape s = new Shape(); s.setLocation(1,1); s.draw();

(B)Circle c = new Shape(); s.setLocation(1,1); s.draw();

(C)Shape s = new Circle() s.setLocation(1,1); s.draw();

(D)Shape s = new Circle() s.Shape.setLocation(1,1); s.Shape.draw();

40. Java 的屏幕坐标系的(0,0)在屏幕的 (A)中心 (B)左上角 (C)左下角 (D)右下角

41. 已知: class C {

public static void main(String[] args) { int i = 10;

System.out.println(i < 8 ? 1.0 : 1); } }

(A)1 (B)1.0 (C)8 (D)10

42. 已知: class C {

public static void main(String[] args) { int i = 1;

boolean b = false; if(b && ++i == 2) {

System.out.print( “first “ + i); } else {

System.out.print( “second “ + i);

} } }

(A)first 1 (B)first 2 (C)second 1 (D)second 2

43. 已知: class C {

public static void main(String[] args) { String s = “Welcome to “; concat(s);

System.out.print(s); }

public static void concat(String s) { s += “Java”; } }

则输出为

(A)Welcome to (B)Welcome to Java (C)编译错误 (D)运行时异常

44. javax.swing.Timer将按一定频率产生什么事件 (A)ActionEvent (B)TimerEvent (C)ItemEvent (D)InputEvent

45. 下面哪种布局方式只影响控件的高度,而不影响宽度 (A)BorderLayout 的WEST 和EAST (B)BorderLayout 的NORTH 和SOUTH (C)GridLayout (D)FlowLayout

46. 已知: class C {

public static void main(String[] args) { try {

System.out.print(10 + 10 / 0); } catch(NullPointerException e1) { System.out.print( “a”);

} catch(RuntimeException e2) { System.out.print( “b”); } finally {

System.out.print( “c”); } } }

(A)a (B)ac (C)bc (D)abc

47. 已知: class C {

public static String s = “c”;

public static void main(String[] args) { C c = new C(); B b = new B();

System.out.print(C.s); System.out.print(B.s); System.out.print(c.s); System.out.print(b.s); System.out.print(((C)b).s); } }

class B extends C {

public static String s = “b”; }

则输出为 (A)ccccc (B)cbcbb (C)cbccc (D)cbcbc

48. 已知: public class C {

public static void main(String[] args) { print(new GraduateStudent()); }

public static void print(Object x) {

System.out.println(x.toString()); } }

class Person {

public String toString() { return “Person”; } }

class Student extends Person { public String toString() { return “Student”; } }

class GraduateStudent extends Student { }

则输出为

(A)GraduateStudent (B)Student (C)Person (D)Object

49. 在JPanel 中绘图应覆盖哪个方法 (A)paint(Graphics g)

(B)paintComponent(Graphics g) (C)print(Graphics g) (D)repaint(Rectangle r)

50. 下面哪个方法声明要求子类必须提供实现 (A)public double m(); (B)static void m() {} (C)abstract double m(); (D)protected void m() {}

51. 已知: class C {

private int count = 0; public C () {

count++; }

public static void getCount() { return count; }

public static void main(String[] args) { C c1 = new C(); C c2 = new C(); C c3 = new C();

System.out.println(C.getCount()); }

}

则输出为 (A)1 (B)2 (C)3

(D)编译错误

52. is-a 关系是指 (A)继承关系 (B)实现关系 (C)关联关系 (D)聚合关系

53. 已知: class C {

int x = 5;

public void m(int x) { x += x;

System.out.println(x); }

public static void main(String[] args) { C c = new C(); c.m(4); } }

的输出为 (A)8 (B)9 (C)10

(D)编译错误

54. 已知: interface I {

int CONST = 3; void m(int i); }

class C implements I {

public static void main(String[] args) { int x = 5;

new C().m(++x); }

void m(int i) {

System.out.println(CONST + i); }

}

则输出为 (A)8 (B)9

(C)编译错误 (D)运行时异常

55. 已知: interface Inter { void m(); }

abstract class Super {

public abstract void m(); }

则下面哪段代码是合法的

(A)class sub extends Super implements Inter { public void m() {} }

(B)class sub extends Super implements Inter { public void m() {}

public void Inter.m() {} }

(C)class sub extends Super implements Inter { public void Super.m() {} public void m() {} }

(D)class sub extends Super implements Inter { public void Inter.m() {} public void Super.m() {} }

56. 已知: class C {

public static void main(String[] args) {

String[] s = {“one”, “two”, “three”}; System.out.println(s.length > 0 ? s[0] : null); } }

则输出为 (A)one (B)two (C)three (D)null

57. 已知: class C {

public static void main(String[] args) {

System.out.println( “null” instanceof Object); } }

则输出为 (A)null (B)true (C)String (D)Object

58. 已知: class C {

public static void main(String[] args) { int[] array = new int[10]; increase(array);

System.out.print(array[0]); }

public static void increase(int[] array) { for(int i = 0; i < array.length; i++) { array[i]++; } } }

则输出为 (A)0 (B)1 (C)2 (D)10

59. this()表示

(A)引用当前对象 (B)调用当前对象

(C)调用当前对象的方法

(D)调用当前对象的构造函数

60. 已知: package p; class C {

public int x; }

那么,在哪种情况下可以访问x (A)任意类

(B)只有类C

(C)只有类C 的子类 (D)包p 中的类

61. 已知: 1 class C {

2 private final int x; 3 public C(int x) { 4 this.x = x; 5 }

6 public int getX() { 7 return x; 8 }

9 public void setX(int x) { 10 this.x = x; 11 }

12 public static void main(String[] args) { 13 C c = new C(5); 14 c.setX(10);

15 System.out.println(c.getX()); 16 } 17 }

则输出为 (A)10

(B)第4 行有编译错误 (C)第 10行有编译错误

(D)第 10行不起作用,数据字段x 的值保持不变

62. 如果X extends Y, 那么 (A)X 是接口,Y 是类 (B)X 是类,Y 是接口

(C)X 和Y 都是类,或者X 和Y 都是接口 (D)X 和Y 是类和接口的任意组合

63. 多态应用于 (A)重载 (B)覆盖 (C)实现 (D)组合

64. 已知:

1 class Crivitch {

2 public static void main(String [] args) { 3 int x = 0;

4 ____________________ 5 do { } while (x++ < y); 6 System.out.println(x); 7 } 8 }

在第4 行插入哪句代码系统的输出是24 (A)int y = 22 (B)int y = 23 (C)int y = 24 (D)int y = 25

65. 已知: class Animal {

public abstract String noise(); }

class Dog extends Animal {

public String noise() { return “wang wang”;} }

class Cat extends Animal {

public String noise() { return “miao miao”; } }

class C {

public static void main(String[] args) { Animal animal = new Cat(); Dog dog = (Dog)animal;

System.out.println(dog.noise()); } }

的输出为

(A)wang wang (B)maio miao (C)编译错误 (D)运行时异常

66. 已知: class C {

public static void main(String[] args) { String s = “123”; s += 123;

System.out.println(s); } }

的输出为 (A)123

(B)246 (C)123123 (D)编译错误

67. 已知: class C {

void add(int i) { add(++i); }

public static void main(String[] args) { C c = new C(); c.add(1); } }

执行时会抛出哪种异常 (A)StackOverflowError

(B)IllegalArgumentException (C)NullPointerException (D)VirtualMachineError

68. 已知: class C {

public static void main(String[] args) { for(int i = 0; i <= 10; i++) { if(i > 7) { break; } }

System.out.println(i); } }

的输出为 (A)7 (B)8 (C)10

(D)编译错误

69. 下面哪个类表示 java.awt.Component 的键盘按下事件 (A)KeyEvent

(B)KeyPressedEvent (C)KeyDownEvent (D)KeyTypedEvent

70. 已知:

class C {

public static void main(String[] args) { print(); }

public static void print() { static int i;

System.out.println(++i); } }

(A)0 (B)1

(C)编译错误 (D)运行错误

71. GUI 控件的事件处理中,事件源与监听器之间的关系是 (A)一对一 (B)一对多 (C)多对一 (D)多对多

72. 已知: class C {

public static void main(String[] args) { System.out.println(++i); } }

在命令行中编译上面的代码,应使用 (A)java C (B)java C.java (C)javac C (D)javac C.java

73. 已知: class C {

public static void main(String[] args) { String s = “Welcome to Java”; System.out.println(s.substring(2,3)); } }

则输出为 (A)elc (B)el (C)l (D)lco

74. 已知: class C {

public static void main(String[] args) { String s = “Welcome”; s += “to”;

s.concat( “Java”); System.out.println(s); } }

则输出为

(A)Welcome (B)Welcome to

(C)Welcome to Java (D)编译错误

75. 已知: class C {

public static void main(String[] args) { System.out.println(9 ^ 2); } }

则输出为 (A)81 (B)11 (C)7 (D)0

76. 下面哪个类表示在JTextField 的按Enter 键的事件 (A)TextEvent (B)InputEvent (C)ActionEvent (D)KeyEvent

77. 已知: class C { int i; String s; public C() {

s += “world”; }

public C(int i) { this.i = i;

this.s = “Hello”;

C(); }

public static void main(String[] args) { C c = new C(1);

System.out.println(c.s); } }

(A)Hello

(B)Hello world (C)Hello world1 (D)编译错误

78. 已知: class C {

public static void test(String s) {

if(s == null | s.length() == 0) {

System.out.println( “String is null”); } else {

System.out.println( “String is not null”); } }

public static void main(String[] args) { test(null); } }

则输出为

(A)String is null (B)String is not null (C)编译错误 (D)运行时异常

79. 已知:

_________________ class C {

public static void main(String[] args) { System.out.println(sqrt(4)); } }

要使程序正确编译需要在横线处添加哪句代码 (A)import java.lang.Math; (B)import java.lang.Math.*; (C)import static java.lang.Math; (D)import static java.lang.Math.*;

80. 已知: class C {

public static void main(String[] args) { for(int i = 1; i < args.length; i++) { System.out.print(args[i]); } } }

在命令行中执行 java C D E F 则输入为 (A)CDEF (B)DEF (C)EF (D)F

多项选择题(60)

81. swtich (expression)语句中表达式的类型可以是 (A)String (B)char (C)short (D)double

82. 设有方法: public A method() { ……. return b; }

则b 可以为 (A)null

(B)b 的类型为A

(C)b 的类型为A 的子类 (D)b 的类型为A 的父类

83. 下面关于try、catch和 finally语句块的组合使用,正确的是 (A)try {,}

(B)try {,} finally {,}

(C)try {,} catch {,} finally {,} (D)try {,} catch {,} catch {,}

84. 已知: interface I {

int methodA();

short methodB(byte s); }

下面哪些代码是正确的?

(A)abstract class C extends I {

public int methodA() { return 0; } }

(B)abstract class C implements I { } (C)interface I2 implements I{ } (D)class C extends I {

int methodA() { return 0; } short methodB() { return 1; } }

85. 下面的哪些声明是合法的? (A)int a[]; (B)int a[5]; (C)int[] a;

(D)int a[] = [5];

86. 已知:

1 public class A {

2 A m() { return this;} 3 }

4 class B extends A {

5 _____________________ 6 }

那么第 5行可以添写? (A)A m() { return this;} (B)String m() { return this;} (C)int m(int i) { return 0;} (D)String m(int i) { return 0;}

87. 如果a,b 是引用类型的变量,且赋值表达式A a = b;是正确的, 那么 (A)表达式(b instanceof A)的值为true (B)b 的类型可以是A 的父类型 (C)b 的值可以为null

(D)如果A 为Object,那么b 可以是任何引用类型的变量

88. 关于接口,描述正确的是

(A)接口是抽象方法和常量值定义的集合 (B)不相关的类可以实现同一接口 (C)一个类只能实现一个接口

(D)接口的成员方法不能提供实现代码

89. 不必被捕获或声明抛出的异常是 (A)NullPointerExcepiton (B)ArithmeticException (C)FileNotFoundException (D)RuntimeException

90. 类与类之间的关系包括 (A)关联关系 (B)继承关系 (C)组合关系 (D)实现关系

91. 下面类中属于字节 I/O流的是 (A)FileReader

(B)FileInputStream

(C)BufferedOutputStream (D)BufferedWriter

92. 已知: 1 class C {

2 public static void main(String[] args) { 3 String s = “- “ ; 4 try {

5 m(args[0]); 6 s += “t “; 7 } finally {

8 System.out.println(s += “f “); 9 } 10 }

11 public static void m(String a) {

12 int y = 7 / Integer.parseInt(a); 13 } 14 }

在命令行中输入: java C java C 0 则

(A)第 6行会执行0 次 (B)第 8 行会执行 1次 (C)第 12行会执行2 次 (D)会抛出不同的异常

93. 下面说法正确的是

(A)继承表示 is-a 的关系

(B)继承表示has-a 的关系

(C)要表示has-a 的关系必须使用接口 (D)可以使用实例变量表示has-a 的关系

94. 已知: class C {

public static void a() {/*代码*/} public void b() {/*代码*/} }

下面说法正确的是

(A)可以在a 的代码中执行C.a() (B)可以在b 的代码中执行C.b() (C)可以在b 的代码中执行a() (D)可以在a 的代码中执行b()

95. 已知:

public abstract class Shape { private int x; private int y;

public abstract void draw(); public setLocation(int x, int y) { this.x = x; this.y = y; } }

则下面的哪些代码是正确的

(A)public class Circle implements Shape { private int radius; }

(B)public abstract class Circle extends Shape { private int radius; }

(C)public class Circle extends Shape { private int radius; public void draw(); }

(D)public class Circle extends Shape { private int radius; public void draw(){} }

96. 下面哪些main 方法可用于程序执行 (A)public static void main(String[] args) (B)public static void main(String[] x)

(C)public static void main(String args[]) (D)public void main(String[] args)

97. 下面说法正确的是

(A)实现同一接口的两个类之间是紧耦合的 (B)封装可以提高类的重用性

(C)封装好的类只允许子类覆盖方法,不允许子类重载方法 (D)封装好的类允许修改方法的实现,而不影响外部代码

98. 已知: class C {

public static void main(String[] args) { String s = “Welcome to Java”;

String s1 = new String(“Welcome to Java”); String s2 = s1.intern();

String s3 = “Welcome to Java”; boolean b = __________________ } }

则在横线处添加哪些代码b 的值为true (A)s1 == s (B)s2 == s (C)s3 == s (D)s.equals(s1)

99. 下面哪些语句是没有编译错误的 (A)StringBuffer sb = “java”; (B)int i = 0x56;

(C)Boolean b = new Boolean( “java”); (D)float f = 1.0;

100.下面哪些类可用于throw语句 (A)Error (B)Exception (C)Event

(D)Throwable

101.已知:

____________________ public class Base { }

则下面哪些语句在横线处是合法的 (A)package org.open; (B)import java.util.*;

(C)class Derived {}

(D)public static final CONTST = 1;

102.已知: class C {

int a = 1;

public void m(final int b) { int c = 1; class Inner {

public void mi(int d) {

____________________ } }

Inner inner = new Inner(); inner.mi(1); } }

则下面哪个表达式在横线处是正确的 (A)int e = a; (B)int e = b; (C)int e = c; (D)int e = d;

103.下面哪个语句是正确的 (A)short s = 256;

(B)String s = ?Hello world?; (C)int x = 012; (D)char c = “a”;

104.下面哪些标识符是合法的 (A)s1 (B)$1 (C)_1 (D)1s

105.下面哪些说明是正确的 (A)可以在类中定义类 (B)可以在类中定义方法 (C)可以在方法中定义类 (D)可以在方法中定义方法

106.已知: 1 class Student {

2 private String code;

3 public Student(String code) { 4 this.code = code; 5 }

6 public String getCode() { 7 return code; 8 }

9 public boolean equals(Student s) { 10 return code.equals(s.code) 11 } 12 }

下面说法正确的是

(A)第 7行会产生编译错误,原因是不能访问private 成员变量 code (B)当执行new Student.equals(new Object())时,会产生运行时异常 (C)code在 Student对象初始化后无法修改

(D)Student.equals 方法不会覆盖Object.equals 方法

107.下面哪些说法是正确的

(A)一个控件上可以注册多个监听器 (B)一个监听器只能注册到一个控件 (C)一个类只能实现一个监听器接口 (D)可以从控件上删除事件监听器

108.下面哪些代码是正确的

(A)File f = new File(“/”,”java.txt”);

(B)DataInputStream d = new DataInputStream(System.in);

(C)OutputStreamWriter o = new OutputStreamWriter(System.out); (D)RandomAccessFile r = new RandomAccessFile(“java.txt”);

109.已知: class C { C c;

_____________________ }

则下面的代码哪些在横线处是正确的 (A)private class Inner {} (B)static class Inner() {} (C)C c2 = new C() {}; (D)c = new C();

110.已知:

1 public interface I { 2 int i = 1; 3 }

下面哪些语句是和第2 行等价的

(A)public int i = 1; (B)static int i = 1; (C)final int i = 1; (D)abstract int i = 1;

111.已知:

class C implements Runnable { public void run() {

System.out.println( “run”);

throw new RuntimeException( “exception”); }

public static void main(String[] args) { Thread t = new Thread(new C()); t.start();

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

则输出为 (A)end run

java.lang.RuntimeException:exception (B)run

java.lang.RuntimeException:exception end (C)run

java.lang.RuntimeException:exception (D)end

java.lang.RuntimeException:exception run

112.已知: class C {

void m() {} }

class B extends C {

_______________ }

下面哪些语句在横线处是合法的 (A)int void m() {} (B)private void m() {} (C)protected void m() {} (D)public void m() {}

113.已知:

class Rectangle {

private int width, height;

public void setSize(int width, int height) { this.width = width; this.height = height; } }

下面哪些代码重载 setSize方法

(A)protected void setSize(int width, int height) { this(width, height) }

(B)public void setSize(int width, float height) { this.width = width;

this.height = (int)height; }

(C)protected void setSize(int width) { this.width = width; }

(D)public void setSize(int height, int width) { this.width = width; this.height = height; }

114.哪些情况下线程会停止

(A)调用该线程的halt 方法 (B)调用该线程的 stop方法

(C)为另一个线程指定更高的优先级 (D)调用System.exit(0);

115.已知: class Base { }

class Derived extends Base { static int i = 10; static float f = 10f; static char c = ?a?;

public static void main(String[] args) { Base b = new Base();

Derived d = new Derived(); ______________________ } }

下面哪些代码在横线处是合法的 (A)d = b;

(B)f = c; (C)c = i; (D)b = d;

116.已知: interface I {

void ma(); void mb(); void mc(); }

class Base {

public void ma(){} }

class Derived extends Base implements I { __________ }

要使Derived 通过编译需要添加下面哪些语句 (A)public void ma() {} (B)void ma() {}

(C)public void mb() {} (D)public void mc() {}

117.下面哪些布局管理影响容器内控件的大小 (A)GridLayout (B)BorderLayout (C)FlowLayout (D)GridBagLayout

118.下面哪些代码可以正确编译 (A)double d = 1.0; int i = 1; if(d == i) {

System.out.println( “Hello world”); }

(B)int i = 1; int j = 2;

if(i = 1 && j = 2) {

System.out.println( “Hello world”); }

(C)boolean b1 = true, b2 = false; if(b1 == b2) {

System.out.println( “Hello world”); }

(D)int i = 0;

if(i) {

System.out.println( “Hello world”); }

119.已知: class C { int i;

________________ }

下面哪些语法在横线处是合法的 (A)i++;

(B)void m(int i){}

(C)class B extends C{} (D)System.out.println(i);

120.下面哪些类型实现了Comparable 接口 (A)Integer (B)double (C)String (D)Object

121.已知: class Base {

void m() {} }

class Derived extends Base {

________________________ }

则在横线处哪些语句是合法的 (A)private void m() {} (B)protected void m() {} (C)public void m() {} (D)public void m(int i) {}

122.已知: class C {

public static void main(String[] args) { String[] s = new String[10];

___________________________ } }

则下面的说明哪些是正确的 (A)s[0]的值为”“

(B)s[3] + s[4]的值为 “nullnull”

(C)s[10]的值为null (D)s.length 的值10

123.已知: class C {

int x = 100;

public void m(int x) {

System.out.println(___________); }

public static void main(String[] args) { C c = new C(); c.m(0); } }

如果程序输出 101,则在横线处填写下面哪些代码 (A)x++ (B)++x (C)++this.x (D)this.x + 1

124.下面哪些方法禁止子类重定义该方法 (A)protected void m() {} (B)final void m() {}

(C)abstract final void m() {} (D)static final void m() {}

125.下面哪些控件可以产生ActionEvent (A)javax.swing.JButton (B)javax.swing.JTextBox (C)javax.swing.JList (D)javax.swing.Timer

126.已知: class C {

public C(int i) {} }

下面哪些代码是对构造函数的重载 (A)C() {}

(B)public Object C(byte b) {} (C)public void C() {} (D)private C(int i, int j) {}

127.下面哪些类属于轻型组件 (A)JFrame

(B)JLabel (C)JButton (D)JTree

128.已知: class C {

public static void main(String[] args) { try {

method();

System.out.println(“a”); } catch (ArithmeticException e) { System.out.println(“b”); } finally {

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

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

static void method () {

throw new NullPointerException(); } }

则输出包含 (A)a (B)b (C)c (D)d

129.下面哪些说法是正确的 (A)静态方法不能被覆盖 (B)私有方法不能被重载 (C)实例变量能够被隐藏 (D)静态方法能够被重载

130.已知: class C {

public static void main(String[] args) { int[] a = new int[10];

_____________________ } }

下面哪些语句在横线处可以正常编译 (A)int i = a.length(); (B)int i = a[10]; (C)int i = a.getAt(0);

(D)int i = a[a[0]];

131.下面哪些类型属于Java基本数据类型 (A)int

(B)Boolean (C)uint (D)double

132.已知String s = “Java”,则下面哪些代码是正确的 (A)s = s + 1; (B)char c = s[3]; (C)int i = s.length;

(D)String t = s + new Object();

133.下面说法正确的是

(A)当且仅当类C 是抽象类时,才能包含 final方法 (B)类C 中的protected 实例方法可以被子类覆盖

(C)类C 中的private 静态方法只能被类C 中的其它静态方法调用

(D)类C 中的public 静态方法可以被子类直接访问,而不需要用类名引用方法

134.已知: 1 class C {

2 private int i;

3 protected C(int i) { this.i = i; } 4 }

5 class B extends C {

6 public B() { this.i = 5; } 7 public B(int i) { super(i); } 8 }

要使用类B 通过编译,只需要修改 (A)第 2行改为:public int i;

(B)第 3行改为:public C(int i) { this.i = i; } (C)第 6行改为:public B() { this(5); } (D)第 6行改为:public B() { super(5); }

135.下面选项中哪些不是Java 关键字 (A)NULL (B)instanceof (C)sizeof (D)interface

136.下面哪些代码中声明的变量可以存放10个 String对象 (A)String[] a; (B)Object[] a;

(C)String[50] a; (D)char a[][];

137.已知: class Base() {}

class Derived extends Base {

public static void main(String[] args) { Base base = new Base();

Derived derived = new Derived(); ________________ } }

则下面表达式在横线处值为 true 的是 (A)base instanceof Derived (B)base instanceof Object (C)derived instanceof Base (D)derived instanceof Derived

138.RandomAccessFile 实现了哪些接口 (A)DataInput (B)InputStream (C)DataOutput (D)OutputStream

139.下面哪些类型属于引用类型 (A)int[] (B)Integer (C)char (D)String

140.下面哪些类是抽象类 (A)java.io.InputStream (B)java.io.PrintString (C)java.io.FileWriter (D)java.io.Reader

判断题(40)

141.一个源文件中可以包含多个公共类。F 142.一个源文件中只能包含一个类。F 143.java 中的标识符可以是任意长度。 T

144.如果源文件中包含public类,源文件的文件名必须与类名相同。145.如果一个类没有访问控制符,它只能被同一包中的类访问。T 146.类可以同时使用 final和abstract进行修饰。 F

T 147.final类不能被继承。 T

148.构造函数名称可以与方法名称相同。T 149.局部内部类可以访问 final局部变量。T 150.局部内部类可以访问所在类的数据字段。 T 151.final方法不能被覆盖。 T

152.每个类都至少包含一个构造函数。 T 153.String对象中的值是不可变的。 T

154.StringBuffer对象中的值是不可变的。 F 155.Integer对象中的值是可变的。 F 156.构造函数的返回值类型为void。F 157.类中不允许定义私有的构造函数。 F

158.this()和 super()只能位于构造函数中的第一句。 T 159.表达式-0.0 == 0.0 的结果为 true。 T

160.声明局部变量时,局部变量将被赋一个默认的初值。 F 161.包含抽象方法的类必须声明为抽象类。 T 162.抽象类必须包含抽象方法。F

163.静态方法可以访问类中的所有成员。 F 164.数据字段的生存周期与对象相同。 T

165.数组的下标超界将访问到未知的内存区域。 F 166.二维数组的行和列是相同的。 F

167.表达式 ““.equals(null)的结果为 true。 F

168.continue 只能出现在在循环语句中(while, do, for)。T 169.接口中只能包含抽象方法,不能包含实例方法。 T 170.抽象类不能实现接口。 F

171.一个类只能有一个父类,但可以实现多个接口。T 172.一个接口可以继承多个父接口。 T

173.类的可访问性不影响类成员的可访问性。 F 174.父类型的变量可以引用子类型的对象。 T

175.静态成员是根据变量引用的对象的实际类型进行访问的。F 176.实例方法是根据引用变量的声明类型进行访问的。F 177.构造函数只能重载不能覆盖。 T

178.子类可以覆盖父类中的所有实例方法。 F 179.内存垃圾回收将删除无法访问的对象。 T 180.私有方法不产生覆盖。 T

问答题(20)

181.将下面的代码修改正确 class Car {

private String color; private int speed; public C(){};

public Car(String color) { this.color = color;

this.speed = 40; } }

class Hummer extends Car {

public Hummer(String color) { this.speed = 60; } }

182.举例说明类和类之间的关系。 依赖,关联,组合,聚合,泛化。

183.举例说明 getter和 setter方法的目的和作用。

getter and setter 方法的目的是保护对象的属性变量。

比如,当我们需要让外界只有读的权限而没有修改的权限,我们可以吧setter方法设置为private。

而且有些属性我们在调用方法的时候可以对属性进行预处理,如初始化。

184.写出下面代码的输出,并说明为什么。 public class Test {

public static void main(String[] args) { Animal x = new Tiger();

System.out.println(“(1)x.news is “ + x.news);

System.out.println(“(2)((Tiger)x).news is “ + ((Tiger)x).news); System.out.println(“(3)x.smile() is “ + x.smile());

System.out.println(“(4)((Tiger)x).smile() is “ + ((Tiger)x).smile()); System.out.println(“(5)x.getNews() is “ + x.getNews());

System.out.println(“(5)x.getMessage() is “ + x.getMessage()); } }

class Animal {

public String news = “Animal's news”; public String message = “Animal's message”;

public static String smile() { return “smile from Animal”; }

public String getNews() { return news; }

public String getMessage() { return message;

}

}

class Tiger extends Animal {

public String news = “Tiger's news”;

public String message = “Tiger's message”;

public static String smile() { return “smile from Tiger”; }

public String getNews() { return news; } }

(1)x.news is Animal's news

(2)((Tiger)x).news is Tiger's news (3)x.smile() is smile from Animal

(4)((Tiger)x).smile() is smile from Tiger (5)x.getNews() is Tiger's news

(5)x.getMessage() is Animal's message

185.说明基本数据类型与引用类型的差别。 基本类型,传递的是值, 引用类型,传的是引用。

186.下面的代码读取并打印文本文件,请修改正确。

public class Test {

public static void main(String[] args) throws Exception{ FileReader fr = new FileReader(“test.txt”); String line;

BufferedReader in = new BufferedReader(fr); while ((line = in.readLine()) != null) { System.out.println(line); }

in.close(); } }

187.举例说明Java语言的编码规范。

类:名词或名词性词组,每个单词首字母大写

方法:动词或动词性词组,首字母小写,第二个及以后的单词首字母大写

变量:名词或名词性词组,首字母小写,第二个及以后的单词首字母大写 常量:名词或名词性词组,每个单词的首字母大写;单词间使用下画线分隔

188.Java语言有什么特点?

(1). Java产生与流行是InterNet发展的客观要求.

(2).是一门各方面性能都很好的编程语言,它的基本特点是简单,面向对象,分布式,解释的,健壮的,完全的,结构中立的,可移植的,性能很优异的,多线程的,动态的,适合的Internet环境上开发应用系统.

(3).Java可以制作大部分网络应用程序系统,而且与如今流行的WWW浏览器结合很好. (4).Java 不仅仅是一种各方,更重要的是一种区别于传统系统,遵守”网络就是计算机”信条的平台技术.Java平台将面向对象系统扩展成包括程序和数据的网络计算机 (NC),而这个平台的核心就是Java虚拟机,许多使Java成为万能开发平台的属性都源于Java虚拟机的概念和实现.

189.说明实例成员和静态成员之间的差别。

实例变量,初始化是在对象新建时,一个对象,一个对象一组实例成员变量; 静态变量,JVM加载类的时候初始化,一个虚拟机中一个类只有一组静态变量。

190.举例说明 String 和StringBuffer 的区别和应用场合。

STRING的长度是不可变的,STRINGBUFFER的长度是可变的。如果你对字符串中的内容经常进行操作,特别是内容要修改时,那么使用StringBuffer,如果最后需要String,那么使用StringBuffer的toString()方法

使 用 StringBuffer 主要就是在性能上的考虑。 String 是一种非常常用的数据类型,但由于 String 是不可变对象,在进行 String 的相关操作的时候会产生许多临时的 String 对象。而 StringBuffer 在操作上是在一个缓冲中进行的,性能当然优越得多。不过,一般做为简单的字符串传递和其它操作,只不要改变字符串内容的操作,用 String 效率会高一些。

191.什么是ChechedException 和Unchecked Exception?

ChechedException 是需要手动捕捉的异常,继承Exception 而且没有继承RuntimeException;

Unchecked Exception 不需要手动捕捉的异常,也称为运行时异常,这种异常一般在编程中就能避免,这种异常继承 RuntimeException 。

192.说明抽象类和接口的异同。

抽象类 和接口 都是需要被继承/实现,自己不能直接创建对象。

不同,

(1) 抽象类中能写实现,接口不能写实现;

(2) 一个类只能继承一个抽象类,但是能实现多个接口

193.列举基本数据类型的包装类,说明包装类的作用。 比如:

public class Book(){

int id ;

string name ; int price ; }

这个Book 就是基本数据的包装类,

作用,我们有时候要传递一组数据,这样管理起来很麻烦, 怎么办呢,把这些数据封装成一个对象, 我们传递的时候只传递一个对象就可以了。

194.举例说明多态。

多态(Polymorphism)按字面的意思就是”多种形状”。引用Charlie Calverts对多态的描述——多态性是允许你将父对象设置成为和一个或更多的他的子对象相等的技术,赋值之后,父对象就可以根据当前赋值给它的子对象 的特性以不同的方式运作(摘自”Delphi4 编程技术内幕”)。简单的说,就是一句话:允许将子类类型的指针赋值给父类类型的指针。

概念很多,我们可以理解为:

父类类型 ,子类对象。 这就是多态。

多态的最大的作用是,吧接口定义和具体实现分离,便以工程的管理。

195.举例说明 this关键字的作用。

this 关键字 在一个对象中表示对象自身。

作用是,在方法体内,当局部变量名 和 属性变量名有冲突的时候 , 我们可以通过this.属性名 来区别 局部变量。 而且一个类有多个构造函数,我们可以通过this在一个构造函数中调用另外一个构造函数。

196.举例说明 Swing控件的事件处理过程。

事件的触发,将调用注册的对应对象中的对应方法。

197.说明覆盖和重载之间的区别。

覆盖 就是 子类 重新实现 父类中的某个方法,

重载 就是 在对象有多个 同名的方法, 但是参数不同。

198.修改下面的代码,实现点击按钮Add 时,标签显示数字增加的功能。 import java.awt.*; import java.awt.event.*; import javax.swing.*;

public class MyFrame extends JFrame implements ActionListener{ private int number = 0;

private JLabel label; private JButton button;

public MyFrame() {

Container container = getContentPane(); container.setLayout(new FlowLayout());

container.add(new JButton(“Add”)); container.add(new JLabel(String.valueOf(number))); container.add(button = new JButton(\ button.addActionListener(this);

container.add(label = new JLabel(String.valueOf(number)));

}

public void actionPerformed(ActionEvent e) {

number++;

label.setText(String.valueOf(number));

}

public static void main(String[] args) {

MyFrame frame = new MyFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack();

frame.setVisible(true); } }

199.下面的类表示一个矩形,请找出源代码中的错误并改正。 public class Rectangle { private int width; private int height; public Rectangle() { super(1, 1);

}

public Rectangle(int width, int height) { this.width = width; this.height = height; }

public void getHeight() { return height;

}

public void setHeight(int height) { height = height; }

public void getWidth() { return width; }

public void setWidth(int width) { width = width; }

public int getArea() {

this.width * this.height; }

public double getPerimeter() {

2 * (this.width + this.height); } }

200.画出程序的运行结果 import java.awt.*; import javax.swing.*; public class Demo {

public static void main(String[] args) {

JFrame frame = new JFrame(“Demo”);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container pane = frame.getContentPane(); pane.setLayout(new GridLayout(0,3)); pane.add(new JButton(“Button 4”)); pane.add(new JButton(“Button 3”)); pane.add(new JButton(“Button 2”)); pane.add(new JButton(“Button 1”)); frame.pack();

frame.setVisible(true); } }