Java程序设计试题及答案Word下载.docx
- 文档编号:5600698
- 上传时间:2023-05-05
- 格式:DOCX
- 页数:26
- 大小:99.69KB
Java程序设计试题及答案Word下载.docx
《Java程序设计试题及答案Word下载.docx》由会员分享,可在线阅读,更多相关《Java程序设计试题及答案Word下载.docx(26页珍藏版)》请在冰点文库上搜索。
(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){
B"
returni<
1;
staticintadd(inti){
C"
return++i;
publicstaticvoidmain(String[]args){
for(intt=init();
test(t);
t=add(t)){
System.out.println("
D"
}
22.阅读程序,并写出程序运行结果
classTObject{
TObject(){
voidm(Strings){
voidm(inti){
voidm(){
publicStringtoString(){
return"
E"
;
publicclassT22{
TObjectobj=newTObject();
System.out.println(obj);
obj.m();
obj.m
(1);
obj.m("
1"
23阅读程序,并写出程序运行结果
abstractclassP{
P(){
P"
abstractvoidgoo();
classAextendsP{
A(){
super();
voidgoo(){
voidfoo(){
F"
classBextendsP{
voidkoo(){
K"
publicclassT23{
Aa=newA();
a.goo();
a.foo();
Bb=newB();
b.koo();
24阅读程序,并写出程序运行结果
interfaceIT{
voidt1();
voidt2();
abstractclassTAimplementsIT{
publicvoidt1(){
}
publicvoidt3(){
classTBextendsTA{
publicvoidt2(){
publicvoidt2(inti){
publicclassT24{
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;
i<
count;
i++){
intn=(int)(Math.random()*1000);
number[i]=(26);
//在number数组中写入生成的随机数
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;
T25t=newT25();
int[]number=t.createArray(100);
doubleoddRate=t.calculateOddRate(number);
奇数为:
"
+oddRate*100+"
%"
偶数为:
+(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){
this.id=id;
this.name=name;
this.gender=gender;
…………//此处省略n行
publicStringtoString(){
Student[id="
+id+"
name="
+name+"
gender="
+gender+"
]"
publicclassT30{
/*取得数据库连接*/
ConnectiongetConnection(){
……//此处省略n行
/*查询数据库中所有学生的数据,将一条学生信息记录转化成一个Studetn对象,
*多个记录生成多个Student,将生成的对象放入到List中,一起返回到*/
List<
Student>
queryAllStudent(){
List<
stuList=(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"
GENDER"
));
(33);
//把stu对象加入到stuList中
}catch(SQLExceptione){
e.printStackTrace();
}finally{
try{
rs.close();
st.close();
conn.close();
}catch(SQLExceptione){}
returnstuList;
/*显示List中的学生*/
voidshowStudent(List<
stuList){
for(______(34)_______s:
stuList){//指明s的类型
System.out.println(s);
T30demo=newT30();
stuList=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(){
解析文件名:
+this.fileName);
//////////////////////////////////////////////////////////////////////////////////////
//////////请完成此类的中其他方法的代码////////////////////////
publicclassT36{
FileNameParsefp=newWindowsFileNameParse("
d:
/MyDocuments/MyJob/Pages/2012-2013-2/PageA/src/T37.java"
fp.showSourceFileName();
盘符:
+fp.getDiskName());
文件全名(带扩展名):
+fp.getFullFileName());
文件名(不带扩展名):
+fp.getFileName());
文件扩展名:
+fp.getExtendName());
路径(不带盘符):
+fp.getDir());
/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().
"
.lastIndexOf("
)return3
theindexofthelastoccurrenceofthespecifiedsubstring,or-1ifthereisnosuchoccurrence.
publicStringsubstring(intbeginIndex)
Returnsanewstringthatisasubstringofthisstring.Thesubstringbeginswiththecharacteratthespecifiedindexandextendstotheendofthisstring.
Harbison"
.substring(3)returns"
bison"
emptiness"
.substring(9)returns"
(anemptystring)
beginIndex-thebeginningindex,inclusive.
thespecifiedsubstring.
publicStringsubstring(intbeginIndex,intendIndex)
Returnsanewstringthatisasubstringofthisstring.ThesubstringbeginsatthespecifiedbeginIndexandextendstothecharacteratindexendIndex-1.ThusthelengthofthesubstringisendIndex-beginIndex.
hamburger"
.substring(4,8)returns"
urge"
smiles"
.substring(1,5)returns"
mile"
endIndex-theendingindex,exclusive.
五、设计并编程(共8分)
37、仔细阅读给定的代码和程序运行结果,完方法size()、del()代码编写。
MyList类是可以存储字符串对象的、基于链表的List的简单实现
classMyListNode{
Stringelement;
MyListNodenextNode=null;
MyListNode(Stringelement){
this.element=element;
classMyList{
privateMyListNodefirstNode=null;
publicvoidadd(Stringelement){ //加入字符串到MyList中
MyListNodenode=newMyListNode(element);
if(firstNode==null){
firstNode=node;
}else{
MyListNodelastNode=firstNode;
while(lastNode.nextNode!
=null){
lastNode=lastNode.nextNode;
lastNode.nextNode=node;
publicintsize(){//返回MyList中节点数
//////////////////完成此方法代码////////////////
publicString[]toArray(){//将MyList中存储的所有字符串转化成String[]
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 程序设计 试题 答案