Java基础入门习题答案doc.docx
- 文档编号:25960840
- 上传时间:2023-06-16
- 格式:DOCX
- 页数:40
- 大小:51.81KB
Java基础入门习题答案doc.docx
《Java基础入门习题答案doc.docx》由会员分享,可在线阅读,更多相关《Java基础入门习题答案doc.docx(40页珍藏版)》请在冰豆网上搜索。
Java基础入门习题答案doc
《Java基础入门》习题答案
第1章Java开发入门
一、填空题
1、JavaEE>JavaSE>JavaME
2、JRE
3、javac
4、bin
5^path、classpath
二、选择题
1、ABCD2、C3、D4、B5>B
三、简答题
1、面向对象、跨平台性、健壮性、安全性、可移植性、多线程性、动态性等。
2、JRE(JavaRuntimeEnvironment,Java运行时坏境),它相当于操作系统部分,提供了Java程序运行时所需要的基本条件和许多Java基础类,例如,IO类、GUI控件类、网络类等。
JRE是捉供给普通用户使用的,如果你只想运行别人开发好的Java程序,那么,你的计算机上必须且只需安装JREo
JDK(JavaDevelopmentKit,Java开发工具包),它包含编译工具、解释工具、文档制作工具、打包工具多种与开发相关的工具,是提供给Java开发人员使用的。
初学者学习和使用Java语言时,首先必须下载和安装JDK。
JDK中已经包含了JRE部分,初学者安装JDK后不必再去下载和安装JRE了。
四、编程题
publicclassHelioWorld{
publicstaticvoidmain(String[]args){
System.out.printIn(n这是第一个Java程序!
");
}
}
第2章Java编程基础
一、填空题
1、
class
2、
tnie和false
3、
单行注释、多行注释、文档注释
4、
基本数据类型、引川数据类型
5、
1、2、4、8
6、
&&&丨II
7、
0
8、
5
9、34
10、56
二、判断题
1、错2、对3、错4、对5、错
三、选择题
1、AD2、AD3、C4、ABCD5^C6、A7、AC8、A9、B10、A
四、程序分析题
1、编译不通过。
int值4和b相加时,由于变量b的类型为by©取值范围没有int类型大,存不卜「int类型的值,因此编译不通过。
2、编译不通过。
这是因为y是在故里层的代码块中定义的一个变虽,只冇在那个代码块中才可使用,在使用y=x;语句时已经超过了y变最的作用域,所以编译无法通过。
3、打印结果为:
3o
4、打印结果为:
9
8
7
五、问答题
1、Java语言的八种基本数据类型有:
byte字节型,占一个字节。
short短整型,占两个字节。
int整型,占4个字节。
long长整型,占8个字节。
float单精度浮点型,占4个字节。
double双精度浮点型,占8个字节。
char字符型,占两个字节。
boolean型,表示逻辑值,冇true和false两个值,分别占一个字节。
2、如果使用“&”在表达式Z间进行连接,那么无论任何情况,“&”两边的表达式都会参与计算。
如果使用“&&”进行连接,当“&&”左边的表达式为false,则不会执行其右边的表达式。
例如定义intx=2,y=0;booleanb=x
被0除界常,因为x/y的表达式执行了。
而booleanb=x
3、方法重载指的是在一个类中可以声明多个同名的方法,而方法中参数的个数或者数据类型不一•致。
调用这些同名的方法时,JVM会根据实际参数的不同绑定到不同的方法。
六、编程题
1、参考答案
publicclassTestOl{
publicstaticvoidmain(String[]args){
intsum=0;
for(inti=1;iv100;i++){
if(i%2!
=0)
sum+=i;
}
System.out.printin(sum);
}
}
2、参考答案
publicclassTest02{
publicstaticvoidmain(Stringargs[]){
inty=function(0);
System.out:
.println(y);
}
publicstaticintfunction(intx){inty;
if(x>0){
y=x+3;
}elseif(x==0){
y=o;
}else{
y=x*x-1;
}
returny;
}
}
3、参考答案
publicclassTest03{
publicstaticvoidmain(String[]args){
int[]arr={25,24,12,76,101,96,28};
for(inti=0;ivarr.length-1;i++){
//定义内层循环
for(intj=0;j if(arr[j]>arr[j+1]){//比较相邻元素 //下面的三行代码用于交换两个元素 inttemp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } for(inti=0;i System.out.print(arr[i]+"");//打印元素和空格} } } 第3章面向对象(上) 一、填空题 1、封装、继承、多态 2、new 3、成员变量、局部变量 4、类、类 5^this 6、finalize() 7、静态变最 8、内部类 9、javadoc 1()>private 二、判断题 1、对2、对3、错4、对5、错 三、选择题 1、B2、D3、B4、ABC5、ABCD6、ACD7、ABCD8、ABCD9、D1()、D 四、程序分析题 1、程序不能编译通过,因为在类A中的成员变量secretMJprivate修饰,所以在类Testi中无法访问。 2、程序不能编译通过,因为在静态方法method()中不能访问非静态成员变量x。 3、程序能够编译通过,运行的结果为"inner 五、简答题 1、构造方法是类的一个特殊成员,它会在类实例化对象时被自动调用。 而普通方法只有在使用的时候才会被调用。 在定义构造方法时要求方法名与类名相同、在方法名的前面没冇返回值类型的声明、在方法中不能使用retum语句返冋一个值 2、单例模式可以保证在整个程序运行期间针对该类只存在一个实例对彖。 六、编程题 1、参考答案 classStudent{ privateStringname; privatedoublegrade; publicStudent(){ } publicStudent(Stringnamerdoublegrade){ this・name=name; this.grade=grade; } publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicdoublegetGrade(){ returngrade; } publicvoidsetGrade(doublegrade){ this・grade=grade; } } publicclassTestOl{ publicstaticvoidmain(String[]args){ Studentstul=newStudent(); stul・setName("zhangsan"); stul・setGrade(99); Studentstu2=newStudent("lisi"#100); } } 2、参考答案 classFather{ privateStringname='•zhangjun11; classChild{ publicvoidintroFather(){ System.out: .printin(nMyFather'snameis11+name);} } } publicclassTest02{ publicstaticvoidmain(String[]args){ Father.Childchild=newFather{).newChild(); child・introFather(); } 第4章面向对象(下) 一、填空题 1、继承 2、方法,抽象类 3、import 4、子类、父类、基类 5sException 6^final 四、程序分析题 1、程序编译能通过,这是因为intx=2/0;System.out.println(x);ii两条语句使用了try块,捕获了程序因为除以0而产生的异常情况,之后程序会继续向下执行,输出“进入catch代码块”「'进入finally代码块”。 2、程序编译不通过,这是因为在程序屮使用了final关键字修饰Animal类,使得Animal类不能被继承。 shout()方法中同样使用了final关键字,使得该方法不能被重写。 3、程序编译能通过,输出结果为“动物叫! ”和“汪汪……”,因为在程序中调用shout()方法时,首先会通过super.shout()调用父类的方法说岀“动物叫! ”之后再输出“汪汪……” 4、程序编译不通过,因为接口中定义的方法不能有方法体,所以定义的eat()方法是错误的。 接口中的方法必须在子类中全部实现,由于nm()方法在子类中并没有重新实现,所以这也是错误的。 五、简答题 1、在继承关系中,子类的方法与父类的某一方法具有相同的方法名、返回类型和参数列表,则称子类的该方法重写(覆盖)父类的方法。 2、多态意味着一个对彖冇着多种形态,可以在特定的情况下,表现不同的状态,从而对应着不同的属性和方法。 简单的说,多态就是使川父类类型的变量引用子类对象,根据被引用子类对象的特性,程序会得到不同的运行效果。 3、在Java中,使用abstract关键字修饰的类称Z为抽象类。 抽象类是不能被实例化的,通常需要写一个子类来继承抽彖类,同吋实例化子类来获得该类的对象。 抽彖类通常用于表示一种抽象的概念。 接口可以说是-•种特殊的抽象类,接口中只能定义常量和抽象方法。 由于接口的特殊性,在定义时需要使用interface关键字。 六、编程题 1、参考答案 classStudent{ publicStringname; publicintage; publicStudent(Stringnamerintage){ this・name=name; this・age=age; } publicvoidshow(){ System・out・printin(nname: H+name+nage: H+age); } } classUnderGraduateextendsStudent{ publicStringdegree; publicUnderGraduate(Stringname,intagezStringdegree){ super(namezage); this・degree=degree; } publicvoidshow(){ System・out・printin(nname: H+name+"age: H+age+Hdegree: H+degree); } } publicclassTest01{ publicstaticvoidmain(String[]args){ Studentstudent=newStudent(Mzhangsann,16); student・show(); UnderGraduateunderGraduate=newUnderGraduate("lisi"z20fMbechalor11);underGraduate・show(); doublearea{doublegivenValue); } classSquareimplementsShape{ publicdoublearea(doublesideLength){returnsideLength*sideLength; } } classCircleimplementsShape{ publicdoublearea(doubler){ returnMath・PI*r*r; } } publicclassTest02{ publicstaticvoidmain(String[]args){ Shapesquare=newSquare(); Shapecircle=newCircle(); System.out・printIn(square・area (2)); System.out.printIn(circle・area(3)); } } 3、参考答案 classNoThisSongExceptionextendsException{ publicNoThisSongException{){ super(); } publicNoThisSongException{Stringmessage){ super(message); } } classPlayer{ publicvoidplay{intindex)throwsNoThisSongException{if(index>10){ thrownewNoThisSongException("您播放的毗III不存在");} System.out.printin(11正在播放歌曲"); } } publicclassTest03{ publicstaticvoidmain(String[]args){ Playerplayer=newPlayer{); try{ player.play{13); }catch(NoThisSongExceptione){ System.out・printin(”异常信息为: H+e.getMessage()); } 第5章多线程 一、填空题 1、线程、通信 2^ThreadRunnable 3、就绪 4^synchronized>对象、this 5、进程 6^新建状态(New)>就绪状态(Runnable)>运行状态(Running)、阻塞状态(Blocked)>死亡状态(Terminated) 7、10、1 8、开启一个新线程、run()方法 9^wait()>notify()>notifyAll() 10^setDaemon(true)>start() 二、判断题 1、错2、对3、对*4、错5、错 三、选择题 1、B2、AC3、ABC4、BC5、ABD6、ABC7、C8、D9、AB10、ABCD 四、程序分析题 1、程序不能编译通过,因为RunHandler类没有实现Runnable接口,因此RunHandler的实例对象不能作为参数传递给Thread的构造方法。 2、程序不能编译通过,因为Thread的子类A重写的mn()方法的访问级别不能低于父类nm()方法的。 访问级别 3、程序不能编译通过,因为同步方法中调用wait()方法的对彖必须为同步锁对彖。 4、t.start(); 五、简答题 1、种是继承java.lang包下的Thread类,覆写Thread类的run()方法,在run()方法中实现运彳亍在线程上的代码。 newThread(){ publicvoidrun(){} }.start(); 另一种就是实现java.lang.Runnable接口,同样是在run()方法屮实现运彳亍在线程上的代码。 newThread(newRunnable(){ publicvoidrun(){} }).start0 2、调用sleep。 方法,正在执行的线程主动让出CPU去执行其他线程,在sleep。 方法指定的时间过后,CPU才会回到这个线程上继续往下执行,如果当前线程进入了同步锁,sleep()方法并不会释放锁,即使当前线程使用sleep()方法让出了CPU,但其它被同步锁扌当住了的线程也无法得到执行。 wait()在一个已经进入了同步锁的线程内进行调用,让当前线程暂时让出同步锁,以便其它正在等待此锁的线程可以得到同步锁并运行。 生其它线程调用了notify。 方法后,调Mwait()方法的线程就会解除wait状态,当再次获得同步锁后,程序可以继续向下执行。 六、编程题 1、参考答案 publicclassMyThreadextendsThread} publicMyThread(Stringname){ super(name); } publicvoidrun(){ System.out.printin(this・getName{)); } publicstaticvoidmain(String[]args){ newMyThread("Threadl11)・start(); newMyThread(HThread211).start(); } } 2、参考答案 publicclassMyRunnableimplementsRunnable{ publicvoidrun(){ for(inti=0;i<50;i++){System.out: printin("new"); } } publicstaticvoidmain(String[]args){ newThread(newMyRunnable())•start(); for(inti=0;i<100;i++){ System.out.printin(nmainn); } } } 3、参考答案 publicclassTestOl{ publicstaticvoidmain(String[]args){ Teachert=newTeacher(); newThread(tf”陈老0帀 newThread(t,"高老0帀")•start(); newThread(tr"李老0币”)•start(); } } classTeacherimplementsRunnable{ privateintnotes=80; publicvoidrun(){ while(true){ dispatchNotes();//调用售票方法 if(notes<=0){ break; } } } privatesynchronizedvoiddispatchNotes(){ if(notes>0){ try{ Thread,sleep(10);//经过的线程休眠10亳秒 }catch(InterruptedExceptione){ e.printStackTrace(); 「「发岀的笔记" } System・out・printin(Thread・currentThread()・getName()++notes--); } } } 4、参考答案 publicclassAccumulatorextendsThread{ privateintstratNum; publicstaticintsum; publicAccumulator(intstartNum){ this・stratNum=startNum; } publicstaticsynchronizedvoidadd(intnum){ sum+=num; } publicvoidrun(){ intsum=0; for(inti=0;i<10;i++){ sum+=stratNum+i; } add(sum); } publicstaticvoidmain(String[]args)throwsException{ Thread[]threadList=newThread[10]; for(inti=0;iv10;i++){ threadList[i]=newAccumulator(10*i+1);threadList[i]・start(); } for(inti=0;i<10;i++){ threadList[i]・join(); } System.out: .println("Sumis: "+sum);} } 第6章JavaAPI 一、填空题 1>StringsStringBuffer 2、Date^Calendar>DateFormat 3、getRuntime() 4、sqrt() 5、DateFormat 6、n、e 7、Random、java.util 8^length() 9、静态 10、edcba 二、判断题 1、错2、错3、对4、错5、对 三、选择题 1、C2、C3、D4、C5、C6、B7、C8、A9、A10、B 四、程序分析题 1、程序编译能通过,输出结果如下 5 7・0 -8.0 -5 8.1 ・6>1 2、程序编译能通过,输出结果如下 str・length(): 15 str.charAt: (0): d lastlndexOf(m): 10 substring(2,4): fe indexOf(g): 5 五、简答题 1、String类是不可变类,即字符串值一旦初始化后就不可能改变。 StringBuffer是可变字符串类,类似String的缓冲区,可以修改字符串的值。 2、Date类用来表示某个特定的瞬间,能够精确到毫秒。 而在实际应用屮,往往需要把一个口期
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 基础 入门 习题 答案 doc
![提示](https://static.bdocx.com/images/bang_tan.gif)