当前位置:文档之家› Java复习题阅读程序题_软件

Java复习题阅读程序题_软件

《JA V A程序设计》复习题之(三)阅读程序题三、程序阅读题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] = () );} catch (IOException e) { };for (i= n-1; i >= 0; i=i-2)" ");}}请写出该程序的功能:该程序使用字符缓冲输入流从键盘输入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];"s=" + s);}}请写出该程序的输出结果:s=2603、阅读以下程序: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<; i++)(data_arr[i]);("Good morning!"); 'for(int i=; i>=0; i=i-2) {(i*4);System,(" "+());(40);();} catch (IOException e) {"File access error: "+e);}}}该程序的输出结果是:99 65 1 23 66 Good morning!4、阅读以下程序并填空。

class _____________________ extends Exception {String mymsg="我自己定义的异常!";double mynum = ;MyException () { super("首字母不能为A! ");}MyException (String msg){_____________ } harAt(O)== 'A') { MyException e = new MyException();"kkkk:" + ());();System.out.println("*********in try*********");__________________________; harAt(O)== 'B') {throw new MyException ("第一个字符不应是B! ");} else { }} catch ( __________________________ ) {();"" + ());} catch( __________________________ ) {"命令行参数个数错!");}}}程序填空:MyExceptionsuper(msg)throw eMyException aaa ArrayIndexOutOfBoundsException5、阅读以下程序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(){ "a="+ a +"\nb="+ b +"\nc="+ c); }}请写出该程序的运行结果:a=60b=20c=306、阅读以下程序import .*;public class abc {public static void main(String args[]) {String sl = "Hello!";String s2 = new String("World!");}}请写出该程序的运行结果: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];}max + " " + min);}}请写出该程序完成的功能:在数组中查找并输出最大值和最小值。

8、阅读以下程序import .*;import class DrawMylmage extends Applet {Image myImage; ;import .*;import .*;public class Mypicture __________________ Applet {Image image;public void _________() {try {image=getlmage(new URL(getCodeBase(),'')); } _______________(MalformedURLException e) { }public void paint(Graphics g) {(image,0,0,__________);}public void start() {______________();}}程序填空题:extends init catch this repaint 10、阅读以下程序:public class Sum {public static void main( String args[]) { double sum = ;for ( int i = 1; i<= 100; i ++ )sum += i;,"sum=" + sum );}}该程序完成的功能是:求sum=1+2+3+...+100的和。

11、阅读以下程序:class SuperClass {int a,b;SuperClass(int x,int y) { a=x; b=y; }voidshow() { "a="+ a + "\nb="+ b); }}class SubClass extends SuperClass {int c;SubClass(int aa,int bb,int cc) {super(aa,bb);c=cc;}voidshow() {"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(){"a="+ a +"\nb="+ b +"\nc="+ c);}class test {public static void main(String[] args) { SuperClass p=new SubSubClass(10,20,30);();}}该程序的输出结果是:a=60b=20c=3012、阅读以下程序:import .*;publiic class Test {public static void main(String args[]) {AB s = new AB("Hello!","I love Java.");() );}}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) {"使用格式错误!正确格式为:java mycopy源文件名目标文件名");(0);} catch (FileNotFoundException e3) {"文件没有找到!");} catch (IOException e2) {"流错误!");}}}程序填空:import .*;FileOutputStreamargs[0](ch);ArrayIndexOutOfBoundsException14、阅读以下程序import .*;public class Reverse {public static void main(String args[]) {int i,n=10;int a[] = new int[10];for(i=0; i<n; i++)try {BufferedReader br= new BufferedReader(new InputStreamReader);a[i]=()); ;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];"s=" + s);}}请写出此程序的输出结果:s=18016、阅读以下程序(提示:注意同步)class One{synchronized void display(int num) {"two " + num);try {(1000);} catch (InterruptedException e) {中断”);}完成”);}}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(){(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);"Synch结束!");}}此程序的输出结果是:two 100完成two 100完成two 100完成two 100完成Synch结束!17、阅读以下程序,并填空。

相关主题