admin 管理员组文章数量: 1184232
2023年12月24日发(作者:umbrella简谱)
JAVA习题集(含答案)
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程序,使其输出的字符串为“I’d like to study Java !”,并在DOS命令行环境下编译与运行该程序。
习题一参考答案 :
1. 答:
第2页 共58页
JAVA习题集
第3页 共58页
JAVA习题集
第4页 共58页
JAVA习题集
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
(Software Development Kit)Standard
Edition]:是Java最通行的版本,是用于工作站、PC机的Java标准平台。J2EE(Java 2
Enterprise Edition):可扩展的企业应用Java
2平台,它提供了企业e-Business架构及Web
Services服务。
9. 答:略
10. 答:javac
第5页 共58页
JAVA习题集
11. 答:修改程序如下:
public class BegintoLearn
{
public static void main(String args[])
{
n("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类型的变量各占有多少个字节?试写出它们各自的取值范围。
第6页 共58页
JAVA习题集
3. 下列符号那些可以作为Java的变量名?
(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
(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;
第7页 共58页
JAVA习题集
(3) m=36+36*2%m--;
(4) m*=5/m-1;
(5) n-=5*3.1;
7. 编写一程序,利用n()方法分别输出下列语句的执行结果:
(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) 不合法
第8页 共58页
JAVA习题集
(6)合法 (7) 合法 (8) 合法
(9) 合法 (10) 合法
2. 答:
变量类型 内存中所占字节数 取值范围
int 4
-231~ (231-1)
float 4
3.4e-038 ~ 3.4e+038
double 8
1.7e-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) 不合法
第9页 共58页
JAVA习题集
4. 答:变量的作用域指的是可以访问这一变量的程序代码的范围。
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;第10页 共58页
JAVA习题集
7. 答:程序如下:
public class ProNum
{
public static void main(String args[])
{
float p;
long q;
int m=10,n=5;
n("m="+m+"
n="+n);
m=m+3;n=n+5;
n("m="+m+"
n="+n);
p=(float)(m+n)/2;
n("p="+p);
q=(m*m*n*n);
n("q="+q);
}
}
8. 答:程序如下:
第11页 共58页
JAVA习题集
class Sphere {
int r;
double v;
v=3.14*r*r*r*4/3;
}
public void display() {
n("V="+v);
}
}
class SphereClass
{
public static void main(String args[]) {
Sphere s = new Sphere();
s.r = 3;
public void CalVolume() {
ume();
}
}
y();
第12页 共58页
JAVA习题集
习题三
1. 什么是结构化程序设计?
2. 使用if语句与使用条件运算符“?:”有什么不同?
3. 使用for循环与while循环的条件有什么不同?
4. 简述break语句与continue的区别?
5. 编写程序:由键盘输入两个字符串“12”与“24”,将它们转换成整数,然后计算并输出这两个数的和。
6. 编写程序:由键盘输入给出一个百分制成绩,要求输出成绩等级’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 3 1
第13页 共58页
JAVA习题集
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之前,这个数列要取到第几项?
13. 使用类,生成10个0~99之间的随机整数,求出它们中的最大值和最小值。
提示:类支持random方法:Math.
Random( )。该方法返回值是正数,类型为double,范围为0~1。如果要得到其它范围的第14页 共58页
JAVA习题集
数,则要进行相应的转换。例如要得到(a,b)之间的整数的方法是:(int)
((b-a+1)*()+a)。若要得到(0,99)之间的整数可以使用下列语句:
int m = (int) (100*());
14. 编写程序:声明一数组来存放12个月的英文名称,由用户从键盘输入月份,如输入8,则程序输出相应的月份名称:August。同时请大家考虑若是用户输入了1~12以外的内容,你的程序将如何应对?
15. 编写程序:由键盘输入一16位的长整数,统计0~9这十个数字每一个出现的次数。
16. 编写程序:将两个各有6个整数的数组,合并成一个由小至大排列的数组(该数组的长度为12)。
17. 编写程序:以递归的方式实现1+2+3+……+n(n = 200)的计算。
第15页 共58页
JAVA习题集
习题三参考答案:
1. 答:具有顺序结构流程、选择结构流程以及循环结构流程这三种结构的程序设计技术称为结构化程序设计。
2. 答:条件运算符“?:”只是if语句的一种最简单的形式,而if语句则具有更强的功能,它除了“?:”所表示的单分支结构外,还具有双分支结构以及多分支结构等。
3. 答:for语句往往在已知循环次数的前提下使用,在循环次数未知时则需使用while语句进行循环。
4. 答:在循环语句被执行时若遇到break语句则强行结束本轮循环,而遇到continue语句时将不在执行该语句下面的本次循环的语句,回到循环开始处继续执行下一次循环。
5. 答:程序如下:
import .*;
public class InNum
{
public static void main(String args[]) throws
IOException
{
第16页 共58页
JAVA习题集
int num1,num2;
String str;
BufferedReader buf;
buf=new BufferedReader(new
InputStreamReader());
("Input first integer:");
str=ne();
num1=nt(str);
("Input second
integer:");
str=ne();
num2=nt(str);
n("The sum is
"+(num1+num2));
}
}
6. 答:程序如下:
import .*;
public class Grade
{
第17页 共58页
JAVA习题集
public static void main(String args[]) throws
IOException
{
int score;
char ch;
String str;
BufferedReader buf;
buf=new BufferedReader(new
InputStreamReader());
("Input the
score(0-100):");
str=ne();
score=nt(str);
if (score>=90)
ch='A';
else if (score>=75)
ch='B';
else if (score>=60)
ch='C';
else
ch='D';
("The Grade is "+ch);
第18页 共58页
JAVA习题集
}
}
7. 答:程序如下:
public 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++)
((a+n*q)+" ");
}
}
8. 答:程序如下:
public class Pyramid
{
public static void main(String args[])
{
int i,j,k;
第19页 共58页
JAVA习题集
for(i=0;i<=4;i++)
{ for ( j=0;j<20-i;j++)
(" ");
for (k=0;k<=2*i;k++)
if (k<=i)
(" "+(2*k+1));
else
("
"+(2*(2*i-k)+1));
n();
}
}
}
9. 答:程序如下:
import .*;
public class PrimeNumber{
public static void main(String args[]) throws
IOException
{
第20页 共58页
JAVA习题集
int n=0;
int m;
String str;
BufferedReader buf;
buf=new BufferedReader(new
InputStreamReader());
("Input the m:");
str=ne();
m=nt(str);
outer:for(int i=3;i<=m;i+=2){ //outer
loop
for(int
j=2;j
if(i%j==0)
continue outer;
}
(" "+i);
n++;
//outputanewline
if(n==10)
//after10numbers
{ n();
第21页 共58页
JAVA习题集
n=0;
}
}
n();
}
}
10. 答:程序如下:
import .*;
public class Factor12{
public static void main(String args[]) throws
IOException
{
int m;
String str;
BufferedReader buf;
buf=new BufferedReader(new
InputStreamReader());
("Input the m:");
str=ne();
m=nt(str);
第22页 共58页
JAVA习题集
(m+"'s factors are: ");
n( );
for(int i=1;i<=m;i++)
if (m%i==0)
(" "+i);
}
}
11. 答:程序如下:
public class MSteel{
public static void main(String args[])
{
int d=0;
float m=2000;
while (m>=5)
{ m=m/2;
d++;
(d+": ");
n(m);
}
第23页 共58页
JAVA习题集
("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=-n;
s=s+t;
} while (4*s-3.14159> 0.0000001 ||
4*s-3.14159< -0.0000001);
第24页 共58页
JAVA习题集
n(m);
}
}
13. 答:程序如下:
public class LSRnd{
public static void main(String args[])
{
int mun,n,max1,min1;
max1=0;
min1=100;
for (n=1;n<=10;n++)
{ mun=(int)(100*());
(mun+"
");
if (mun>max1)
max1=mun;
if (mun min1=mun; } n(); 第25页 共58页 JAVA习题集 n("The largest number is: "+max1); n("The smallest number is: "+min1); } } 14. 答:程序如下: import .*; public class StrArry { public static void main(String args[]) throws IOException { int m; String str; String month[]={"January","February","March","April","May","June","July","August","september","October","November","December"}; BufferedReader buf; 第26页 共58页 JAVA习题集 buf=new BufferedReader(new InputStreamReader()); ("Input the m:"); str=ne(); m=nt(str); if (m>=1 && m<=12) n(month[m-1]); else ("Your Input is wrong"); } } 15. 答:程序如下: import .*; public class StatNum { public static void main(String args[]) throws IOException { int m; char s; 第27页 共58页 JAVA习题集 String str; int a[]=new int[10]; BufferedReader buf; buf=new BufferedReader(new InputStreamReader()); ("Input the long number:"); str=ne(); for (int i=0;i<=15;i++) { s=(i); m=(int)s-48; // 字符数据转换为整型数据时转换的是其Unicoad代码。 a[m]=a[m]+1; } for (m=0;m<10;m++) n(m+": "+a[m]); } } 16. 答:程序如下: import .*; 第28页 共58页 JAVA习题集 public class SortArray { public static void main(String args[]) 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*()); aa[i]=m; n=(int)(100*()); bb[i]=n; n(aa[i]+" "+bb[i]); } for (int i=0;i<6;i++) //先将两个数组进行排序 for (int j=i;j<6;j++) { if (aa[i]>aa[j]) 第29页 共58页 JAVA习题集 {int t=aa[i];aa[i]=aa[j];aa[j]=t;} if (bb[i]>bb[j]) {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]=bb[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++) 第30页 共58页 JAVA习题集 (cc[i]+" "); } } 17. 答:程序如下: class RecuSum{ static long Sum1(int n){ if (n==1) return 1; else return n+Sum1(n-1); } public static void main(String args[]) { int n=200; n("Sum="+Sum1(n)); } } 第31页 共58页 JAVA习题集 习题四 1类和对象之间的关系。 2声明一个类Person,成员变量有姓名、出生年月、性别。有成员方法以显示姓名、年龄和性别。 3 声明一个矩形类Rectangle,其中有多个构造方法。用不同的构造方法创建对象,并输出矩形的周长和面积。 4 声明一个矩阵类Matrix,有这样一些实例方法:将一个矩阵转置、求两个矩阵的和。 5 举例说明类(静态)成员的特点。 6 说明继承和接口的区别已及它们各自的作用。 7 写出下面程序的运行结果 class Leve1{ String name=""; int height,width; public Leve1(){;} public Leve1(Leve1 le) { name=; height=; width=;} public Leve1(String na,int h,int w) { name=na; 第32页 共58页 JAVA习题集 height=h; width=w;} public int higherthen(Leve1 t) { return ;} public void print(){ n("class Leve1: "); n("Name=: "+name+", Height=: "+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 ;} public void print(){ n("class Leve2: "); n("Name=: "+name+", Height=: "+height+", Width=: "+width);} 第33页 共58页 JAVA习题集 } public class Lev_ex{ public static void main(String args[]){ Leve1 le1=new Leve1("Tower_1",50,20); Leve2 le2=new Leve2(le1,40,25); (); (); n("Leve1 is "+then(le2)+" highter then Leve2."); n("Leve2 is "+hen()+" wider then Leve1."); } } 8 Java中的包和windows的文件夹有什么关系? 9 说明类的成员的访问权限和包的关系。 10 创建两个String类的对象str1和str2,判断sr2是否是str1的子串。如果是,输出str1中,在子串ztr2前和后的字符串。如: “Action”是“addActionListener”的子串,在此子串前是字符串”add”,后面是字符串”Listener”。 第34页 共58页 JAVA习题集 11 写出下面程序的运行结果 class Father{ int x; public Father(int x) { this.x=x;} public Father(){;} public int fun(){ int f=0; for(int i=1;i<=x;i++) f=f+i; return f;} public int fun(int x) { int f=1; for(int i=this.x;i<=x;i++) f=f*i; return f;} } class Son extends Father{ public Son(int a) { super(a);} public int fun(){ int f=1; 第35页 共58页 JAVA习题集 for(int i=1;i<=x;i++) f=f*i; return f;} } public class Pol_ex{ public static void main(String args[]){ Father f1=new Father(5); Son s1=new Son(3); n(()); n(()); n((6)); f1=s1; n(()); n(()); n((6)); } } 习题四参考答案: 2、 import .*; 第36页 共58页 JAVA习题集 public class Person {private String name; private char sex; private int year,month; public Person( ){} public Person(String nm,char sx,int y,int m) { name=nm; sex=sx; year=y; month=m;} public void printPerson( ) {Calendar now=tance(); int age=()-year; n("Name: "+name+",Sex: "+sex+", Age: "+age);} public static void main(String args[]) {Person pe1=new Person("Tom",'m',1980,10); erson();} } 3、 public class Rectangle 第37页 共58页 JAVA习题集 {double width,length,girth,area; public Rectangle(){}; public Rectangle(double wd,double le) { width=wd;length=le;} public void setWidth(double wd) {width=wd;} public void setLength(double le) {length=le;} public double getWidth( ) {return width;} public double getLength( ) {return length;} public double girth() {return 2*(width+length);} public double area() {return width*length;} public void printRectangle() { n("Width="+width+" ,Length="+length);} public static void main(String args[]) {Rectangle re1=new Rectangle(10,20); 第38页 共58页 JAVA习题集 Rectangle re2=new Rectangle(); th(3); gth(4); ectangle(); n("Girth="+()+",Area="+()); ectangle(); n("Girth="+()+",Area="+());} } 4、 public class Matrix {private int mx[][],m,n; public Matrix(int r,int c) {m=r; n=c; mx=new int[m][n]; iniMatrix();} public Matrix() 第39页 共58页 JAVA习题集 {m=3; n=3; mx=new int[3][3]; iniMatrix();} public void iniMatrix() {int i,j; for(i=0;i<=m-1;i++) for(j=0;j<=n-1;j++) mx[i][j]=(int)(()*100);} public void tranMatrix() {int i,j,t; int mt[][]=new int[m][n]; for(i=0;i<=m-1;i++) for(j=0;j<=n-1;j++) mt[i][j]=mx[i][j]; t=m; m=n; n=t; mx=new int[m][n]; for(i=0;i<=m-1;i++) for(j=0;j<=n-1;j++) mx[i][j]=mt[j][i];} 第40页 共58页 JAVA习题集 public void printMatrix() {int i,j; for(i=0;i<=m-1;i++) { for(j=0;j<=n-1;j++) (" "+mx[i][j]); n();} } public void addMatrix(Matrix b) {int i,j; for(i=0;i<=m-1;i++) for(j=0;j<=n-1;j++) mx[i][j]=mx[i][j]+[i][j]; } public static void main(String args[]) {Matrix ma=new Matrix(4,3); Matrix mb=new Matrix(4,3); n("The matrix_A:"); atrix(); n("The matrix_B:"); atrix(); n("Matrix_A + Matrix_B:"); rix(mb); atrix(); 第41页 共58页 JAVA习题集 n("Transpose Matrix_B:"); trix(); atrix(); }} 10、 public class Substring {public static void main(String args[]) {String str1=new String("addActionListener"); String str2=new String("Action"); int n; n=f(str2); if(n>=0){n("String_2 is in String_1"); n("The substring before String_2 is "+ing(0,n)); n("The substring behind String_2 is "+ing(n+( )));} }} 第42页 共58页 JAVA习题集 习题五 问答题: 1. 请解释try、catch、finally块之间的关系。 2. try块的嵌套有几种形式?他们对异常分别是怎样处理的? 3. 简述throw,throws语句的作用。 习题五参考答案: 1. try块用来监视某段代码执行过程中是否发生异常,若发生则产生异常对象并抛出。catch用于捕获异常并处理它。finally块中的语句无论是否发生异常都将被执行。 2.有两种形式。一种是显式嵌套,它是指在某个方法中,某一个try块又包含另一try块,当内层try块抛出异常对象时,首先对内层try块的catch语句进行检查,若与抛出异常类型匹配第43页 共58页 JAVA习题集 则由该catch处理,否则由外层try块的catch处理。 另一种是隐式嵌套,它是指在不同的方法中,若方法1的try块中调用方法2,而方法2又包含一个try语句,则方法1中的try块为外层,方法2中的try为内层。 3.throw语句用于在程序中自行抛出异常,throw语句执行后,其后继语句将不再执行,执行流程将直接寻找后面的catch语句,如果catch语句中的参数能匹配throw语句抛出的Throwable对象,则执行相应的异常处理程序。如果catch语句中的参数没有一个能与该对象相匹配,则由缺省的异常处理器终止程序的执行并输出相应的异常信息。 throws语句用于声明一个方法可能引发的所有异常,这些异常是要求调用该方法的程序处理的。 第44页 共58页 JAVA习题集 习题六 问答题: 1 线程有那些特点。 2 什么是线程的生命周期?线程的是怎样在各种状态下转换的。 3 编写一个线程程序,有两各线程,分别在屏幕上显示1~50之间的奇数和偶数。观查一共有几个线程在运行,各个线程是怎样被处理器执行到的。 4 编写程序同上题,利用Java对线程的调度技术,使屏幕上先显示1~50之间的奇数,再显示1~50之间的偶数。 5 一个多线程的程序怎么会出现死锁? 6 编写一个程序,创建两个线程,其中一个产生5个1~100之间的随机整数,另一个线程将这5个数加起来。观察运行的情况,分析结果是否正确。如果结果有问题,想办法解决它。 习题六参考答案: 3. public class Thread1 extends Thread 第45页 共58页 JAVA习题集 { int d; public Thread1(String name,int d) {super(name); this.d=d;} public void run() {int k; n(); (getName()+": "); for(k=d;k<=50;k=k+2) (k+" "); n(getName()+" end!");} public static void main(String args[]) {Thread1 th1=new Thread1("th1",1); Thread1 th2=new Thread1("th2",2); (); (); n("activecount="+Count());}} 4. public class Thread2 extends Thread { int d; 第46页 共58页 JAVA习题集 public Thread2(String name,int d) {super(name); this.d=d;} public void run() {int k; n(); (getName()+": "); for(k=d;k<=50;k=k+2) (k+" "); n(getName()+" end!");} public static void main(String args[]) {Thread2 th1=new Thread2("th1",1); Thread2 th2=new Thread2("th2",2); (); ority(1); (); n("activecount="+Count());}} 6. class Databuf {private int value,totalvalue; 第47页 共58页 JAVA习题集 private boolean writeable=true; int sort=0; synchronized void senddata() {while(!writeable) try {();} catch(InterruptedException e) {n(e);} value=(int)(()*100); writeable=false; notifyAll();} synchronized int adddata() {while(writeable) try {();} catch(InterruptedException e) {n(e);} totalvalue=totalvalue+value; writeable=true; notify(); return value;} int getvalue() 第48页 共58页 JAVA习题集 {return value;} int gettotalvalue() {return totalvalue;} } class Send extends Thread {Databuf da; public Send(Databuf d) {da=d;} public void run() {int i; synchronized(da) {for(i=1;i<=5;i++) {try {sleep(1);} catch(InterruptedException e) {n(e);} ta(); n("Send data"+i+": "+ue()); }}} } class Add extends Thread 第49页 共58页
版权声明:本文标题:JAVA习题集(含答案) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1703394465a449364.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论