文档库 最新最全的文档下载
当前位置:文档库 › Java程序的设计试题与答案

Java程序的设计试题与答案

《JAVA程序设计》试卷(A卷)

一、选择题(每小题 2分,共 40 分。)

1.JDK提供的编译器是()。

(A)java.exe (B)javac.exe

(C)javap.exe (D)javaw.exe

2.以下作为Java程序入口的main 方法声明正确的()。

(A)public void main(String args[])

(B)public int main(String args[])

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

(D)public static int main(String args[])

3.以下标识符错误的是()。

(A)Public (B)张三(C)class (D)main

4.java中定义字符串String s=”pzhu”,下面操作可以取得字符串长度的是()。

(A)s.length() (B)s.length (C)s.size() (D)length(s)

5.如下定义数组,操作正确的是()。

int a[]={1,2,3};

(A)a[3]=100 (B)a[0].length (C)a++ (D)a.length 6.如下定义二维数组操作错误的是()。

int a[][]={{1,2},{3}};

(A)a[0][1]=200 (B)a[0].length (C)a[1][1]=100 (D)a.length 7. 以下数据类型存储空间最大的是()。

(A)byte (B)long(C)float (D)char

8. 面向对象的三大特性,不包括如下( )。

(A)异常(B)封装(C)继承(D)多态

9、关于类的定义以下说法错误()。

(A)类定义使用class关键字(B)每个类中必须有一个main方法

(C)一个包可以包含多个类(D)java中所有类都是Object类的子类

10. 关于构造方法以下说法错误的是( )。

(A)构造方法名必须与类名一致(B)构造方法可以重载

(C)构造方法是通过new来调用(D)每个类都必须编写构造方法代码

11.关于继承如下说法错误的是()。

(A)Java是单继承的(B)通过extends来定义继承

(C)所有父类方法都可以被override的(D)继承呈现的是is a的关系

12. 以下代码执行的结果是( )。

System.out.println("攀枝花学院pzhu".length());

(A)编译错误(B)运行错误(C)9 (D)14 13. 用来存储键值对的容器是( )。

(A)ArrayList (B)LinkedList (C)HashSet (D)HashMap 14、java中用来抛出异常的关键字是()。

(A)try (B)catch (C)throw (D)throws 15.关于finally块中的代码,以下说法不正确的是()。

(A)try块中的return语句会中断finally块中语句的执行

(B)无论finally块前的语句运行是否产生异常,其中的语句都会执行

(C)finally块中的语句通常中用作资源的清理

(D)try块中的System.exit(1)语句会中断finally块中语句的执行

16.关于Java字符串说法错误的是( )。

(A)Java中的字符串是常量(B)Java中的字符串不是对象

(C)Java中的字符串存储在常量池中(D)一个字符串定义后的长度不可变

17.关于JDBC操作数据库,以下说法不正确的()。

(A)JDBC只能操作MySQL数据库

(B)JDBC中定义的Connection,Statement,ResultSet都是接口

(C)JDBC操作数据库必须要有相应的实现了JDBC接口的驱动

(D)JDBC可以通过将客户端的SQL传递给数据库服务器来实现数据库的操作

18.以下程序代码错误的是()。

abstract class P{}

class A extends P{}

abstract class B extends P{}

(A)P p=new A();(B)P p=new B();(C)A a=new A();(D)P p=new P(){void foo(){}};

19.以下Collection c创建有误的是()。

(A)Collection c=new ArrayList();(B)Collection c=new LinkedList();

(C)Collection c=new HashSet();(D)Collection c=new HashMap();

20. 以下程序代码错误的是()。

interface IA{

void f();

}

(A)abstract class A implements IA{} (B)class A implements IA{void f(){}}

(D)IA a=new IA(){void f(){}}

二、程序阅读(每题 5分,共 20分)

21.阅读程序,并写出程序运行结果

public class T21 {

static int init(){

System.out.println("A");

return 0;

}

static boolean test(int i){

System.out.println("B");

return i<1;

}

static int add(int i){

System.out.println("C");

return ++i;

}

public static void main(String[] args) {

for(int t=init();test(t);t=add(t)){

System.out.println("D");

}

}

}

22.阅读程序,并写出程序运行结果

class TObject{

TObject(){

System.out.println("A");

}

void m(String s){

System.out.println("B");

}

void m(int i){

System.out.println("C");

}

void m(){

System.out.println("D");

}

public String toString(){

return "E";

}

}

public class T22 {

public static void main(String[] args) { TObject obj=new TObject();

System.out.println(obj);

obj.m();

obj.m(1);

obj.m("1");

}

}

23 阅读程序,并写出程序运行结果abstract class P{

P(){

System.out.println("P");

}

abstract void goo();

}

class A extends P{

A(){

super();

}

void goo() {

System.out.println("A");

}

void foo(){

System.out.println("F");

}

}

class B extends P{

void goo() {

System.out.println("B");

}

void koo(){

System.out.println("K");

}

}

public class T23 {

public static void main(String[] args) {

A a=new A();

a.goo();

a.foo();

B b=new B();

b.koo();

}

}

24 阅读程序,并写出程序运行结果

interface IT{

void t1();

void t2();

}

abstract class TA implements IT{ public void t1() {

System.out.println("A");

}

public void t3() {

System.out.println("B");

}

}

class TB extends TA{

public void t1() {

System.out.println("C");

}

public void t2() {

}

public void t2(int i) {

System.out.println("E");

}

}

public class T24 {

public static void main(String[] args) { IT obj=new TB();

obj.t1();

obj.t2();

TA aObj=(TA)obj;

aObj.t1();

aObj.t3();

TB bObj=(TB)obj;

bObj.t2(100);

}

}

程序一:如下程序测试Math.random生成随机数的奇偶比率,仔细阅读程序和运行结果,补全空白处的代码。

/*

* 测试Math.random生成随机数的奇偶比率

*/

public class T25 {

/*

* 生成给定数量的0到1000随机整数,并把生成的随机存入到一个int数组中

* @param int count要生成的随机数量

* @return int[] 生成的随机数存储数组

*/

int[] createArray(int count){

int number[]= (25) ; //创建长度为count的int数组

for(int i=0;i

int n=(int)(Math.random()*1000);

number[i]= (26) ;//在number数组中写入生成的随机数

System.out.println("number["+i+"]="+number[i]);

}

return (27) ; //返回生成的数组

}

/*

*计算给定数组的奇数的比率

*@param int[] number要计算的数组

*@return double 奇数的比率

*/

double calculateOddRate(int[] number){

int count=(28) ; //读取数组元素的个数,即要计算平均数的整数个数

double odd=0;//奇数计数

for(int n:number){

if( (29) ){//如果n是奇数,奇数计数加1

odd++;

}

}

return odd/count;

}

public static void main(String[] args) {

T25 t=new T25();

int[] number=t.createArray(100);

double oddRate=t.calculateOddRate(number);

System.out.println("奇数为:"+oddRate*100+"%");

System.out.println("偶数为:"+(1-oddRate)*100+"%");

}

}

运行结果:

number[0]=907

…..//此处省略98行

number[99]=598

奇数为:52.0%

偶数为:48.0%

程序二:以下程序是通过JDBC读取数据表Student的基本操作,认真阅读程序和运行结果,补全程序的空白处。

表:Students

部分程序如下

class Student{

private int id;

private String name;

private String gender;

public Student(int id, String name, String gender) {

super();

this.id = id;

https://www.wendangku.net/doc/8d17817536.html, = name;

this.gender = gender;

}

…………//此处省略n行

public String toString() {

return "Student [id=" + id + ", name=" + name + ", gender=" + gender+ "]";

}

}

public class T30 {

/*取得数据库连接*/

Connection getConnection(){

……//此处省略n行

}

/* 查询数据库中所有学生的数据,将一条学生信息记录转化成一个Studetn对象,

* 多个记录生成多个Student,将生成的对象放入到List中,一起返回到*/

List queryAllStudent(){

List stuList= (30) ;//创建可以存储Student的List

Connection conn=null;

Statement st=null;

ResultSet rs=null;

try {

conn=getConnection();

st= (31) .createStatement(); //通过连接创建statement

rs=st.executeQuery("SELECT ID,NAME,GENDER FROM Students");

while( (32) ){ //结果是否有记录

Student stu=new Student(

rs.getInt("ID"),

rs.getString("NAME"),

rs.getString("GENDER"));

(33) ; //把stu对象加入到stuList中

}

} catch (SQLException e) {

e.printStackTrace();

}finally{

try {

rs.close();

st.close();

conn.close();

} catch (SQLException e) {}

}

return stuList;

}

/*显示List中的学生*/

void showStudent(List stuList){

for(______(34)_______s:stuList){ //指明s的类型

System.out.println(s);

}

}

public static void main(String[] args) {

T30 demo=new T30();

List stuList=demo.queryAllStudent();

demo.showStudent(stuList);

}

}

运行结果

Student [id=2, name=Name02, gender=女]

Student [id=4, name=Name04, gender=女]

四、基本代码编写(共12分)

35、(5分)编写一个main方法,计算如下数组元素的平均值

double source[]={2,5,9,10,3};

36、(7分)文件名解析器,仔细阅读如下代码和运行结果,完成WindowsFileNameParse类的代码,执行后得到给定的运行结果。

interface FileNameParse{

void showSourceFileName();

String getDiskName();

String getFullFileName();

String getFileName();

String getExtendName();

String getDir();

}

class WindowsFileNameParse implements FileNameParse{

private String fileName;

WindowsFileNameParse(String fileName){

this.fileName=fileName;

}

public void showSourceFileName(){

System.out.println("解析文件名:"+this.fileName);

}

//////////////////////////////////////////////////////////////////////////////////////

//////////请完成此类的中其他方法的代码////////////////////////

}

public class T36 {

public static void main(String[] args) {

FileNameParse fp=new W indowsFileNameParse("d:/My

Documents/MyJob/Pages/2012-2013-2/PageA/src/T37.java");

fp.showSourceFileName();

System.out.println("盘符:"+fp.getDiskName());

System.out.println("文件全名(带扩展名):"+fp.getFullFileName());

System.out.println("文件名(不带扩展名):"+fp.getFileName());

System.out.println("文件扩展名:"+fp.getExtendName());

System.out.println("路径(不带盘符):"+fp.getDir());

}

}

运行结果

解析文件名:d:/My Documents/MyJob/Pages/2012-2013-2/PageA/src/T37.java

盘符:d

文件全名(带扩展名):T37.java

文件名(不带扩展名):T37

文件扩展名:java

路径(不带盘符):/My Documents/MyJob/Pages/2012-2013-2/PageA/src

附String类部分的api doc

public int indexOf(String str)

Returns the index within this string of the first occurrence of the specified substring.

Examples: "abca".indexOf("a") return 0

Parameters:

str - the substring to search for.

Returns:

the index of the first occurrence of the specified substring, or -1 if there is no such occurrence.

public int lastIndexOf(String str)

Returns the index within this string of the last occurrence of the specified substring. The last occurrence of the empty string "" is considered to occur at the index value this.length().

Examples: "abca".lastIndexOf("a") return 3

Parameters:

str - the substring to search for.

Returns:

the index of the last occurrence of the specified substring, or -1 if there is no such occurrence.

public String substring(int beginIndex)

Returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.

Examples:

"Harbison".substring(3) returns "bison"

"emptiness".substring(9) returns "" (an empty string)

Parameters:

beginIndex - the beginning index, inclusive.

Returns:

the specified substring.

public String substring(int beginIndex, int endIndex)

Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.

Examples:

"hamburger".substring(4, 8) returns "urge"

"smiles".substring(1, 5) returns "mile"

Parameters:

beginIndex - the beginning index, inclusive.

endIndex - the ending index, exclusive.

Returns:

the specified substring.

五、设计并编程(共8分)

37、仔细阅读给定的代码和程序运行结果,完方法size()、del()代码编写。

MyList类是可以存储字符串对象的、基于链表的List的简单实现

class MyListNode {

String element;

MyListNode nextNode = null;

MyListNode(String element) {

this.element = element;

}

}

class MyList {

private MyListNode firstNode = null;

public void add(String element) {//加入字符串到MyList中

MyListNode node = new MyListNode(element);

if (firstNode == null) {

firstNode = node;

} else {

MyListNode lastNode = firstNode;

while (lastNode.nextNode != null) {

lastNode = lastNode.nextNode;

}

lastNode.nextNode = node;

}

}

public int size() {//返回MyList中节点数

//////////////////完成此方法代码////////////////

}

public String[] toArray() {//将MyList中存储的所有字符串转化成String[] int count = size();

if (count == 0) {

return null;

}

String[] dest = new String[count];

MyListNode lastNode = firstNode;

int i = 0;

do {

dest[i++] = lastNode.element;

lastNode = lastNode.nextNode;

} while (lastNode != null);

return dest;

}

public void del(String element) {//删除节点元素值为element字符串的节点///////////////完成此方法代码/////////////////

}

}

public class T37 {

public static void main(String[] args) {

MyList myList = new MyList();

myList.add("s001");

myList.add("s002");

myList.add("s003");

myList.add("s004");

myList.add("s005"); System.out.println("SIZE:" + myList.size()); String sa1[] = myList.toArray(); showArray(sa1); myList.del("s001"); myList.del("s003"); myList.del("s005"); System.out.println("SIZE:" + myList.size()); String sa2[] = myList.toArray(); showArray(sa2); } static void showArray(String[] sa) { System.out.print("["); for (String s : sa) { System.out.print(s + " "); } System.out.println("]"); } }

运行结果

SIZE:5 [s001 s002 s003 s004 s005 ] SIZE:2

[s002 s004 ]

《Java 程序设计》试卷(A 卷)

评阅标准及考核说明 一、[教师答题时间: 15 分钟] 选择题(每小题 2 分,共 40 分。请将答案填在下面的表格内)

1、[三基类]B

2、[三基类] C

3、[三基类]C

4、[三基类]A

5、[三基类]D

6、[三基类]C

7、[三基类]B

8、[三基类]A

9、[三基类]B 10、[三基类]D 11、[三基类]C

12、[三基类]C

13、[三基类]D

14、[三基类]C

15、[三基类]A

16、[三基类]B

17、[三基类]A 18、[三基类]B 19、[三基类]D 20、[三基类]C

二、[教师答题时间: 10 分钟] 填空题(每小题5分,共 20 分)

21、[三基类]

A

B

D C B

22、[三基类] A E D C B

23、[三基类] P A F P K

24、[三基类]

C D C B E

三、[教师答题时间: 10 分钟] 程序填空(每空2分,共 20分) 25、[一般综合型] new

int[count]

26、[一般综合型] n

27、[一般综合型] number

28、[一般综合型] number.length 29、[一般综合型] n%2==1 30、[一般综合型] new ArrayList()或new LinkedList() 31、[一般综合型] conn 32、[一般综合型] rs.next()

33、[一般综合型] stuList.add(stu) 34、[一般综合型] Student

四、基本代码编写(共12分)

35、(5分)[一般综合型][教师答题时间: 2 分钟]

参考程序

public static void main(String[] args) { int source[]={2,5,9,10,3}; double sum=0; int count=source.length; for(int i=0;i

评分说明:

(1)评分总体原则以实现功能为最终标准,无论用什么方式实现只要程序实现了题目要求的功能的情况,没有语法错误(个别笔误除外)的情况下可得到满分 (2)没有实现题目要求功能的情况,如下步骤可以得分 a.正确写出main 方法得1分 b.写出数组的初始化的情况下可以得1分 b.写出for 循环正确得2分 c.格式基本正确得1分 36、(7分)[综合型][教师答题时间: 3 分钟]

参考程序

public String getDiskName(){(1分)

return fileName.substring(0,fileName.indexOf(":"));

}

public String getFullFileName(){(1分)

return fileName.substring(https://www.wendangku.net/doc/8d17817536.html,stIndexOf("/")+1);

}

public String getFileName(){(2分)

return fileName.substring(https://www.wendangku.net/doc/8d17817536.html,stIndexOf("/")+1, https://www.wendangku.net/doc/8d17817536.html,stIndexOf("."));

}

public String getExtendName(){(1分)

return fileName.substring(https://www.wendangku.net/doc/8d17817536.html,stIndexOf(".")+1);

}

public String getDir(){(2分)

return fileName.substring(fileName.indexOf(":")+1, https://www.wendangku.net/doc/8d17817536.html,stIndexOf("/"));

}

评分说明:

(1)评分总体原则以实现功能为最终标准,无论用什么方式实现只要程序实现了题目要求的功能的情况,没有语法错误(个别笔误除外)的情况下可得到满分

五、设计并编程(共8分)

37、(8分)[综合型][教师答题时间:5 分钟]

参考程序

public int size() {(3分)

int len = 0;

if (firstNode != null) {

len = 1;

MyListNode lastNode = firstNode;

while (lastNode.nextNode != null) {

len++;

lastNode = lastNode.nextNode;

}

}

return len;

}

public void del(String element) {(5分)

if (firstNode != null) {

if (firstNode.element.equals(element)) {

firstNode = firstNode.nextNode;

} else {

MyListNode preNode = firstNode;

MyListNode lastNode=firstNode.nextNode;

while(lastNode!=null) {

if (lastNode.element.equals(element)) {

preNode.nextNode=lastNode.nextNode;

return;

}

preNode = lastNode;

lastNode = lastNode.nextNode;

}

}

}

}

评分说明:

(1)评分总体原则以实现功能为最终标准,无论用什么方式实现只要程序实现了题目要求的功能的情况,没有语法错误(个别笔误除外)的情况下可得到满分

(2)两个方法给分如下标注。

注:考核类型是指:三基类、一般综合型和综合型。

宁可累死在路上,也不能闲死在家里!宁可去碰壁,也不能面壁。是狼就要练好牙,是羊就要练好腿。什么是奋斗?奋斗就是每天很难,可一年一年却越来越容易。不奋斗就是每天都很容易,可一年一年越来越难。能干的人,不在情绪上计较,只在做事上认真;无能的人!不在做事上认真,只在情绪上计较。拼一个春夏秋冬!赢一个无悔人生!早安!—————献给所有努力的人

相关文档