1、JAVA习题集 JAVA习题集(含答案) 习题一: 1. 简述Java的发展过程。 2. 什么是软件? 3. 什么叫做源程序? 4. 什么叫做编译? 5. 什么是Java 的byte-codes?它的最大好处是什么? 6. 机器语言程序、高级语言程序以及可表示成Java字节码的程序之间的区别是什么? 7. Java程序可分为哪两种?分别如何运行? 8. 试简述J2SE、J2ME与J2EE的简单区别。 9. 练习使用浏览器查看Java API文档。 10. SDK的编译命令是什么? 11. 试修改例1-1程序,使其输
2、出的字符串为“I’d like to study Java !”,并在DOS命令行环境下编译与运行该程序。 习题一参考答案 : 1. 答: 1991:Sun Microsystems公司进军消费电子产品(IA)市场。 1991.4:Sun 成立“Green”小组,以C++为基础开发新的程序设计语言,并将其命名为Oak。 1992.10:Green小组升格为First Person公司,他们将Oak的技术转移到Web上,并把Oak改名为Java。 1993~1994:Web在Internet上开始流行,致使Java得以迅速发展并获得成功。 1995.5:Sun Microsy
3、stems公司正式发表Java与HotJava产品。 1995.10:Netscape与Sun Microsystems合作,在Netscape Nevigator中支持Java。 1995.12:微软(Microsoft)IE加入支持Java的行列。 1996.2:Java Beta测试版结束,Java 1.0版正式诞生。 1997.2:Java发展至1.1版。Java的第一个开发包JDK(Java Development Kit)发布。 1999.7:Java升级至1.2版。 2000.9:Java升级至1.3版。 2001.7:Java升级至1.4版。 2.
4、答:软件可以理解为程序的另一种名称。 3. 答:直接使用高级语言书写的程序代码称为源程序 4. 答:把用高级语言编写的源程序翻译成可执行(目标)程序的过程称为编译。 5. 答:Java源程序经Java的编译器编译过后产生的程序称为Java 的byte-codes。它的最大好处是可以实现跨平台运行。 6. 答:机器语言程序是可以让计算机直接运行的程序;高级语言程序是必需经过编译或解释才可以让计算机运行的程序,并且程序的运行依赖于该语言的语言处理系统;可表示成Java字节码的程序必需经过编译产生Java字节码程序,Java字节码程序可以实现跨
5、平台运行。 7. 答:Java程序分为:Java Application(Java应用程序)与应用在www上的Java applet(Java小应用程序)。Java Application可以在Java平台上独立运行,以main() Method作为程序入口,由Java解释器加载执行。而Java applet则是内嵌在Html文件中,必须搭配浏览器来运行,它没有程序入口。 8. 答:J2ME(Java 2 Micro Edition):用于嵌入式Java消费电子平台。不论是无线通讯、手机、PDA等小型电子装置都可采用其作为开发工具及应用平台。J2SE[Java 2
6、Software Development Kit)Standard Edition]:是Java最通行的版本,是用于工作站、PC机的Java标准平台。J2EE(Java 2 Enterprise Edition):可扩展的企业应用Java 2平台,它提供了企业e-Business架构及Web Services服务。 9. 答:略 10. 答:javac 11. 答:修改程序如下: public class BegintoLearn { public static void main(String args[]) { System.o
7、ut.println("I’d like to study Java ! "); } } 程序运行部分略。 习题二 1. 下面哪些是合法的常量 (1) 2M (2) 4 (3) 8I (4) 6.5 (5) ch3 (6) 5.6E03 (7) \t (8) –90 (9) 012 (10) 0x14 2. int、char、float和double类型的变量各占有多少个字节?试写出它们各自的取值范围。 3. 下列符号那些可以作为J
8、ava的变量名? (1) 3a (2) main (3) KFC (4) ch.s (5) File (6) a*b (7) m_n (8) new (9) nn2 (10) -x (11) _y (12) exp (13) ha? (14) m&m (15) j,k (16)p:q 4. 什么叫做变量的作用域? 5. 试写出下列Java表达式的运算结果: (1) 15+4*5-12 (2) (18-4)/7+6 (3) 2>=5 (4) 6<=6
9、 (5) 5>2 && 8<8 &&23<36 (6) 56/9+3.6 (7) 48%9+5*5-4 (8) 9-7<0 || 11>8 (9) (3>2) ? 8 : 9 (10) 9= =8 && 3<7 6. 假设int m=2,float n=0.1f,经过下列表达式的运算后,m、n各为什么值? (1) m=9/8; (2) m=45%8+4*5-4; (3) m=36+36*2%m--; (4) m*=5/m-1; (5) n-=5*3.1; 7. 编写一程序,利用System.out.prin
10、tln()方法分别输出下列语句的执行结果: (1) 将变量m的初值赋值为10,变量n的初值赋值为5。 (2) 变量m的值加3,n的值加5。 (3) 求m和n的平均值,并将该值赋于变量p。 (4) 将m的平方乘以n的平方并赋值给变量q。 请注意各变量定义时所应该使用的数据类型。 8. 已知圆球体积公式为 ,编写一程序,设计一个求圆球体积的方法,并在主程序中调用它,求出当r=3时,圆球的体积值。 习题二参考答案: 1. 答: (1) 不合法 (2) 合法 (3) 不合法 (4) 合法 (5) 不合法
11、6)合法 (7) 合法 (8) 合法 (9) 合法 (10) 合法 2. 答: 变量类型 内存中所占字节数 取值范围 int 4 -231~ (231-1) float 4 3.4e-038 ~ 3.4e+038 double 8 1.7e
12、308 ~1.7e+308 char 2 0 ~ 65535。 3. 下列符号那些可以作为Java的变量名? (1) 不合法 (2)合法 (3)合法 (4) 不合法 (5)合法 (6) 不合法 (7) 合法 (8). 不合法 (9)合法 (10). 不合法 (11)合法 (12)合法 (13) 不合法 (14) 不合法 (15) 不合法 (16) 不合法 4. 答:变量的作用域指的是可以访问这一
13、变量的程序代码的范围。 5. 答: (1) 23 (2) 8 (3) false (4) true (5) false (6) 9.6 (7) 24 (8) true (9) 8 (10)false 6. 答: (1) 1; (2) 21; (3) 45; (4) –45; (5) –15.4; 7. 答:程序如下: public class ProNum { public static void main(String args[])
14、 { float p; long q; int m=10,n=5; System.out.println("m="+m+" n="+n); m=m+3;n=n+5; System.out.println("m="+m+" n="+n); p=(float)(m+n)/2; System.out.println("p="+p); q=(m*m*n*n); System.out.println("q="+q);
15、 } } 8. 答:程序如下: class Sphere { int r; double v; public void CalVolume() { v=3.14*r*r*r*4/3; } public void display() {
16、 System.out.println("V="+v); } } class SphereClass { public static void main(String args[]) { Sphere s = new Sphere(); s.r = 3; s.CalVolum
17、e(); s.display(); } } 习题三 1. 什么是结构化程序设计? 2. 使用if语句与使用条件运算符“?:”有什么不同? 3. 使用for循环与while循环的条件有什么不同? 4. 简述break语句与continue的区别? 5. 编写程序:由键盘输入两个字符串“12”与“24”,将它们转换成整数,然后计算并输出这两个数的和。 6. 编写程序:由键盘输入给出一个百分制成绩,要求输出成
18、绩等级’A’、’B’、’C’和’D’,90分以上为’A’,75~89为’B’,60~74为’C’,60分以下为’D’。 7. 编一程序,求一个10项所组成的等差数列,其奇数项之和为135,偶数项之和为150。 8. 用for语句输出下列数字金字塔: 1 1 3 1 1 3 5
19、 3 1 1 3 5 7 5 3 1 1 3 5 7 9 7 5 3 1 9. 编写程序:由键盘输入一正整数,求出小于且等于这个数的所有质数。 10. 编写程序:由键盘输入一整数,求出该数所有的因子,如输入6,则输出的6的所有因子为1、2、3、6。 11. 假设有一条钢材长2000米,每天截取其中的一半,编一程序求出多少天后,钢材的长度开始短于5米? 12. 编写程序:利用数列 来取得 的近似值。并计算在得到3.14159之前,这
20、个数列要取到第几项? 13. 使用java.long.Math类,生成10个0~99之间的随机整数,求出它们中的最大值和最小值。 提示:java.long.Math类支持random方法:Math. Random( )。该方法返回值是正数,类型为double,范围为0~1。如果要得到其它范围的数,则要进行相应的转换。例如要得到(a,b)之间的整数的方法是:(int) ((b-a+1)*Math.random()+a)。若要得到(0,99)之间的整数可以使用下列语句: int m = (int) (100*Math.random()); 14.
21、 编写程序:声明一数组来存放12个月的英文名称,由用户从键盘输入月份,如输入8,则程序输出相应的月份名称:August。同时请大家考虑若是用户输入了1~12以外的内容,你的程序将如何应对? 15. 编写程序:由键盘输入一16位的长整数,统计0~9这十个数字每一个出现的次数。 16. 编写程序:将两个各有6个整数的数组,合并成一个由小至大排列的数组(该数组的长度为12)。 17. 编写程序:以递归的方式实现1+2+3+……+n(n = 200)的计算。 习题三参考答案: 1. 答:具有顺序结构
22、流程、选择结构流程以及循环结构流程这三种结构的程序设计技术称为结构化程序设计。 2. 答:条件运算符“?:”只是if语句的一种最简单的形式,而if语句则具有更强的功能,它除了“?:”所表示的单分支结构外,还具有双分支结构以及多分支结构等。 3. 答:for语句往往在已知循环次数的前提下使用,在循环次数未知时则需使用while语句进行循环。 4. 答:在循环语句被执行时若遇到break语句则强行结束本轮循环,而遇到continue语句时将不在执行该语句下面的本次循环的语句,回到循环开始处继续执行下一次循环。 5. 答:程序如下: import java.io.*; publi
23、c class InNum { public static void main(String args[]) throws IOException { int num1,num2; String str; BufferedReader buf; buf=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Input first integer:"); str=buf.readLine();
24、 num1=Integer.parseInt(str); System.out.print("Input second integer:"); str=buf.readLine(); num2=Integer.parseInt(str); System.out.println("The sum is "+(num1+num2)); } } 6. 答:程序如下: import java.io.*; public cl
25、ass Grade { public static void main(String args[]) throws IOException { int score; char ch; String str; BufferedReader buf; buf=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Input the score(0-100):"); str=buf.readLin
26、e(); score=Integer.parseInt(str); if (score>=90) ch='A'; else if (score>=75) ch='B'; else if (score>=60) ch='C'; else ch='D'; System.out.print("The Grade is "+ch); } } 7. 答:程序如下: pub
27、lic class ShuLie { public static void main(String args[]) { int a ,q ,n; q=(150-135)/5; a=(135-20*q)/5; for (n=0;n<10;n++) System.out.print((a+n*q)+" "); } } 8. 答:程序如下: public class Pyramid { public static void main(String args[])
28、 { int i,j,k; for(i=0;i<=4;i++) { for ( j=0;j<20-i;j++) System.out.print(" "); for (k=0;k<=2*i;k++) if (k<=i) System.out.print(" "+(2*k+1)); else System.out.print(" "+(2*(2*i-k)+1))
29、 System.out.println(); } } } 9. 答:程序如下: import java.io.*; public class PrimeNumber{ public static void main(String args[]) throws IOException { int n=0; int m; String str; BufferedReader buf; buf=new BufferedReader(new InputS
30、treamReader(System.in)); System.out.print("Input the m:"); str=buf.readLine(); m=Integer.parseInt(str); outer:for(int i=3;i<=m;i+=2){ //outer loop for(int j=2;j
31、nue outer; } System.out.print(" "+i); n++; //outputanewline if(n==10) //after10numbers { System.out.println(); n=0; } } Syst
32、em.out.println(); } } 10. 答:程序如下: import java.io.*; public class Factor12{ public static void main(String args[]) throws IOException { int m; String str; BufferedReader buf; buf=new BufferedReader(new InputStreamReader(System.in)); S
33、ystem.out.print("Input the m:"); str=buf.readLine(); m=Integer.parseInt(str); System.out.print(m+"\'s factors are: "); System.out.println( ); for(int i=1;i<=m;i++) if (m%i==0) System.out.print(" "+i);
34、 } } 11. 答:程序如下: public class MSteel{ public static void main(String args[]) { int d=0; float m=2000; while (m>=5) { m=m/2; d++; System.out.print(d+": "); System.out.println(m); } System.ou
35、t.print("You need "+d+" days"); } } 12. 程序如下: public class AlmostPi{ public static void main(String args[]) { int n; long m; double s,t; n=1; m=0; s=0; do { t=(double)n/(2*m+1); m++; n
36、n; s=s+t; } while (4*s-3.14159> 0.0000001 || 4*s-3.14159< -0.0000001); System.out.println(m); } } 13. 答:程序如下: public class LSRnd{ public static void main(String args[]) { int mun,n,max1,min1; max1=0; min1=100;
37、 for (n=1;n<=10;n++)
{ mun=(int)(100*Math.random());
System.out.print(mun+" ");
if (mun>max1)
max1=mun;
if (mun 38、number is: "+max1);
System.out.println("The smallest number is: "+min1);
}
}
14. 答:程序如下:
import java.io.*;
public class StrArry
{
public static void main(String args[]) throws IOException
{
int m;
String str;
String month[]={"Janu 39、ary","February","March","April","May","June","July","August","september","October","November","December"};
BufferedReader buf;
buf=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Input the m:");
str=buf.readLine();
m=Integer.parseInt(str); 40、
if (m>=1 && m<=12)
System.out.println(month[m-1]);
else
System.out.print("Your Input is wrong");
}
}
15. 答:程序如下:
import java.io.*;
public class StatNum
{
public static void main(String args[]) throws IOException
{
int 41、m;
char s;
String str;
int a[]=new int[10];
BufferedReader buf;
buf=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Input the long number:");
str=buf.readLine();
for (int i=0;i<=15;i++)
{ s=str.charAt(i);
42、 m=(int)s-48; // 字符数据转换为整型数据时转换的是其Unicoad代码。
a[m]=a[m]+1;
}
for (m=0;m<10;m++)
System.out.println(m+": "+a[m]);
}
}
16. 答:程序如下:
import java.io.*;
public class SortArray
{
public static void main(String args[]) 43、 throws IOException
{
int m,n,k;
int aa[]=new int[6];
int bb[]=new int[6];
int cc[]=new int[12];
for (int i=0;i<6;i++) //利用产生随机数的方式为数组赋值。
{ m=(int)(100*Math.random());
aa[i]=m;
n=(int)(100*Math.random()); 44、
bb[i]=n;
System.out.println(aa[i]+" "+bb[i]);
}
for (int i=0;i<6;i++) //先将两个数组进行排序
for (int j=i;j<6;j++)
{ if (aa[i]>aa[j])
{int t=aa[i];aa[i]=aa[j];aa[j]=t;}
if (bb[i]>bb[j] 45、)
{int t=bb[i];bb[i]=bb[j];bb[j]=t;}
}
m=0; //用合并法将两个有序数组排序并合并
n=0;
k=0;
while (m<6 &&n<6)
{ if (aa[m]<=bb[n])
{ cc[k]=aa[m];m++;}
else
{cc[k]=b 46、b[n];n++;}
k++;
}
while (m<6)
{cc[k]=aa[m];m++;k++;}
while (n<6)
{cc[k]=bb[n];n++;k++;}
for (int i=0;i<12;i++)
System.out.print(cc[i]+" ");
}
}
17. 答:程序如下:
class RecuSum{
static long Sum1 47、int n){
if (n==1)
return 1;
else
return n+Sum1(n-1);
}
public static void main(String args[]) {
int n=200;
System.out.println("Sum="+Sum1(n));
}
}
习题四
1类和对象之间的关系。
2声明一个类Person,成员变量有姓名、出生年月、性别。有成员方法以 48、显示姓名、年龄和性别。
3 声明一个矩形类Rectangle,其中有多个构造方法。用不同的构造方法创建对象,并输出矩形的周长和面积。
4 声明一个矩阵类Matrix,有这样一些实例方法:将一个矩阵转置、求两个矩阵的和。
5 举例说明类(静态)成员的特点。
6 说明继承和接口的区别已及它们各自的作用。
7 写出下面程序的运行结果
class Leve1{
String name="";
int height,width;
public Leve1(){;}
public Leve1(Leve1 le) {
name=le.name;
height= 49、le.height;
width=le.width;}
public Leve1(String na,int h,int w) {
name=na;
height=h;
width=w;}
public int higherthen(Leve1 t) {
return this.height-t.height;}
public void print(){
System.out.println("class Leve1: ");
System.out.println("Name=: "+name+", Heigh 50、t=: "+height+", Width=: "+width);}
}
class Leve2 extends Leve1{
int width;
public Leve2(){;}
public Leve2(Leve1 le,int h,int w) {
super(le);
height=h;
width=w;}
public int widerthen(){
return width-super.width;}
public void print(){
System.out.println("class Le






