《面向对象程序设计JAVA语言程序设计》期末考试试题及部分答案

更新时间:2023-03-13 07:41:01 阅读量: 教育文库 文档下载

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

一、单选题

1. 在下列概念中,Java语言只保留了D

A.运算符重载

B.结构和联合

C.指针

D. 方法重载

2. 下列类定义中,不正确的是B

A.class x { .... }

B.static class x implements y1,y2 { .... } D.class x extends y { .... }

C.public class x extends Applet { .... } 3. 下列语句序列执行后,k 的值是B

int m=3, n=6, k=0; while( (m++) < (- - n) ) ++k;

i++

先把i的初值赋给表达式(i++),作为表达式(i++)的值,然后i自增1作为i的值。

如i=1,则表达式(i++)的值等于1,而i的值变为2。 ++i

i先自增1,再把自增后的值赋给表达式(++i)。 如i=1,则表达式(++i)的值等于2,i的值也为2,

A.0

B.1

C.2

D.3

4. 设i、j为int型变量名,a为int型数组名,以下选项中,正确的赋值语句是A

A.a[0] = 7;

B.i = i + 2

C.i++ - --j; D.a(0) = 66;

5. Java语言的类间的继承关系是B

A.多重的

B.单重的

C.线程的

D.不能继承

6. 下列语句序列执行后,k 的值是C

int i=10, j=18, k=30; switch( j - i ) {

case 8 : k++; case 9 : k+=2; case 10: k+=3; default : k/=j; } A.31

B.3

C.2

D.33

7. 设有定义 float x=3.5f, y=4.6f, z=5.7f;则以下的表达式中,值为true的是C

A.x > y || x > z

B.z > ( y + x )

C.x != y

D.x < y & ! ( x < z )

8. 下列修饰符中,定义类头时能使用的修饰符是C

A.private

B.static

C.abstract

D.protected

9. 设i、j、k为类x中定义的int型变量名,下列类x的构造函数中不正确的是B

A.x( int m){ ... }

B.void x( int m){ ... } D.x( int h,int m,int n){ ... }

C.x( int m, int n){ ... }

10. 下列对封装性的描述中,错误的是 D

A.封装体包含了属性和行为

B.封装使得抽象的数据类型提高了可重用性 C.被封装的某些信息在封装体外是不可见的 D.封装体中的属性和行为的访问权限是相同的 11. 下列关于包的描述中,错误的是 B

A.包是若干个类的集合

B.包是一种特殊的类型 D.包有有名包和无名包两种

C.包是使用package语句创建的

12. 下列关于Applet程序的描述中,错误的是 C

A.Applet程序的主类必须是Applet类得子类 B.Applet不是完整的独立程序

C.Applet程序的主类中应有一个main()方法 D.Applet的字节码文件必须嵌套在一个HTML文件中 13. 下列界面元素中,不是容器的是

A. Dialog

B.Frame

C

C.List

D.Panel

14. 下列关于异常处理的描述中,错误的是 D

A.程序运行时异常由Java虚拟机自动进行处理 B.使用try-catch-finally语句捕获异常 C.使用throw语句抛出异常

D.捕获到的异常只能在当前方法中处理,不能在其他方法中处理 15. 以下方法不属于迭代器(Iterator)接口所定义的方法的是 D

A.hasNext() B.next() 16. 主线程属于

A.守护线程

C.remove()

D.nextElement()

(C )

B.Thread线程 C.用户线程 D.Runnable线程

二、填空题

1. 2.

三目运算符的3个操作数中,第一个操作数的类型必须是 逻辑 型的。

在Java的基本数据类型中,char型采用Unicode编码方案,每个Unicode码占用 2 字节内存空间,这样,无论是中文字符还是英文字符,都是占用 2 字节内存空间。 3. 4. 5. 6.

假设x=13,y=4,则表达式x%y != 0的值是 true ,其数据类型是 布尔型 。 Java语言中,多态性主要表现在两个方面: 方法重载 和 成员覆盖 。 定义类的关键字是 class ,定义接口的关键字是 interface 。

在Java语言中,通过类的定义只能实现 单 重继承,但通过接口的定义可以实现 多 重继承关系。 7.

在Java中字符串包括字符串常量和字符串变量,分别由类 String 和类 StringBuffer 来创建对象。 8. 9.

Java 源程序文件编译后产生的文件称为 ·class 文件,其扩展名为 ·java 。 在多线程程序设计中,若要启动一个线程需要调用的函数是 start 。

10. 在Java语言中,系统规定:表示当前类的构造方法用 this ,表示直接父类的构造方法用 super 。

17. Java语言规定标识符是由 字母 、 下划线 、 数字 和 美元符号 组成的字符序列,长度不限;又规定第一个字符不能是 数字 ,并区分字母的大小写。 18. Java中实现多线程一般使用两种方法,一是Thread,二是 Runnable 。 19. Java中变量有三要素: 类型 、 变量名 和 变量值 。

20. Java语言中,说明或声明数组时 内存大小,创建数组时 内存大小。 21. 集合框架提供的Map接口的实现类中,在Map中插入、删除和定位元素,使用 HasMap

是最好的选择;需要按顺序遍历键,需选择 ThereMap 。

22. Outputstream 类和 Inputstream类是所有字节流输入输出类的父类。 23. 导入my.package包中的所类的命令是 import 。

24. 构造方法是一种特殊的成员方法,构造方法名与 类 相同,并且此方法 无 有返回值。

25. 图形用户界面的设计中有事件处理模型处理事件,在该模型中主要包括 事件源、 事件对象 和 事件监听者 。

三、判断题

1. Java语言具有较好的安全性和可移植性及与平台无关等特性。T

2. Java语言的基本数据类型有4种:整型、浮点型、字符型和布尔型。T

3. 组成Java Application的若干类中,有且仅有一个主类,只有主类中含有主方法main。

T

4. 数据由高类型转换到低类型时候,采用强制转换,数据精度要受到损失。T 5. 子类所继承父类的成员都可以在子类中访问。 6. 静态方法只能处理静态变量。T

7. 抽象方法是一种只有说明而无具体实现的方法。

8. Java语言规定在嵌套的程序块中不允许定义同名的成员变量。

9. Throwable类有两个子类:Error类和Exception类。前者由系统保留,后者供应用程序

使用。

10. 运行Applet程序是使用AppletViewer命令运行嵌入了字节码文件的html文件,从而获

得运行结果。

11. 成员方法的重载和覆盖是同一回事。

12. 集合Set是通过键-值对的方式来存储对象的。

13. Java语言是一种强类型语言,数据类型转换有两种:隐含转换和强制转换。 14. Java语言中,数组在静态和动态赋值时都判越界。

15. Frame容器是有边框的容器,它也是一种独立窗口,只能作为最外层容器。 16. 死锁的产生是因为多个线程间存在资源竞争。

1.组成Java Application的若干类中,有且仅有一个主类,只有主类中含有主方法main。( ) 2.数据由高类型转换到低类型时候,采用强制转换,数据精度要受到损失。

( )

( )

3.成员方法的重载和覆盖是同一回事。

4.抽象方法是一种只有说明而无具体实现的方法。 5.Java2的事件处理方法是采用委托事件处理模型。

( )

( )

6.Throwable类有两个子类:Error类和Exception类。前者由系统保留,后者供应用程序使用。 ( )

7.运行Applet程序是使用AppletViewer命令运行嵌入了字节码文件的html文件,从而获得运行结果。

8.Java语言具有较好的安全性和可移植性及与平台无关等特性。

( ) ( )

9.Java语言的基本数据类型有4种:整型、浮点型、字符型和布尔型。

10.子类所继承父类的成员都可以在子类中访问。

( )

11.Java语言规定在嵌套的程序块中不允许定义同名的成员变量。 ( )

12.Java语言采用16位颜色标准。Java的调色板保证128色。

13.静态方法只能处理静态变量。

( )

( )

14.画布(Canvas)是一种可容纳多个组件的容器。

15.Java语言是一种强类型语言,数据类型转换有两种:隐含转换和强制转换。 ( ) 16.Java语言中,数组在静态和动态赋值时都判越界。

17.Frame容器是有边框的容器,它也是一种独立窗口,只能作为最外层容器。18.集合Set是通过键-值对的方式来存储对象的。

19.异常处理是在编译时进行的。

20.死锁的产生是因为多个线程间存在资源竞争。

四、阅读下列程序,请写出程序的输出结果

1. class B { int b; B(int x) { b = x;

System.out.println(\

}

}

class A extends B { int a;

A(int x, int y) { super(x); a = y;

System.out.println(\,a=\

}

}

public class Test {

( )

( )

( ( )

)

}

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

A obj = new A(13, 23);

答案 b=13 b=13,a=23

2.

public class Test1 { } class AB { }

String s1; String s2;

AB(String str1, String str2) { }

public String toString() { }

return s1 + s2; s1 = str1; s2 = str2;

public static void main(String[] agrs) { }

AB s = new AB(\System.out.println(s.toString());

答案 Hello!I love JAVA.

3.public class Test {

public static void main(String[] args) {

int[][] num1 = new int[2][3]; num1[0][0] = 3;

}

}

num1[0][1] = 9; num1[0][2] = 60; num1[1][0] = 78; num1[1][1] = 79; for (int i = 0; i <= 1; i++) { }

for (int j = 0; j < num1[i].length; j++)

System.out.print(num1[i][j] + \

System.out.println();

答案

3 9 60 78 79 0

4. public class Test {

public static void main(String[] agrs) { }

int i, s = 0;

int a[] = { 10, 20, 30, 40, 50, 60, 70, 80, 90 }; for (i = 0; i < a.length; i++) }

if (a[i] % 3 == 0) { }

s += a[i];

System.out.println(\

答案 s=30 s=90 s=180

5. public class Test3 {

public static void main(String[] agrs) { }

} class A {

private int privateVar; public A(int _privateVar) { boolean isEqualTo(A anotherA) { } }

if (this.privateVar == anotherA.privateVar) else

return false;

return true;

privateVar = _privateVar; }

A a = new A(2);

A b = new A(3);

System.out.println(a.isEqualTo(b));

答案 false

6. public class Test {

public static void main(String[] args) {

int[][] num1 = new int[2][3]; num1[0][0] = 3;

num1[0][1] = 9; num1[1][1] = 79;

num1[0][2] = 45;

num1[1][0] = 78;

for (int i = 0; i <= 1; i++) { }

int[][] num2 = new int[][] { { 1, 2, 3 }, { 7, 8 } ,{ 9 } }; for (int i = 0; i

for (int j = 0; j < num2[i].length; j++)

System.out.print(num2[i][j] + \for (int j = 0; j < num1[i].length; j++)

System.out.print(num1[i][j] + \

System.out.println();

}

}

}

System.out.println();

答案

3 9 45 78 79 0 1 2 3 7 8 9

7. public class Test2 {

public static void main(String[] agrs) { }

int i, s = 0;

int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; for (i = 0; i < a.length; i++) }

if (a[i] % 3 == 0) { }

s += a[i];

System.out.println(\

答案 s=3 s=9 s=18

8. class B {

int b; B(int x) { }

b = x;

System.out.println(\

}

class A extends B {

int a;

A(int x, int y) { } }

public class Test { }

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

A obj = new A(5, 10); super(x); a = y;

System.out.println(\,a=\

答案 b=5 b=5,a=10

9. public class Test4 {

public static void main(String[] agrs) { }

static void method() {

try {

wrench(); try {

method();

} catch (Exception e) {

System.out.println(\

} finally { }

System.out.println(\

}

System.out.println(\

} catch (ArithmeticException e) {

System.out.println(\

} finally { }

System.out.println(\

System.out.println(\

static void wrench() { } }

throw new NullPointerException();

答案 E A B

五、阅读程序,回答问题

1.

1: public class Output1 {

2: public static void main(String arge[]) { 3: int i=0;

4: for ( char ch = 97; ch<113; ch++,i++) { 5: if( i % 8 == 0 )

6: System.out.println(\

7: System.out.print(\ 8: } 9: } 10: }

(1)程序第5、6行的if语句的功能是什么? (2)程序输出的结果有几行?

2.

1: import java.util.Arrays; 2: public class SortArray {

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

4: String[] str = {\5: Arrays.sort(str);

6: for (int i=0; i

(1)写出程序运行后的结果。

(2)在第4行和5行之间,进行str[0].length()方法调用的返回结果是多少? 3.

abstract class SuperAbstract{ }

interface AsSuper{ }

abstract class SubAbstract extends SuperAbstract implements AsSuper{

public void b(){…} }

public class InheritAbstract extends SubAbstract{

public void x(){…} public int c(int i ) {…} public String f(){…}

public static void main(String args[]){ abstract String f(); void x(); void a(){…} abstract void b(); abstract int c(int i);

}

}

InheritAbstract instance=new InheritAbstract(); instance.x(); instance.a(); instance.b(); instance.c(100);

System.out.println(instance.f());

根据以上这段程序,回答问题: (1)哪几个类是抽象类,写出类名。 (2)哪几个类是非抽象类,写出类名。 (3)是否有接口,写出接口名。 4.定义类A和类B如下。 class A{

int a=1;

double d=2.0; void show( ) {

System.out.println(\ } }

class B extends A{

float a=3.0f; String d=\ void show( ) { super.show( );

System.out.println(\ a=\ } }

若在应用程序的main方法中有以下语句:

A a=new A();

a.show();

则输出的结果如何?Class A: a=1 d=2.0

5.写出此程序完成的功能。 public class Sum { }

回答问题:以上程序完成什么功能? 运行结果是:sum=5.187377517639621

6.写出此程序完成的功能。 import java.io.*; public class ArrayCount {

public static void main(String[] args) {

int i, n = 10, sum = 0, count = 0; int a[] = new int[10]; for (i = 0; i < n; i++)

try {

BufferedReader br = new BufferedReader(new

InputStreamReader(System.in));

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

double sum = 0.0; for (int i = 1; i <= 100; i++)

sum += 1.0 / (double) i;

System.out.println(\

a[i] = Integer.parseInt(br.readLine());

} catch (IOException e) { }

for (i = 0; i < n; i++)

sum += a[i];

}

}

sum /= n;

for (i = 0; i < n; i++)

if (a[i] < sum)

++count;

System.out.println(count);

7.

import java.util.*; public class Test4{

public static void main(String args[]) {

}

(1)程序运行后输出的结果如何?

size = 3 5 10 15

}

Set set = new TreeSet(); set.add(new Integer(10)); set.add(new Integer(5)); set.add(new Integer(15)); set.add(new Integer(5)); set.add(new Integer(10));

System.out.println(\Iterator it = set.iterator(); while (it.hasNext()) { }

System.out.print(it.next() + \ \

(2)说明java中的集合(Set接口)和映射(Map接口)的主要区别。 8.

1: import java.applet.Applet;

2: import java.awt.*; 3: import java.awt.event.*;

4: public class TestKeyListener extends Applet { 5: Color clr; 6: public void init() { 7: addKeyListener( 8: new KeyAdapter() {

9: public void keyTyped( KeyEvent e ) {

10: if(e.getKeyChar()=='r'||e.getKeyChar()=='R') 11: clr=Color.red;

12: else if(e.getKeyChar()=='g'||e.getKeyChar()=='G') 13: clr=Color.green;

14: else if(e.getKeyChar()=='b'||e.getKeyChar()=='B') 15: clr=Color.blue; 16: else

17: clr=Color.black; 18: setBackground(clr); 19: } 20: }); 21: } 22: }

(1)程序第10、11行实现的功能是什么?返回引发键盘事件的按键所对应的Unicode字符,如果为大写R或小写r则让变量clr等于Color类red方法。

(2)编译该程序,并编写嵌入该程序字节码文件的网页后,通过支持java的浏览器打开网页。如果敲击键盘的‘b’键,有什么结果?

六、程序设计题

1. 设计一个Calculator类,并完成下列的各方法的程序设计并测试: (1)定义add()方法,用来计算两数(a,b)之和。 (2)定义sub()方法,用来计算两数(a,b)之差。

(3)定义mul()方法,用来计算两数(a,b)之积。 (4)定义div()方法,用来计算a/b。

public class kk {

int a, b;

int add() {

return (a + b); }

int sub() {

return (a - b); }

int mul() {

return (a * b); }

int div() { if (b != 0)

return (a / b); else

return 0; }

public static void main(String[] args) { kk rect = new kk(); rect.a = 10; rect.b = 6;

System.out.println(\ + rect.add()); System.out.println(\ + rect.sub()); System.out.println(\ + rect.mul()); System.out.println(\ + rect.div()); } } 2.

定义一个ArrayList集合对象,往其中添加随机产生的0-10之间的float类型数据,共添加10个,并打印输出该集合对象中每个元素的值。 import java.util.ArrayList; import java.util.Iterator;

public class Test {

public static void main(String[] args) {

ArrayList list = new ArrayList(); for (int i = 0; i < 10; i++)

list.add(i, (float) Math.random()*10);

for(int j=0;j

Iterator it=list.iterator();

while(it.hasNext()){ System.out.println(it.next());

}

}

}

3. 试利用do while循环,计算10!。

public class Sum {

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

for(int i=1;i<=10;i++) {

s*=i; }

System.out.println(s); } }

其他问题:

1、 用输入流读、并统计用户输入信息中的字母、数字 import java.io.*; public class Test11 {

public static void main(String[] args) throws IOException { int nDig=0,nChar=0,nOther=0; InputStreamReader in=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(in); System.out.println(\请输入:\ String str=br.readLine(); for(int i=0;i

if(str.charAt(i)>='0'&&str.charAt(i)<='9'){ nDig++; }else if(str.charAt(i)>='a'&&str.charAt(i)<='z'||str.charAt(i)>='A'&&str.charAt(i)<='Z'){ nChar++; }else nOther++; } System.out.println(\字母:\ System.out.println(\数字:\ System.out.println(\其它字符:\ } }

2、循环语句的使用

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

Top