Java程序设计试题及答案.docx
《Java程序设计试题及答案.docx》由会员分享,可在线阅读,更多相关《Java程序设计试题及答案.docx(26页珍藏版)》请在冰豆网上搜索。
Java程序设计试题及答案
《JAVA程序设计》试卷(A卷)
得分
阅卷人
一、选择题(每小题2分,共40分。
)
1.JDK提供的编译器是()。
(A)java.exe(B)javac.exe
(C)javap.exe(D)javaw.exe
2.以下作为Java程序入口的main方法声明正确的()。
(A)publicvoidmain(Stringargs[])
(B)publicintmain(Stringargs[])
(C)publicstaticvoidmain(Stringargs[])
(D)publicstaticintmain(Stringargs[])
3.以下标识符错误的是()。
(A)Public(B)张三 (C)class(D)main
4.java中定义字符串Strings=”pzhu”,下面操作可以取得字符串长度的是()。
(A)s.length()(B)s.length(C)s.size()(D)length(s)
5.如下定义数组,操作正确的是()。
inta[]={1,2,3};
(A)a[3]=100(B)a[0].length(C)a++(D)a.length
6.如下定义二维数组操作错误的是()。
inta[][]={{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)继承呈现的是isa的关系
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.以下程序代码错误的是()。
abstractclassP{}
classAextendsP{}
abstractclassBextendsP{}
(A)Pp=newA();(B)Pp=newB();(C)Aa=newA();(D)Pp=newP(){voidfoo(){}};
19.以下Collectionc创建有误的是()。
(A)Collectionc=newArrayList();(B)Collectionc=newLinkedList();
(C)Collectionc=newHashSet();(D)Collectionc=newHashMap();
20.以下程序代码错误的是()。
interfaceIA{
voidf();
}
(A)abstractclassAimplementsIA{}(B)classAimplementsIA{voidf(){}}
(C)classAimplementsIA{voidf(Strings){}}(D)IAa=newIA(){voidf(){}}
得分
阅卷人
二、程序阅读(每题5分,共20分)
21.阅读程序,并写出程序运行结果
publicclassT21{
staticintinit(){
System.out.println("A");
return0;
}
staticbooleantest(inti){
System.out.println("B");
returni<1;
}
staticintadd(inti){
System.out.println("C");
return++i;
}
publicstaticvoidmain(String[]args){
for(intt=init();test(t);t=add(t)){
System.out.println("D");
}
}
}
22.阅读程序,并写出程序运行结果
classTObject{
TObject(){
System.out.println("A");
}
voidm(Strings){
System.out.println("B");
}
voidm(inti){
System.out.println("C");
}
voidm(){
System.out.println("D");
}
publicStringtoString(){
return"E";
}
}
publicclassT22{
publicstaticvoidmain(String[]args){
TObjectobj=newTObject();
System.out.println(obj);
obj.m();
obj.m
(1);
obj.m("1");
}
}
23阅读程序,并写出程序运行结果
abstractclassP{
P(){
System.out.println("P");
}
abstractvoidgoo();
}
classAextendsP{
A(){
super();
}
voidgoo(){
System.out.println("A");
}
voidfoo(){
System.out.println("F");
}
}
classBextendsP{
voidgoo(){
System.out.println("B");
}
voidkoo(){
System.out.println("K");
}
}
publicclassT23{
publicstaticvoidmain(String[]args){
Aa=newA();
a.goo();
a.foo();
Bb=newB();
b.koo();
}
}
24阅读程序,并写出程序运行结果
interfaceIT{
voidt1();
voidt2();
}
abstractclassTAimplementsIT{
publicvoidt1(){
System.out.println("A");
}
publicvoidt3(){
System.out.println("B");
}
}
classTBextendsTA{
publicvoidt1(){
System.out.println("C");
}
publicvoidt2(){
System.out.println("D");
}
publicvoidt2(inti){
System.out.println("E");
}
}
publicclassT24{
publicstaticvoidmain(String[]args){
ITobj=newTB();
obj.t1();
obj.t2();
TAaObj=(TA)obj;
aObj.t1();
aObj.t3();
TBbObj=(TB)obj;
bObj.t2(100);
}
}
得分
阅卷人
三、程序填空(每空2分,共20分)
程序一:
如下程序测试Math.random生成随机数的奇偶比率,仔细阅读程序和运行结果,补全空白处的代码。
/*
*测试Math.random生成随机数的奇偶比率
*/
publicclassT25{
/*
*生成给定数量的0到1000随机整数,并把生成的随机存入到一个int数组中
*@paramintcount要生成的随机数量
*@returnint[]生成的随机数存储数组
*/
int[]createArray(intcount){
intnumber[]=(25);//创建长度为count的int数组
for(inti=0;iintn=(int)(Math.random()*1000);
number[i]=(26);//在number数组中写入生成的随机数
System.out.println("number["+i+"]="+number[i]);
}
return(27);//返回生成的数组
}
/*
*计算给定数组的奇数的比率
*@paramint[]number 要计算的数组
*@returndouble奇数的比率
*/
doublecalculateOddRate(int[]number){
intcount= (28);//读取数组元素的个数,即要计算平均数的整数个数
doubleodd=0; //奇数计数
for(intn:
number){
if((29)){//如果n是奇数,奇数计数加1
odd++;
}
}
returnodd/count;
}
publicstaticvoidmain(String[]args){
T25t=newT25();
int[]number=t.createArray(100);
doubleoddRate=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
ID
NAME
GENDER
2
name02
女
4
name04
女
部分程序如下
classStudent{
privateintid;
privateStringname;
privateStringgender;
publicStudent(intid,Stringname,Stringgender){
super();
this.id=id;
this.name=name;
this.gender=gender;
}
…………//此处省略n行
publicStringtoString(){
return"Student[id="+id+",name="+name+",gender="+gender+"]";
}
}
publicclassT30{
/*取得数据库连接*/
ConnectiongetConnection(){
……//此处省略n行
}
/*查询数据库中所有学生的数据,将一条学生信息记录转化成一个Studetn对象,
*多个记录生成多个Student,将生成的对象放入到List中,一起返回到*/
ListqueryAllStudent(){
ListstuList=(30);//创建可以存储Student的List
Connectionconn=null;
Statementst=null;
ResultSetrs=null;
try{
conn=getConnection();
st=(31).createStatement();//通过连接创建statement
rs=st.executeQuery("SELECTID,NAME,GENDERFROMStudents");
while((32)){//结果是否有记录
Studentstu=newStudent(
rs.getInt("ID"),
rs.getString("NAME"),
rs.getString("GENDER"));
(33);//把stu对象加入到stuList中
}
}catch(SQLExceptione){
e.printStackTrace();
}finally{
try{
rs.close();
st.close();
conn.close();
}catch(SQLExceptione){}
}
returnstuList;
}
/*显示List中的学生*/
voidshowStudent(ListstuList){
for(______(34)_______s:
stuList){//指明s的类型
System.out.println(s);
}
}
publicstaticvoidmain(String[]args){
T30demo=newT30();
ListstuList=demo.queryAllStudent();
demo.showStudent(stuList);
}
}
运行结果
Student[id=2,name=Name02,gender=女]
Student[id=4,name=Name04,gender=女]
得分
阅卷人
四、基本代码编写(共12分)
35、(5分)编写一个main方法,计算如下数组元素的平均值
doublesource[]={2,5,9,10,3};
36、(7分)文件名解析器,仔细阅读如下代码和运行结果,完成WindowsFileNameParse类的代码,执行后得到给定的运行结果。
interfaceFileNameParse{
voidshowSourceFileName();
StringgetDiskName();
StringgetFullFileName();
StringgetFileName();
StringgetExtendName();
StringgetDir();
}
classWindowsFileNameParseimplementsFileNameParse{
privateStringfileName;
WindowsFileNameParse(StringfileName){
this.fileName=fileName;
}
publicvoidshowSourceFileName(){
System.out.println("解析文件名:
"+this.fileName);
}
//////////////////////////////////////////////////////////////////////////////////////
//////////请完成此类的中其他方法的代码////////////////////////
}
publicclassT36{
publicstaticvoidmain(String[]args){
FileNameParsefp=newWindowsFileNameParse("d:
/MyDocuments/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:
/MyDocuments/MyJob/Pages/2012-2013-2/PageA/src/T37.java
盘符:
d
文件全名(带扩展名):
T37.java
文件名(不带扩展名):
T37
文件扩展名:
java
路径(不带盘符):
/MyDocuments/MyJob/Pages/2012-2013-2/PageA/src
附String类部分的apidoc
publicintindexOf(Stringstr)
Returnstheindexwithinthisstringofthefirstoccurrenceofthespecifiedsubstring.
Examples:
"abca".indexOf("a")return0
Parameters:
str-thesubstringtosearchfor.
Returns:
theindexofthefirstoccurrenceofthespecifiedsubstring,or-1ifthereisnosuchoccurrence.
publicintlastIndexOf(Stringstr)
Returnstheindexwithinthisstringofthelastoccurrenceofthespecifiedsubstring.Thelastoccurrenceoftheemptystring""isconsideredtooccurattheindexvaluethis.length().
Examples:
"abca".lastIndexOf("a")return3
Parameters:
str-thesubstringtosearchfor.
Returns:
theindexofthelastoccurrenceofthespecifiedsubstring,or-1ifthereisnosuchoccurrence.
publicStringsubstring(intbeginIndex)
Returnsanewstringthatisasubstringofthisstring.Thesubstringbeginswiththecharacteratthespecifiedindexandextendstotheendofthisstring.
Examples:
"Harbison".substring(3)returns"bison"
"emptiness".substring(9)returns""(anemptystring)
Parameters:
beginIndex-thebeginningindex,inclusive.
Returns:
thespecifiedsubstring.
publicStringsubstring(intbeginIndex,intendIndex)
Returnsanewstringthatisasubstringofthisstring.ThesubstringbeginsatthespecifiedbeginIndexandextendstothecharacteratindexendIndex-1.ThusthelengthofthesubstringisendIndex-beginIndex.
Examples:
"hamburger".substring(4,8