Java程序设计试卷及答案
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
文档从互联网中收集,已重新修正排版,word格式支持编辑,如有帮助欢迎下载支持。
Java程序语言试卷(A)
答题卡:
一题:
1.___________________________________________________________
2.___________________________________________________________
3.___________________________________________________________
4.___________________________________________________________
5.___________________________________________________________
6.___________________________________________________________
四题:
1.___________________________________________________________
2.___________________________________________________________
3.___________________________________________________________
4.___________________________________________________________
5.___________________________________________________________
五题:
一、阅读JA V A程序,写出运行结果。
(30分,每小题5分)
1、public class ko5_2{
public static void main(String args[]) {
int x=20,y=30;
if (x>0)
if (x<y)
x - =10;
else
y + =10;
"x="+x);
" y="+y);
} }
请给出运行结果___________________________
2、public class ko5_12{
public static void main(String args[]){
int t,z=10;
t=sum(z);
"sum="+t); }
static int sum(int x) {
if (x==1)
return(1);
else
return(sum(x-1)*x);
}
}
请给出运行结果___________________________
3、interface ko6_7interface {
float x=30.5f;
float y=4.6f;
float total();
}
public class ko6_7 implements ko6_7interface
{
float dollar,money;
ko6_7(float a,float b)
{dollar=a;money=b;}
public float total()
{return x*dollar+money/y;}
public static void main(String args[]) {
ko6_7 z=new ko6_7(100,4600);
"美金="+z.dollar);
"人民币="+z.money);
"折合台币="+z.total());
}
}
请给出运行结果___________________________ 4、class koA{
static int a;
public void display(){
a=”+a);
}}
class ko13{
public static void main(String args[]){
koA a1=new koA; a1.a=10;
koA a2=new koA; a2.a=20;
koA.a=50;
a1.dispay(); a2.display();
}
}
请给出运行结果___________________________ 5、class A{
void callme(){
A’s callme() method”);
}}
class B extends A{
void callme(){
B’s callme() method”);
}}
public class Dispatch{
public static void main(String args[]){
A a=new B();
a. callme();
}
}
请给出运行结果___________________________
6、public class BubbleSort{
public static void main(String args[]){
int k,j;
int intArray[]={30,1,-9,70};
int le=intArray.length;
for( j=0;j<le-1;j++)
for(k=j+1;k<le;k++)
if(intArray[j]>intArray[k]){
int t=intArray[j]; intArray[j]=intArray[k];
intArray[k]=t; }
for(j=0;j<le;j++)
“);
} }
请给出运行结果_________________________
二、填空、请将程序(或部分程序)填写完整。
(20分,每小题2分)1.运行HelloApp .java Applet小程序的网页如下。
<html>
<applet __①____="HelloApp.class" width=200 height=35>
</applet> </html>
2、对圆进行数据抽象,并建立类。
class point{ // 定义点类point
int x0; int y0;
point(int x,int y){ //构造方法
this.x0=x; this.y0=y;}
}
class circle{ // 定义圆类circle
point center;
int radius;
___②_____(point p1,int r1){ //构造方法
this.center=p1; this.radius=r1;}
double area(){ // 定义园面积
return Math.PI*radius*radius;}
}
3、建立抽象类figure,并进一步建立矩形类。
public abstract class figure{
public abstract float area();
public abstract float circumference();}
public class rectangle __③____ figure{
public float d;
public rectangle( )
{d=11.4;}
public float area( )
{return d*d;}
public float circumference( )
{return 2*d;}
}
4、封装可以限定类中的成员只被该类本身访问:package p1;
public class ori{
// 要求限定类中的成员只被该类本身访问
___④____ int n_p=2;
void Access(){
;}
}
5、S tring类对象的访问。
Class accessString{
Public static void main(String args[]){
String s=”Let’s learn java from the
very beginning .See how to access a String!”;
length of String is=”+s.length());
at the specified index 6 is=”+____⑤_______);
first occurrence of ‘a’ is=”+s.indexOf(‘a’));
//
}}
6、字符串转化成简单数据类型。
public class StringToObject{
public static void main(String args[]) { int nInt; double dDouble;
//生成相应的数据类型
String strInteger = new String("314");
String strDouble = new String("3.1416");
nInt = _____⑥_______(strInteger);
dDouble = Double.parseDouble(strDouble);
”+
nInt+””+dDouble);
}
}
7、例外处理及输入流的应用。
import java.io.*;
public class ko8_2{
public static void main(String args[])
throws IOException{
int[] ko=new int[5];
int n,a;
String x;
BufferedReader keyin=
new BufferedReader(
new InputStreamReader(System.in));
"Enter an integer:");
x=keyin.readLine();
n=Integer.parseInt(x);
try{
a=110/n;
"此描述可能无法执行!");
}
catch(__________⑦_________ e){
"除数为0的错误");
}
"执行完catch的描述!!!");
}}。