Java语言程序设计(第2版)第1-6章-课后习题答案_第1页
Java语言程序设计(第2版)第1-6章-课后习题答案_第2页
Java语言程序设计(第2版)第1-6章-课后习题答案_第3页
Java语言程序设计(第2版)第1-6章-课后习题答案_第4页
Java语言程序设计(第2版)第1-6章-课后习题答案_第5页
已阅读5页,还剩57页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

rapid development of the market economy environment to explore public servants duty consumption monetization reform has provided a good foundation. The socialization of rear service work has been launched, and rapid progress in some places and departments, duty consumption monetization of carrier and approach to management has been resolved. Third, in recent years, exploring the monetization of duty consumption has made some progress, have gained some experience and can provide reference to the comprehensive reform of the system of public servants duty consumption further. Implementing an honest canteen, standardize official entertaining management; enhancing the telecommunication expense management; elimination of County travel and countryside subsidies; research village officials capitalization management of corporate spending, and so on. Finally, group .18 session to be held in Beijing from November 9, 2013 to 12th. 35 years ago blew the third plenary session of the reform and opening up in the spring breeze, changed, affect the world; today, 35 years later, in the eyes of the nation and the world expect, again to reform mark China, ushered in the 18 session. XI General Secretary pointed out that Chinas reform has entered a crucial period and the Sham Shui Po District, must be based on greater political courage and wisdom, lose no time in deepening reform in important fields. Dares to crack a hard nut, dares to question the Rapids, which dares to break the barrier of ideas, and dare to benefit cure barriers. Deepening reform and opening up is on schedule to achieve institutional safeguards of the moderately well-off. Under the five in one the General layout of socialist modernization requirements, 18 session of the decision was a five in one and the improvement of overall scheme of reform, will promote an integrated and coordinated economic, political, cultural, social and ecological civilization construction of the five reforms and the partys construction in the area of institutional reform. The five in one programme is to achieve a comprehensive reform of institutional guarantees for objectives of build a well-off society, the smooth progress of the construction of a well-off society and reform the objectives of the programme. One, holding time and place importance on November 9, 2013 to the 18 session of the 12th Beijing since 1978, 35, have been 7 plenary session, each time on major issues of political and economic life of the country has made important deployment. In accordance with PRC political practice, often at every session of the CPC Central Committee in a plenary session was held immediately after the partys Congress, on the theme personnel, discussing election Centrals top leaders, such as the election of the Standing Committee of the political Bureau, through the Central Committee members, decisions, such as members of the Central Military Commission. The second plenary session, is held in two sessions before the general election, mainly to discuss a new State personnel issues. But by the third第1章 Java语言概述选择题1-1 在下列概念中,Java语言只保留了(B)A. 运算符重载B. 方法重载C. 指针D. 结构和联合1-2 下列关于Java语言特性的描述中,错误的是(D)A. 支持多线程操作B. Java程序与平台无关C. Java和程序可以直接访问Internet上的对象D. 支持单继承和多继承1-3 下列关于Java Application程序在结构上的特点的中,错误的是(C)A. Java程序是由一个或多个类组成的B. 组成Java程序的若干个类可以放在一个文件中,也可以放在多个文件中C. Java程序的文件名要与某个类名相同D. 组成Java程序的多个 类中,有且仅有一个主类1-4 Java程序经过编译后生成 的文件的后缀是(C)A. .objB. .exeC. .classD. .java1-5 下列关于运行字节码文件的命令行参数的描述中,正确的是(A)A. 第一个命令行参数(紧跟命令字的参数)被存放在args0中B. 第一个命令行参数被存放在args1中C. 命令行的命令字被存放在args0中D.数组args的大小与命令行参数的个数无关判断题1-1 JavaC+的语言之前问世的。(错)1-2 Java语言具有较好的安全性和可移植性及与平台无关等特性。(对)1-3 Java语言中取消了联合的概念,保留了结构概念。(错)1-4 Java语言中数据类型占内在字节数与平台无关。(对)1-5 Java语言中可用下标和指针两种方式表示数组元素。(错)1-6 Java语言的源程序不是编译型的,而是编译解释型的。(对)1-7 操作系统中进程和线程两个概念是没有区别的。(错)1-8 Java语言既是面向对象的又是面向网络的高级语言。(对)1-9 Java程序分为两大类:一类是Application程序,另一类是Applet程序。前者又称Java应用程序,后者又称为Java小应用程序。(对)1-10 Java Application程序是由多个文件组成的,其中可以有也可以没有主文件。(错)1-11 组成Java Application的若干类中,有且仅有一个主类,只有主类中含有主方法main()。(对)1-12 Java Application程序中,必有一个主方法main(),该方法有没有参数都可以。(对)1-13 Java程序中是不区分大小写字母的。(错)1-14 下述两个命令:javac和java是不同的,编译时用前者,运行时用后者。(对)1-15 Java程序编译后生成的是字节码文件,其后缀是.exe。(错)1-16 字节码文件机器是不能直接识别的,它要经过解释器,边解释边执行。(对)1-17 在Java程序中,使用import语句引入类是在解释器中实现的。(对)1-18 在运行字节码文件时,使用java命令,一定要给出字节码文件的扩展名.class。(错)1-19 Java程序中有多个类,编译后只生成一个字节码文件,其名字同主类名。(错)1-20 Java程序中出现的输出方法println()和print()是完全一致的。(错)分析程序的输出结果题1-1 程序Exerl_1.javaimport java.io.*;public class Exerl_1public static void main (String args )System.out.print(This is a );System.out.println(strint.)该程序的输出结果如下: This is a string.1-2 程序Exerl_2.javaImport java.io.*;Public class Exerl_2Public static void mian(String args )Char ch= ;System.out.println(Input a character:);Trych=(char)System.in.read();Catch(IOException e)System.out.println(The character is +ch+); 该程序的输出结果如下Input a character:kThe character is k1-3 程序Exerl_3.javaimport java.io.*;public class Exerl_3Public static void main(String args )String str= System.out.println(Input a string:);TryBufferedReader in =new BufferedReader(new InputStreamReader(System.in);str=in.readLine();Catch(IOException e )System.out.println(The string is +str+ );1-4 程序Exerl_4.javaimport java.io.*public class Exerl_4public static void main(String args )A a=new A;a.i=8;a.d=1.25;a.meth(该程序输出结果如下所示);System.out.println(ti=+a.i+,d=+a.d);class Aint i;double d;void meth(string str)System.out.println(str);该程序输出结果如下所示i=8,d=1.25编程题1-1编写一个Java Application程序,使该程序运行后输出字符串Hello!How are you.。参考程序内容如下import java.io.*;public class prol_1public static void main(String args )System.out.println(Hello!How are you.);1-2 编写一个Java Application程序,使该程序输出显示如下结果:Var1=10,var2=19.5Ok!参考程序内容如下:import java.io.*;public class prol_2public static void main(String args )int var1=10;double var2=19.5;System.out.println(var1=+var1+,var2=+var2);System.out.println(ok!);第2章Java语言语法基础选择题2-1 Java语言所用的字符集中字符是 B 位。A. 8B. 16C. 32D. 642-2 下列关于标识符的描述中,正确的是 A 。A. 标识符中可以使用下划线和美元符B. 标识符中可以使用连接符和井号符C. 标识符中大小写字母是无区别的D. 标识符可选用关键字2-3 Java语言和各种分隔符中,非法的是 D 。A. 空白符B. 分号C. 逗号D. 问号2-4 下列是Java语言中可用的注释,其中错误的是 C 。A. /B. /*/C. /*/D. /*/2-5 Java语言中字符型数据的长度是 B 位。A. 8B. 16C. 32D. 642-6 下列描述的两种数据类型的长度不相等的是 A 。A. 字符型和布尔型B. 字节型和布尔型C. 短整型和字符型D. 整型和单精度浮点型2-7 下列是Java语言中的复合数据类型,其中错误的是 C 。A. 数组B. 接口C. 枚举D. 类2-8 下面是关于Java语言中数据类型的强制转换的描述,其中错误的是 C,D 。A. 当数据类型从高转换为低时需强制转换B. 强制转换时使用强制转换去处符,形如()C. 浮点型数据强制转换为整型时,小数部分四舍五入D. 赋值操作中都要采用强制类型转换2-9 下列关于常量的描述中,错误的是 B,D 。A. Java语言的常量有5种B. 浮点型数12.456是单精度的C. 布尔型常量只有两个可选值:true和falseD. 字符串常量含有结束符0 2-10 下列关于定义符号常量的描述中,错误的是 A,C 。A. 定义符号常量使用关键字constB. 定义符号常量时要赋初值C. 符号常量既可以定义为类成员,又可在方法中定义D. 定义符号常量时必须给出数据类型2-11 下列关于变量的默认值的描述,其中错误的是 B,C 。A. 定义变量而没有进行初始化时,该变量具有默认值B. 字符型变量的默认值为换行符C. 布尔型变量的默认值是trueD. 变量的默认值是可以被改变的2-12 下列关于变量定义的描述中,正确的是 A,C 。A. 定义变量时至少应指出变量名字和类型B. 定义变量时没有给出初值该变量可能是无意义值C. 定义变量时,同一个类型多个变量间可用逗号分隔D. 定义变量时必须要给变量初始化2-13 下列关于变量作用域的描述,错误的是 D 。A. 在某个作用域定义的变量,仅在该作用域内是可见的,而在该作用域外是不可 见的B. 在类中定义的变量的作用域在该类中的方法内可以使用C. 在方法中定义 的变量的作用域仅在该方法体内D. 在方法中作用域可嵌套,在嵌套的作用域中可以定义同名变量2-14 下列关于增1(+)减1(-)1运算符的描述中,正确的是 A,B 。A. 增1 减1运算符都是单目运算符B. 增1 减1运算符都具有前缀运算和后缀运算C. 增1 减1运算符不会改变变量的值D. 增1 减1运算符前缀运算和后缀运算后表达式值是相同的2-15 关于运算符优先级的描述中,错误的是 D 。A. 在表达式中,优先级高的运算符先进行计算B. 赋值运算符优先级最高C. 单目运算符优先级高于双目和三目运算符D. 逻辑运算符优先级高于逻辑位运算符2-16 下列关于表达式的描述中,正确的是 A,C 。A. 任何表达式都有确定的值和类型B. 算数表达式的类型由第一个操作数的类型决定C. 逻辑表达式的操作数是逻辑型的D. 赋值表达式的类型取决于右值表达式的类型2-17 下列表达式中,非法的是 B,C 。int a=5,b=6;double c=1.1,d=2.2A. a+c+dB. (a+c)- -C. cbD. a!=b?c:d2-18 下列关于条件语句的描述中,错误的是 A,C 。A. If语句可以有多个else子句和else if子句B. If语句中可以没有else子句和else if子句C. If语句中的条件可以使用任何表达式D. If语句的if体、else体内可以有循环语句2-19 下列关于开关语句的描述中,错误的是 B,C,D 。A. 开关语句中,default子句可以省略B. 开关语句中,case子句的语句序列中一定含有break语句C. 开关语句中,case 子句和default子句都可以有多个D. 退出开关语句的唯一条件是执行break语句2-20 下列关于循环语句的描述中,错误的是 B,D 。A. 任何一种循环体内都可以包含一种循环语句B. 循环体可以是空语句,也可以是空C. 循环体内可以出现多个break语句D. 循环语句中,循环体至少被执行一次2-21 下列循环语句的循环次数是 D 。int i=5;doSystem.out.println(i-);i-;while(i!=0);A. 0B. 1C. 5D. 无限2-22 下列循环语句中,循环体被执行的次数为 D 。for (int i=0,j=0;(j!=18)|(i”和“”都是右移运算符,其区别在于对向右移出的位是舍弃还是保留。(错)2-26 赋值运算符组成的赋值表达式中,通常将右边表达式的类型转换为左边变量的类型后再赋值。(对)2-27 三目运算符的3个操作数中,第一个操作数的类型必须是逻辑型的(对)2-28 强制类型转换运算符的功能是将一个表达式的类型转换为所指定的类型。(对)2-29 对象运算符instanceof是一个单目运算符,其表达式的值的类的对象。(错)2-30 内存分配运算符new可以为创建的数组分配内存空间,但不能为创建的变量分配空间。2-31 运算符的优先级中,最低的是三目运算符。(错)2-32 运算符的结合性中,除了三目运算符是自右向左外,其余都是自左向右。(错)2-33 关系表达式和逻辑表达式的值都是布尔型的。(对)2-34 赋值表达式的值不能是逻辑型的。(错)2-35 条件表达式的类型总是“?”和“:”之间的操作数的类型。(错)2-36 块语句可能嵌套,外层块语句定义的变量在内层块语句中是可见的(对)2-37 条件自勉中的表达式可以是任何表达式。(错)2-38 在条件语句的嵌套结构中,一个if子句最多有一个else子句与它配对,而且一定是距离它最近的。(对)2-39 开关语句中,case子句后面的可以是块语句。(对)、2-40 循环语句中,表达式必须是逻辑型表达式。(对)2-41 在Java语言中,break语句不能作用在的左边。(错)2-42 Java语言中,数组在静态和动态赋值时都判越界。(对)2-43 说明或声明数组时不分配内存大小,创建数组时分配内存大小。(对)2-44 基本数据类型的数组在创建时系统将指定默认值。(对)2-45 数组在定义时可以进行初始化,使用初始值表。(对)2-46 Java语言中数组元素只有下标表示,没有指针表示。(对)2-47 创建数组时,系统自动将数组元素个数存放在length变量中,可供用户对数组操作时使用。(对)2-48 字符串可分为字符串常量和字符串变量两种,它们都是对象。(对)2-49 Java语言中不使用字符数组存放字符串(对)2-50 下列创建字符串s的方法是错误的。(对)char ch=m,m,n,n;char s=new char(ch);计算表达式的值,上机验证其结果题2-1 已知:int a=3,b=5;求下列表达式的值:(1) (a+b)%b(2) ba(3) -ba(4) A&b(5) +a-b+答案:(1)3(2)0(3)(4)1(5)-12-1 已知:double x=1.5,y=2.8;求下列表达式的值:(1) x+y(2) x+4/5(3) 2*x=y(4) y/6*6(5) y+=x-=1答案(1)false(2)1.5(3)false(4)2.8(5)3.32-3 已知:Boolean b1=true, b2=false; int a=6;求值:(1) b1&b2(2) !b1&b2|b2(3) b2&b1|!b2(4) b1!=b2(5) !(a6)&a6|b2=true)答案:(1)false(2)false(3)true(4)false(5)true2-4 已知:int a=3,5,7,9;求值:(1) a0+a1(2) a3=6(3) a2*a34*a3(4) (a0+=a1)+ +a2(5) for(int i=1;i4;i+)a0+=ai;答案:(1)8(2)6(3)true(4)16(5)24分析程序输出结果题2-1 Exer2_1.java程序public class Exer2_1public static void main(String args)int i=3;char c=p;float f=3.5f;double d=32.56;short s=25;booleab=true;string st=new;System.out.println(i+s=+(i+s);System.out.println(d+f=(d+f);System.out.println(st+year=+st+year);System.out.println(!b|b=+(!b|b);System.out.println(b?i:d=+(b?i:d);输出结果如下:i+s=28d+f=36.06st+year=new yearc+2=r!b|b=trueb?i:d=3.02-2 Exer2_2.java程序public class Exer2_2public static void main(String args)int suma=0,sumb=0;for(int a=0,b=0;a5;a+,b+)suma+=a;sumb+=b;System.out.println(suma=+suma+,sumb=+sumb)输出结果如下:suma=10,sumb=602-3 Exer2_3.java程序public class Exer2_3public static void main(String args)int i,j;First_Loop:for(i=1;i50;i+)for(j=2;ji;j+)if(i%j)=0continue First_Loop;System.out.println(i);输出结果如下:1235711131719232931374143472-4 Exer2_4.java程序public class Exer2_4public static void main(String args)char ch=B;switch(ch)caseA:System.out.println(5):break;caseB:System.out.println(4):break;caseC:System.out.println(3):break;default:System.out.println(0);输出结果如下:42-5 Exer2_5.java程序public class Exer2_5public static void main(String args)int a=3,b=5;if(a=3)if(b=1)a+;elseb+;System.out.println(a+,+b);int x=1,y=4;if(x=2)if(y=4)x+;elsey+;System.out.println(x+,+y);输出结果如下:3,51,52-6 Exer2_6.java程序public class Exer2_6public static void main(String args)int i;for(i=1;i2)System.out.println(AB);elseSystem.out.println(BA);if(i=5)break;System.out.println(i=+i);输出结果如下:BABAABABABi=52-7 Exer2_7.java程序public class Exer2_7public static void main(String args)long j1=1,j2=1;for(int i=5;i=1;i-)j1*=i;for(int i=4;i=1;i-)j2*=i;System.out.println(5!+4!+(j1+j2);输出结果如下:5!4!=1442-8 Exer2_8.java程序public class Exer2_8public static void main(String args)String s1=;char ch=4,5,6,7,8;double d;d=new double5;d0=1.2f;d1=2.3f;d2=3.4f;d3=4.5f;d4=5.6f;String s2=ab,cd,ef;inta=1,2,3,4,5,6,7,8,9;System.out.println(s1=+s1);for(ing i=0;i5;i+)System.out.print(chi+,);System.out.println();for(i=0;i5;i+)d0+=di;System.out.println(d0=+d0);System.out.println(s20+;+s21+;+s22);for(i=0;i3;i+)System.out.println();for(int j=0;j3;j+)System.out.print(aij)+ ;输出结果如下:s1=4,5,6.7.8d0=18.3716ab;cd;ef1 2 34 5 67 8 92-9 Exer2_9.java程序运行时命令行带有参数。运行格式如下:C:Exer2_951100程序Exer2_9.java内容如下:public class Exer2_8public static void main(String args)int sum=0;ing i=Integer.parseInt(args0);int n=Integer.parseInt(args1);for(;i=n;i+)sum=+=i;System.out.println(sum=+sum);输出结果如下:sum=37752-10 Exer2_10.java程序public class Exer2_8public static void main(String args)A:for(int i=0;i5;i+)for(ing j=0;j8;j+)System.out.print(*);System.out.println();continue A;输出结果如下:*编程题2-11 按下列要求写出Java语言的语句。(1) 定义一个double型变量d,并赋初值为2.5。该语句如下:Double d=2.5;(2) 在屏幕上输出显示(1)中所定义的变量d的值。该语句如下:System.out.println(d=+d);(3) 定义一个字符型一维数组ch它的大小为10。该语句如下:char ch=new char10;(4) 定义一个字符串常量s1,其字符串为“abc”String s1=abc;(5) 定义一个boolean型变量b,赋初值为true。boolean b=true(6) 在屏幕上输出显示26个大写字母。实现该功能的程序段如下:for(char ch=a;chb时,c值为a,否则c值为 a,b,c;if(ab)c=a;elsec=b;(9) 用for循环语句实现下述循环:循环变量m为100,每次m循环减10;当m等于50时退出循环,循环体为空语句。for(int m=100;m=50;m-=10)(10) 使用带标号名的break语句,在双重for循环自勉结构中,一次从内层循环跳出外层循环。该功能程序段如下:F:for(ing i=)for(ing j=)break F;2-12 编程求出一个一维int型数组的元素最大值、最小值、平均值和所有元素之和。程序内容如下:import java.io.*;public class pro2_1public static void main(String args)int a=23,76,45,21,63,90,46,83;int max,min,sum;max=min=a0;for(int i=1;i8;i+)if(maxai)max=ai;for(int i=1;iai)min=ai;for(int i=1;i8;i+)sum+=ai;System.out.println(max=+max+,min=+min+,ave=+sum/8+,sum=+sum);运行该程序后,输出结果如下:Max=90,min=21,ave=53,sum=4242-3 编程将一个浮点型数的整数部分和小数分别输出显示。import java.io.*;public class pro2_2public static void main(String args)double d=23.56dl;d1=d;int n=0;while(true)d-;if(d0)break;n+System.out.println(integer=+n+,decimal=+(d1-n);运行该程序后,输出结果请读者自己分析2-4 编程生成Fibonacci数列,该数列定义为:f1=f2=1,fn=fn-1+fn-2(n3)程序内容如下:import java.io.*;public class pro2_3public static void main(String args)int f1,f2;f1=f2=1for(int i=1;i=5;i+)System.out.print(f1+ +f2+ );f1+=f2;f2+=f1;System.out.println()这里假定生成Fiblnacci数列的前20项,可根据需要生成若干项。运行该程序后的输出结果如下:112358132134552-5 编程求出自然数101205中的所有素数。程序内容如下import java.io.*;public class pro2_4public static void main(String args)int n=0;boolean prime=false;for(int i=101;i=205;i+=2)prime=true;for(int j=2;ji;j+)if(j%j=0)prime=false;break;if(prime)System.out.print(i+ );n+;if(n%8=0)System.out.println();2-6 使用switch语句根据考试成绩等级输出百分制的分数段。规定如下:A. 85100B. 7084C. 6069D. 60假定85100分为A,7084为B,6069分为C,60分为D。程序内容如下:import java.io.*;public class pro2_5public static void main(String args)int a=Interger.valueOf(Value();int n=a/5;if(a60)n=11;switch(n)case 20:case 19:case 18:case 17:System.out.println(A);break;case 16:case 15:case 14:System.out.println(B);break;case 13:case 12:System.out.println(C);break;case 11:System.out.println(D);输入的分数是通过命令行参数来实现的。假定命令参数为73时,运行该程序输出结果如下:B2-7 百鸡百钱问题。公鸡每只3无,母鸡每只5无,小鸡3只一元,用100元买100只鸡,公鸡、母鸡、小鸡应各买多少?程序内容如下:import java.io.*;public class pro2_6public static void main(String args)for(int a=1;a34;a+)for(int b=1;b21;b+)if(3*a+5*b+(100-a-b)/3=100 & (100-a-b)%3=0)System.out.println(公鸡=+

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论