Java程序设计试题及复习资料Word文件下载.docx
- 文档编号:17078960
- 上传时间:2022-11-28
- 格式:DOCX
- 页数:22
- 大小:22.42KB
Java程序设计试题及复习资料Word文件下载.docx
《Java程序设计试题及复习资料Word文件下载.docx》由会员分享,可在线阅读,更多相关《Java程序设计试题及复习资料Word文件下载.docx(22页珍藏版)》请在冰豆网上搜索。
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){
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分)文件名解析器,仔细阅读如下代码和运行结果,完成Windows类的代码,执行后得到给定的运行结果。
interface{
voidshowSource();
StringgetDiskName();
StringgetFull();
Stringget();
StringgetExtendName();
StringgetDir();
classWindowsimplements{
privateString;
Windows(String){
this.;
publicvoidshowSource(){
解析文件名:
+this.);
//////////////////////////////////////////////////////////////////////////////////////
//////////请完成此类的中其他方法的代码////////////////////////
publicclassT36{
fp=newWindows("
d:
/MyDocuments/MyJob/Pages/2012-2013-2/PageA/src/T37.java"
fp.showSource();
盘符:
+fp.getDiskName());
文件全名(带扩展名):
+fp.getFull());
文件名(不带扩展名):
+fp.get());
文件扩展名:
+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.
thespecifieds
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 程序设计 试题 复习资料
![提示](https://static.bdocx.com/images/bang_tan.gif)