java2习题200道及答案

更新时间:2024-05-19 14:09:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

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;

}

则输出为 (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();

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;

本文来源:https://www.bwwdw.com/article/l0j7.html

Top