(完整版)java复习题(程序填空)程序填空题:1.public class Sum{public static void main(String [] args){int j=10;System.out.println("j is : "+j);calculate(j);System.out.println("At last, j is : "+j);}static void calculate (int j){for (int i = 0;i<10;i++)j++;System.out.println("j in calculate() is: "+j);}}输出结果为:j is : (1)j in calculate() is : (2)At last j is : (3)答案:(1)10 (2)20 (3) 102.按要求填空abstract class SuperAbstract{void a(){…}abstract void b();abstract int c(int i);} interface AsSuper{void x();}abstract class SubAbstract extends SuperAbstractimplements AsSuper {public void b(){…}abstract String f();}public class InheritAbstract extends SubAbstract{public void x(){…}public int c(int i ) {…}public String f(){…}public static void main(String args[]){InheritAbstract instance=new InheritAbstract();instance.x();instance.a();instance.b();instance.c(100);System.out.println(instance.f());}}在以上这段程序中:抽象类有:SuperAbstract和(1)(写出类名)非抽象类有:(2)(写出类名)接口有:(3)(写出接口名)AsSuper中的x()方法是(4)方法,所以在InheritAbstract中必须对它进行(5)答案:(1) SuperAbstract;(2) InheritAbstract;(3) AsSuper;(4) 抽象抽象抽象抽象;(5) 覆盖和实现覆盖和实现覆盖和实现覆盖和实现。
3.按注释完成程序public class Leaf {private int i = 0; //此属性值用于检验Leaf increment(){ //定义方法increment(),返回值是Leaf类的对象i++;return (1) ;//将当前对象的地址作为返回值返回}void print() {System.out.println(" i = " + i);}public static void main(String args[]){Leaf x=(2); //af类的对象xx.increment().increment().increment().print();}//多次调用方法increment(),返回的都是x的地址,i 值表示调用次数}输出结果为i = (3)答案: (1) this;(2) new Leaf();(3) 34.按注释提示完成文件复制的程序//FileStream源代码如下:import java.io.*;class FileStream {public static void main(String args []) {try {File inFile = new File("file1.txt"); //指定源文件File outFile=new File("file2.txt"); //定目标文件FileInputStream fis =(1);FileOutputStream fos = new FileOutputStream(outFile);int c;//逐字节从源文件中输入,再输出到fos流while ((c = fis.read ())!=-1)(2);fis.close();fos.close();}catch (Exception e) {System.out.println("FileStreamsTest: "+e);}}}答案:(1) new FileInputStream(inFile);(2) fos.write(c);5. 阅读程序,给出结果:// AbstractClassDemo.java源代码如下:abstract class Shape { //定义抽象类Shape和抽象方法display abstract void display();}class Circle extends Shape {void display() { //实现抽象类的方法System.out.println("Circle");}}class Rectangle extends Shape {void display() { //实现抽象类的方法System.out.println("Rectangle");}}class Triangle extends Shape {void display() { //实现抽象类的方法System.out.println("Triangle");}}public class AbstractClassDemo{public static void main(String args[]){(new Circle()).display(); //定义无名对象来调用对应的display方法(new Rectangle()).display();(new Triangle()).display();}}输出结果是?答案:(1) Circle;(2) Rectangle;(3) Triangle。
6.下面程序的运行结果是。
public class ArrayT est{public static void main(String[] args){int []a = new int[4];for(int i=0; i<="" p="">[i] = 5-i;for(int b:a)System.out.print(b);}}答案:54327.下面程序的运行结果是。
public class Test2{public String add(String a){a = a + “World”;return a;}public static void main(String[] args){Test2 t = new Test2();String s = “hello”;System.out.print(t.add(s));}}答案:helloWorld8.下列代码执行的结果是public class Expression{public static void main(String[] args){int v1=10,v2=99,v3=0;System.out.println((v1<0)&&(v2/v3)==3);}}答案:false10下列代码的功能是把按钮save添加到窗口myFrame的中间,在空白处应填入的代码是。
public class MyFrame{public static void main(String[] args){JFrame myFrame = new JFrame();JButton save = new JButton();myFrame.getContentPane().add(save," ");myFrame.setSize(200, 200);myFrame.setVisible(true);}}答案:BorderLayout.CENTER11.下列程序的功能是创建了一个显示5个“Hello!”的线程并启动运行,请将程序补充完整。
public class ThreadTest extends Thread{public static void main (String args[ ]){ThreadTest t =new ;T.start ( ) ;}public void run ( ){int i =0;while(true){System.out.println("Hello!");if (i++ = = 4) break;}}}答案:ThreadTest12.下列程序的功能是统计命令行参数的个数,请在下划线处填上适当的代码。
public class Length{public static void main(String args[]){System.out.println("number of String args:"+args. );}}答案:length13. 定义一个立方体的类Prog1,属性包括长、宽、高;方法包括:构造方法(初始化立方体的长宽高)、计算体积方法、计算底面积方法,显示立方体的各属性方法;最后创建一个立方体对象,计算并显示其底面积和体积class Prog1{double length;double width;double height;Prog1( ,double w,double h){length=l;;height=h;}double volume(){length* width * height;}double area(){length*width;}void print(){System.out.println("立方体的长:"+length+" 宽:"+width+" 高:"+height+" 体积:"+volume()+" 底面积:"+area());}public static void main(String args[]){Prog1 b = new Prog1(3,4,5);b.print();}}答案:1)double l2)width=w3)return4)return14. 定义一个学生类Prog1,包括姓名、学号、数学成绩、外语成绩、计算机成绩等属性,以及初始化各属性的构造方法、计算学生平均成绩的方法、显示学生成绩单的方法import java.awt.*;public class Prog1{String name;long no;double math;double english;double computer;Prog1( s,long n,double m,double e,double c){name=s;no=n;math=m;english=e;mputer=c;}public verage(){return (math+english+computer)/3;}public void print{System.out.println("姓名:"+name);System.out.println("学号:"+no);System.out.println("数学成绩:"+math);System.out.println("外语成绩:"+english);System.out.println("计算机成绩:"+computer);}public static void main (String args[]){Prog1 b=new Prog1("李利",20030101,78,67,89);b.print();System.out.println("平均分:"+b. );}}答案1)String2)double 或float3)()4)average()15. 编写一个程序,把六个按钮分别标识为‘A’至‘F’,并排列成一行。