2024年2月22日发(作者:)
《JAVA程序设计》复习题之(三)阅读程序题
(计算机科学与技术专业使用)
三、程序阅读题
1.阅读以下程序
import .*;
public class Reverse2 {
public static void main(String args[ ]){
int i,n=10;
int a[] = new int[10];
try {
BufferedReader br = new BufferedReader(
new InputStreamReader());
a[i] = nt(ne() );
} catch (IOException e) { };
for (i= n-1; i >= 0; i=i-2)
(a[i]+" ");
n();
}
}
请写出该程序的功能:
该程序使用字符缓冲输入流从键盘输入10个数,然后倒序并间隔打印出来。
2.阅读以下程序
import .* ;
public class abc {
public static void main(String args[ ]) {
int i, s = 0 ;
int a[] = { 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120 };
for ( i = 0 ; i< ; i++ )
if (i % 3 == 0) s += a[i];
n("s=" + s);
}
}
请写出该程序的输出结果:
s=260
3、阅读以下程序:
import .*;
public class TestRandomAccess {
public static void main(String args[]) {
int data_arr[]={65,66,56,23,27,1,43,65,4,99};
try {
RandomAccessFile randf=new RandomAccessFile("","rw");
for (int i=0; i nt(data_arr[i]); TF("Good morning!"); ' for(int i=data_-l; i>=0; i=i-2) { (i*4); System,(" "+t()); (40); n(F()); (); } catch (IOException e) { n("File access error: "+e); } } } 该程序的输出结果是: 99 65 1 23 66 Good morning! 4、阅读以下程序并填空。 class _____________________ extends Exception { String mymsg="我自己定义的异常!"; double mynum = 2.0; MyException () { super("首字母不能为A! ");} MyException (String msg){_____________ } //调用父类构造方法,参数为 msg public void displayme() { n(mymsg); } public double mymethod() { return (mynum); } } class ExceptionTest { public static void main(String[] args) { try { if ( args[O].charAt(O)== 'A') { MyException e = new MyException(); n("kkkk:" + od()); yme(); System.out.println("*********in try*********"); __________________________; //抛出异常e } else if(args[O].charAt(O)== 'B') { throw new MyException ("第一个字符不应是B! "); } else { n(args[0]); } } catch ( __________________________ ) { n(sage()); yme(); n("" + od()); } catch( __________________________ ) { n("命令行参数个数错!"); } } } 程序填空: MyException super(msg) throw e MyException aaa ArrayIndexOutOfBoundsException 5、阅读以下程序 import .*; public class Test { public static void main(String args[]) { SubSubClass m=new SubSubClass(3,6,6); (); } } class SuperClass { int a,b; SuperClass(int x,int y){ a=x; b=y; } } class SubClass extends SuperClass { int c; SubClass(int aa,int bb,int cc) { super(aa,bb); c = cc; } } class SubSubClass extends SubClass { int a; SubSubClass(int aa,int bb,int cc) { super(aa,bb,cc); a = aa + bb + cc; } void show() { n("a="+ a +"nb="+ b +"nc="+ c); } } 请写出该程序的运行结果: a=60 b=20 c=30 6、阅读以下程序 import .*; public class abc { public static void main(String args[]) { String sl = "Hello!"; String s2 = new String("World!"); n((s2)); } } 请写出该程序的运行结果: Hello!World! 7、阅读以下程序 import .*; public class Class1 { public static void main(String args[]){ int i,max,min; int a[] = {12,67,8,98,23,56,124,55,99,100); max= min= a[0]; for(i=1; i<; i++){ if( a[i]< min) min = a[i]; if( a[i]> max) max = a[i]; } n( max + " " + min); n(); } } 请写出该程序完成的功能: 在数组中查找并输出最大值和最小值。 8、阅读以下程序 import .*; import ; public class DrawMylmage extends Applet { Image myImage; //定义一个图像类Image的对象myImage public void init(){ myImage= getImage(getDocumentBase(),""); } public void paint(Graphics g) { age(myImage,0,0,this); } } 请写出该程序的功能: 在Applet界面中显示当前文件夹下名为“”的图像。 9、阅读以下程序并填空。 import .*; import .*; import .*; public class Mypicture __________________ Applet { Image image; public void _________() { try { image=getlmage(new URL(getCodeBase(),'')); } _______________(MalformedURLException e) { } public void paint(Graphics g) { age(image,0,0,__________); } public void start() { ______________(); } } 程序填空题:extends init catch this repaint 10、阅读以下程序: public class Sum { public static void main( String args[]) { double sum = 0.0 ; for ( int i = 1; i<= 100; i ++ ) sum += i;, n( "sum=" + sum ); } } 该程序完成的功能是: 求sum=1+2+3+...+100的和。 11、阅读以下程序: class SuperClass { int a,b; SuperClass(int x,int y) { a=x; b=y; } voidshow() { n("a="+ a + "nb="+ b); } } class SubClass extends SuperClass { int c; SubClass(int aa,int bb,int cc) { super(aa,bb); c=cc; } voidshow() { n("c="+ c +"na="+ a +"nb="+ b); } } class SubSubClass extends SubClass { int a; SubSubClass(int aa,int bb,int cc) { super(aa,bb,cc); a=aa+bb+cc; } void show(){ n("a="+ a +"nb="+ b +"nc="+ c); } class test { public static void main(String[] args) { SuperClass p=new SubSubClass(10,20,30); (); } } 该程序的输出结果是: a=60 b=20 c=30 12、阅读以下程序: import .*; publiic class Test { public static void main(String args[]) { AB s = new AB("Hello!","I love Java."); n( ng() ); } } class AB { String sl; String s2; AB( String strl, String str2 ) { sl = str1; s2 = str2; } public String toString() { return sl + s2; } } 该程序的输出结果是: Hello!I love Java. 13、阅读以下程序,并填空。 import _______________ class MyCopy { public static void main(Stringo args) { int ch; FileInputStream fin; _______________ fout; try { fin = new FileInputStream(args[0]); fout = new FileOutputStream(____________); ch = (); while(ch!=-1) { __________________ ch = (); } (); (); } catch (____________ e1) { n("使用格式错误!正确格式为:java mycopy源文件名目标文件名"); (0); } catch (FileNotFoundException e3) { n("文件没有找到!"); } catch (IOException e2) { n("流错误!"); } } } 程序填空: import .*; FileOutputStream args[0] (ch); ArrayIndexOutOfBoundsException 14、阅读以下程序 import .*; public class Reverse { public static void main(String args[]) { int i,n=10; int a[] = new int[10]; for(i=0; i try { BufferedReader br= new BufferedReader( new InputStreamReader()); a[i]=nt(ne()); //输入一个整数 } catch (IOException e) { }; for (i = n-1; i >= 0; i--) (a[i]+" "); n(); } } 请写出此程序功能: 程序运行时从键盘输入10个整数,然后倒序输出。 15、阅读以下程序 import .* ; public class Test { public static void main(String args[ ]) { int i, s = 0; int a[] = {10, 20, 30, 40, 50, 60, 70, 80, 90}; for ( i = 0 ; i < ; i++ ) if(a[i] % 3 == 0) s += a[i]; n("s=" + s); } } 请写出此程序的输出结果: s=180 16、阅读以下程序(提示:注意同步) class One{ synchronized void display(int num) { n("two " + num); try { (1000); } catch (InterruptedException e) { n(”中断”); } n(”完成”); } } class Two implements Runnable { int number; One one; Thread t; public Two(One one_num, int n) { one = one_num; number = n; t = new Thread(this); (); } public void run(){ y(number); } } public class Synch { public static void main(String args[]) throws InterruptedException { One one = new One(); int digit = 100; Two s1 = new Two(one,digit); Two s2 = new Two(one,digit); Two s3 = new Two(one,digit); Two s4 = new Two(one,digit); (); (); (); (); n("Synch结束!"); } } 此程序的输出结果是: two 100 完成 two 100 完成 two 100 完成 two 100 完成 Synch 结束! 17、阅读以下程序,并填空。 import ____________________ ; class FileType{ public static void main(String args[]){ ____________________; try { FileReader fis = new_____________(args[0]); BufferedReader reader = new BufferedReader( ); String s; while((s=ne())!=________) { n(" "+(i++)+":" + s); } (); (); } catch (IOException e) { n(e); } catch (__________________ e1) { n(“缺少命令行参数!”); } } } 程序填空: .*; int i; FileReader fis null ArrayIndexOutOfBoundsException 18、阅读以下程序: public class Sum { public static void main(String args[]) { double sum = 0.0: for (int i=1; i<=100; i++) sum += i*i; n( "sum="+sum); } } 该程序的功能是: 求出sum的值为1到100的平方和。 19、阅读以下程序: class Example { public static void main(String args[]) { int a[][] = new int[3][3]; a[0][0]=1; a[1][1]=1; a[2][2]=1; n("数组a:"); for (int i=0; i < ; i++) { for(int j=0; j < a[i].length; j++) (a[i][j] + " "); n(); } } } 该程序的输出结果是: 1 0 0 0 1 0 0 0 1 20、阅读以下程序: public class Test2{ static boolean foo(char c){ (c); return true; } public static void main(String[] args){ int i = 0: for(foo('A'); foo('B') && (i<2); foo('C')){ i++; foo('D'); } } } 此程序的输出结果是: "ABDCBDCB" 21、阅读以下程序,并填空。 import ____________________; import .*; public class OvalDrawer extends Frame implements Runnable{ private Color[] colors= { ,,, ,_GRAY }; private Color color; private int x=l0,y=10,width=10,height=10; public OvalDrawer(String title){ super(title); ___________________________; //设置窗口大小为300*300 setVisible(true); _________________________; //创建线程并启动 } public void run() { while(true) { x = (int)(0*300); y = (int)(0*300); width = (int)(()*100); height = (int)(()*100); color = colors[(int)(()*(-1))]; ___________________; //刷新窗口内容 try { (400); } catch(InterruptedException e) { throw new RuntimeException(e); } } } public void paint(Graphics g){ or(color); ______________________; //画椭圆 } public static void main(String args[]) { new OvaIDrawer("hello"); } } 程序填空: .*; setSize(300,300); new Thread(this).start(); repaint(); al(x,y,width,height); 22、阅读以下程序: public class Sum { public static void main(String args[]) { double sum = 0.0; for (int i=1; i<=100; i++) sum += 1.0 / (double)i; n("sum="+sum); } } 此程序完成的功能是: 求解sum=1+1/2+1/3+...+1/100的值并打印输出。 23、阅读以下程序: import .*; import ; public class Applet1 extends Applet { public void paint(Graphics g) { ne(30,5,100,45); ct(30,50,50,20); al(30,80,50,40); ring("They are figures! ",30,150); } } 此程序完成的功能是: 在Applet界面中显示一些由直线、矩形框、椭圆框和文字组成的图形。 24、阅读以下程序: import .*; public class Test { public static void main(String args[]) { int i; int a[]={11,22,33,44,55,66,77,88,99}; for(i=0; i <= /2; i++) (a[il + a[-i-1] + " "); n(); } } 此程序的输出结果是: 110 110 110 110 110 25、阅读程序并填空 import .*; import .*; public class DrawStringDemo ____________ Applet { private Font afont = ____________Font("Helvetica",,18); public void init() { _______________(); } public void paint(Graphics g) { or(); ____________(afont); _____________________("This is a test",10,40); } } 程序填空: extends new setBackground setFont drawString 26、阅读以下程序 public class Test( public static void main(String[] args){ ("sqrt(2.0)=%f", (2.0)); } } 此程序的输出是: sqrt(2.0)=1.414214 27、阅读以下程序 public class SumTest { public static void main(String args[]) { double sum = 0.0; for(int i=1; i <= ; i++) sum += ouble(args[i]); n("sum=" + sum); } } 此程序完成的功能是: 从命令行输入若干个数,求这若干个数的和并输出结果。 28、阅读以下程序 import r; class IfTest { public static void main(String[] args){ double y,x; Scanner keyin = new Scanner(); (”请输入x的值:”); x = uble(); if ( x > 0 ) y = 2 * x; else if( x == 0) y = 2 + (x); else y = x * x + 1; n("y=" + y); } } 此程序完成的功能是: 从键盘输入一个数x,根据x的值求出相应的y值。 2*x x>0 y= 2 + cos(x) x=0 x2+1 x<0 29、阅读以下程序,完成填空,使其完整。 import ____________________; class Rectangle { public static void main(String[] args) { ______________w,l,S,s; //定义变量 Scanner keyin = new Scanner(); (”请输入长方形的长:”); l = uble(); (”请输入长方形的宽:”); w = ________________________ ; //输入宽 S = ________________________ ; //计算面积并保存在S中 s = _________________________ ; //计算周长并保存在s中 n("此长方形的面积:"+ S +"n此长方形的周长:" +s); } } 程序填空: r或.* double uble() w*l (w+l)*2 30、阅读以下程序: public class Sum public static void main(String args[]) { double sum = 0.0; for(int i = 1; i <= 100; i++) sum += i*i*i; n(”sum=” + sum); } } 此程序完成的功能是: 计算并输出1到100的立方和sum值。 31、阅读以下程序 class Test { public static void main(String[] args){ int i = 1; do { if (i % 3 == 0) (” ”+i); i++; } while(i <= 20); } } 此程序的输出结果是: 3 6 9 12 15 18 32、阅读以下程序 class Test{ int a; static int b; Test() { a=20; b=30;} public static void main(String[] ars){ Test sl = new Test(); Test s2 = new Test(); s2.a = 100; s2.b = 10000; n("s1.a=" + s1.a); n("s1.b=" + s1.b); n("s2.a=" + s2.a); n("s2.b=" + s2.b); } } 此程序的输出结果是: s1.a=20 s1.b=10000 s2.a=100 s2.b=10000 33、阅读以下程序,并填空。 import java r; class TestSushu { public static void main(String[] args){ int m; boolean flag = true; Scanner keyin = ____________________ //创建Scanner输入对象 (”请输入要测试的数:”); m = __________________ //用Scanner对象输入一个整数 for(int i = 2; i <= ________________ ; i++){ //对m开平方根 if ( m % i == 0) { flag = false; ________________ //结束循环 } } if (__________) //判断 n("" + m +"是素数"); else n(""+ m +"是合数"); } } 程序填空: new Scanner(); t(); (m) break; flag


发布评论