山东大学java期末讲完第12章之后习题课内容及其答案讲课字体版.docx
《山东大学java期末讲完第12章之后习题课内容及其答案讲课字体版.docx》由会员分享,可在线阅读,更多相关《山东大学java期末讲完第12章之后习题课内容及其答案讲课字体版.docx(15页珍藏版)》请在冰豆网上搜索。
山东大学java期末讲完第12章之后习题课内容及其答案讲课字体版
▪第十一章要求:
▪分析自测题4,5,6的结果.(自己设计的题目难度将以此做为基础!
)
▪复习要求:
能够设计一般难度的递归程序,能够根据递归的基本思想和技巧看懂较复杂的递归程序。
本章作业:
1.考虑将以前章节的某些算法改写成递归算法。
(可以参考其他语言的递归程序的写法)
2.写出字符串全排列输出的递归程序。
(参考)
3.分析RecursionDemo和RecursionDemo2的结果。
▪第十二章要求:
▪总结1:
主要参考程序:
10.4(ListNode.java),
10.5(StringLinkedList.java),
10.8(LinkedListDemo.java)----见课后邮件
▪掌握:
认真读懂课后程序10.4,10.5,10.8,熟练掌握访问链表的各个方法。
▪作业:
1。
课堂所讲程序为基本程序,需熟练掌握。
2.很多方法也是基本方法,实际编程时要求有很多变化。
例如:
在具有某
个数据特征的节点后面插入节点,或第N个节点后面插入节点。
▪参考题:
将链表某些满足一定条件和特征的节点的信息存储到一个数据结构中,如何处理?
(问题:
访问链表数据参考以前讲过的什么方法?
)
备注:
本文档比“期末(讲完第11章之后)习题课内容及其答案”在英文选择题上做了中文注释
习题001、选择填空(可多选)
(1).下列哪是有效的浮点数变量的声明:
(A,D,F.G)√
A:
floatfff=-1;
B:
floatfff=1.0;
C:
floatfff=42e1;
D:
floatfff=2.02f;
E:
floatfff=3.03d;
F:
floatfff=0x0123;
G:
shortb=31000;
(2).下面哪个逻辑表达式的值为真:
(A,B,c)√
intd=10;
doublee=10.0;
A:
(d==e)
B:
(d==10.0)
C:
(e==10)
(3).分析以下程序段:
√
....
....
Stringfff="base";
fff.substring(0,3);
fff.concat("ket");
该段程序执行后fff的结果为()
(base)(因为String的方法操作不变性0
以下英文试题为单选题:
(4)whicharelegaldeclarations?
(c)(p14)
Selectallrightanswers:
A.short[5]x2;
B.shortz2[5];
C:
short[]z[][];
D:
short[]y2=[5];
[考察数组的声明]
(5)whatistheresultwhenyoucompileandrunthefollowingcode?
(D)(p25)√
ClassExample{
StaticintmyArg=1;
Publicstaticvoidmain(String[]args){
intmyArg;
System.out.println(myArg);
}
}
Selectallrightanswers:
A.Thiscodecompilesanddisplays0inthestandardoutputwhenrun;
B.Thiscodecompilesanddisplays1inthestandardoutputwhenrun;
C:
Thiscodedoesnotcompilebecauseyoucannotdefinealocalvariablenamedthesameasastaticvariable;
D:
Thiscodedoesnotcompilebecausethelocalvariableisusedbeforeitisinitialized.
[注意区分实例变量和局部变量初始化的不同:
实例变量在创建对象时会根据类型赋予默认值,如:
int型变量赋予0。
而方法内的局部变量必须使用前赋值,即使创建对象后局部变量也要赋予初值]
//下题已经在讲完第7章之后的习题课中出现
(6)whatistheresultwhenyoucompileandrunthefollowingcode?
(D)(p96)
ClassExample{
Publicstaticvoidmain(String[]args){
If(newBoolean(“true”)==newBoolean(“true”)){
System.out.println(“True”);
}
else{System.out.println(“False”);
}
}
}
Selectallrightanswers:
A.Compilationerror;
B.Nocompilationerror,butruntimeexception;
C:
Prints“True”;
D:
Prints“False”.
[考察两个对象的比较:
两个属于包装类的对象的比较,就是两个一般对象的比较]
(7)whatistheresultwhenyoucompileandrunthefollowingcode?
(C)(p157)√
ClassExample{
privateintm;
Publicstaticvoidfun(){
//somecode…………….
}
}
Howcanthemembervariablembeaccessibledirectlyinthemethodfun()?
Selectallrightanswers:
A.changeprivateintmtoprotectedintm;
B.changeprivateintmtopublicintm;
C:
changeprivateintmtostaticintm;
D:
changeprivateintmtointm.
[考察静态方法对其他方法和变量的访问]
(8)whatistheresultwhenyoucompileandrunthefollowingcode?
(D)(p169)√
publicClassExample{
Publicstaticvoidmain(String[]args){
intx=5;
Examplep=newExample();
p.doStuff(x);
System.out.print(“mainx=”+x);
}
voiddostuff(intx){
System.out.print(“dostuffx=”+x++);
}
}
Selectallrightanswers:
A.Compilationfails;
B.Anexceptionisthrownatruntime;
C:
doStuffx=6mainx=6;
D:
doStuffx=5mainx=5;
E:
doStuffx=5mainx=6;
F:
doStuffx=6mainx=5;
[考察静态方法对非静态方法的调用,以及局部变量的作用范围]
(9)whatistheresultwhenyoucompileandrunthefollowingcode?
(A)(p182)
ClassExample{
Strings=”Hello”;
publicstaticvoidmain(String[]args){
Exampleh=newExample();
h.methodA(s);
System.out.println(h.s);
}
publicstaticvoidmethodA(Strings){
s.replace(‘a’,’e’);
s+=”World!
!
!
”;
}
}
Selectallrightanswers:
A.Compileerrorbecausestringisnotstatic;
B.prints“HelloWorld!
!
!
”.
C:
prints“Hello”.
D:
prints“World!
!
!
”.
E:
Runtimeexceptionoccurs.
[考察静态方法对变量的访问]
(10)whatistheresultwhenyoucompileandrunthefollowingcode?
(B)(p207)
(11)Giventhefollowingcode:
(A)(p242)
Switch(s){
Default:
System.out.println(“BestWishes”);
}
Whichdatatypecan‘s’be?
selectallrightanswer:
A:
byte
B:
long
C:
float
D:
double
[考察Switch语句]
(11+1)whatistheresultwhenyoucompileandrunthefollowingcode?
(A)(p243)
(12)whatistheresultwhenyoucompileandrunthefollowingcode?
(F)(p268)
ClassA{
voidprocess()throwsException{
ThrownewException();
}
}
ClassBextendsA{
voidprocess(){
System.out.println(“B”);
}
publicstaticvoidmain(Stringargs[]){
Aa=newB();
a.process();
}
}
Selectallrightanswers:
A.prints“B”.
B.Thecodeexceptionisthrownatruntime.
C:
Thecoderunwithnooutput.
D:
compilationfailsbecauseofanerrorinline10.
E:
compilationfailsbecauseofanerrorinline11.
F:
compilationfailsbecauseofanerrorinline12.
(该题目考察方法对所抛出的异常的处理)
(备注:
程序在编译a.process();会报错,因为main方法中忽视了异常处理)
(或者说:
尽管程序员想实际执行classB中的方法process(),但编译阶段就会报错)
(该题目仅供参考)
(13)whatistheresultwhenyoucompileandrunthefollowingcode?
(C)(p321)(t)
(14)whatistheresultwhenyoucompileandrunthefollowingcode?
(B)(p337)√
PublicclassExample{
staticintage;
Publicstaticvoidmain(String[]args){
age=age+1;
System.out.println(“Theageis”+age);
}
}
Selectallrightanswers:
A.Compilesandrunswithnooutput;
B.CompilesandrunsprintingoutTheageis1;
C:
Compilesbutgeneratesaruntimeerror;
D:
Doesnotcompile;
E:
Compilesbutgenerateacompiletimeerror.
[该题目考察静态变量的初始化方法,静态变量在声明后会根据类型赋予默认值]
(15)whatistheresultwhenyoucompileandrunthefollowingcode?
(B)(p351)√
ClassBase{
publicvoidBase(){
System.out.println(“Base”);
}
}
PublicclassExampleextendsBase{
Publicstaticvoidmain(String[]args){
Examplei=newExample();
}
}
Selectallrightanswers:
A.CompiletimeerrorBaseisakeyword;
B.Compilationandnooutputatruntime;
C:
OutputofBase;
D:
RuntimeerrorBasehasnovalidconstructor.
[该题目考察构造函数,本例中父类Base中的Base()不是构造函数,只是一个方法]
(16)注意英文的语气:
If某种条件,allofthefollowingarelegalexceptwhichassignmentstatement?
(17)注意英文的语气:
In某种场合或条件,WhichofthefollowingisnotawaythataJavaprogramcouldhandleanexception?
(18)referstopolymorphicvariable的问题
习题01:
阅读下面异常处理程序,写出执行结果(6分)√
publicclassExceptionDemo{
publicstaticvoidmain(String[]args){
ExceptionTestet=newExceptionTest();et.m1();}
}
classExceptionTest{inti;
int[]a=newint[5];
voidm1(){
try{while(true){
m2();
System.out.println();}
}catch(Exceptione){
System.out.println("m1runs");}
}
voidm2()throwsException{
try{System.out.println(10/i);
System.out.println(a[i]);
}catch(ArithmeticExceptione){
i=10;
System.out.println("handleArithmeticException");}
Finally{
System.out.println("finally");}
System.out.println("m2ends");
}
}
习题02:
阅读下列程序,并给出下列程序运行的输出结果(10分)√
classT{
voidf(intx){System.out.println("intinT:
"+x);}
voidf(doublex){System.out.println("doubleinT:
"+x);}
voidf(Objectx){System.out.println("ObjectinT:
"+x);}}
classSextendsT{
voidf(intx){System.out.println("intinS:
"+x);}}
publicclassTestOverride{
publicstaticvoidmain(String[]args){
Tt=newS();t.f(20);t.f(20L);
t.f(3.5f);t.f(3.5);t.f("abcdef");
}
}
习题03:
编写方法,用£≈1-1/3!
+1/5!
-1/7!
+(-1)n*1/(2n-1)!
公式求£的近似值,直到误差小于10-6视为稳定,输出£。
(10分)√
习题04:
要求任意名学生按照A、B、C、D等级对学生食堂的饭菜质量进行评估,并将调查的结果(四个等级的各占比例)存入文本文件result.txt.(10分)[注:
不必写出完整的类设计,编写相关的方法即可](参考题)√
习题05:
下面的程序是算2n的,填入适当的程序片断。
(7分)√
Theprogramwillcompute2n,filltheproperprogramsegmentinblank。
publicstaticdoubletentoThe(intn)
{
if____________//1、第一小题
return1 ;
elseif(n>0)
return_______________//2、第二小题
else
return_____________________//3、第三小题
}
习题06:
回答学生问题:
(可能是04-05学年考试题)(参考题)
(1)比较FileoutputStreamoo=newFileoutputStream("Hello.txt");
FileInputStreamoo=newFileInputStream("Hello.txt");
当文件”Hello.txt”不存在时,各自是什么动作?
√
(2)在try-catch-finally语句中return语句的用法√
习题07:
回答学生问题:
(处理文本文件)(参考题)
(1)输入10个小数,按升序排序,然后写入某个文本文件,使其数字之间有定界符/分割符符号存在.√
(2)某文本文件存有10个无序小数,整数间以一个、多个定界符隔开。
如何读出这些数字,并按照升序排列,并输出之.
习题07+:
(参考题)
回答学生问题:
对图9.17(处理二进制文件的程序Doubler.java)的改写要求:
(1)如何打开指定完整路径的某个二进制文件.
(2)完成某种数学计算
(3)将结果写入别的文件
习题08:
对chapter11的自测题第4,第5题的要求:
深入分析递归过程,完全掌握此类题目的写作方法
(要求自己能写出一个较完整的递归程序,起码基本逻辑正确)
习题09:
chapter11编程练习的的第3题完全版:
(原教材)
importjava.io.*;
publicclassexercise3{
publicstaticvoidmain(String[]args){
Strings="";
intn=0;
intflag=1;
intfact=0;
while(flag==1){
System.out.println("Enterainteger,calculatethefactorial(n!
):
");
n=SavitchIn.readInt();
fact=Fact(n);
System.out.println(Integer.toString(n)+"!
isequalto"+
Integer.toString(fact));
System.out.println("Doyouwanttocontinue?
(y/n)");
s=SavitchIn.readLine();
if(s.equals("y")){flag=1;
}
if(s.equals("n")){flag=0;
System.exit(0);
}
}
}
publicstaticintFact(intn){
intfact=0;
if(n==0){return
(1);}
else{return(n*Fact(n-1));}
}
}
习题10:
(可以做参考练习,本题有一定难度)
阅读下面一段递归程序,写出调用walkthrough(0,0)后,二维数组table中各个位置存放的字母,(请重新绘制table!
!
)
A
A
A
A
A
D
A
D
D
A
A
A
A
D
A
A
D
A
A
D
A
A
A
A
A
staticintwidth=5,height=5,count=0;
staticchar[][]table={{'A','A','A','A','A'},{'D','A','D','D','A'},{'A','A','A','D','A'},{'A','D','A','A','D'},{'A','A','A','A','A'}};
staticvoidwalkthrough(intx,inty){
if(table[x][y]=='A'){
table[x][y]=(char)((int)'a'+count);
count++;
if(xwalkthrough(x+1,y);
if(x>0)
walkthrough(x-1,y);
if(ywalkthrough(x,y+1);
if(y>0)
walkthrough(x,y-1);
}
}
习题11:
(别的教材里出现过,可以做参考练习,本题有一定难度)√
试编写一个递归程序,实现一串字符或一个字符数组的全排列输出:
例如:
“abc”的全排列输出是:
abc
acb
bac
bca
cba
cab
又例如:
“abcd”的全排列