【Java】练习题库 程序阅读题

三、写出下面程序的运行结果

1、 import java.io.*;

public class abc

{   

public static void main(String args [ ])

{    

AB s = new AB("Hello!","I love JAVA.");

         System.out.println(s.toString( ));

    }

}

class AB {

  String s1;

  String s2;

  public AB(String str1, String str2)

  { 

s1 = str1;  

s2 = str2; 

}

  public String toString( )

  { 

return s1+s2;

}

}

运行结果:Hello! I love JAVA.

2、 import java.io.* ;

    public class abc

    {

          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 < a.length ; i ++ )

                     if ( a[i]%3 = = 0 )  s += a[i] ; 

               System.out.println("s="+s);

           }

     }

运行结果:s = 180

3、import java.io.* ;

   public class abc

   {

         public static void main(String args[ ])

         { 

System.out.println("a="+a+"\nb="+b);  

}

    }

    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()

       {  

System.out.println("a="+a+"\nb="+b+"\nc="+c);  

}

  }

运行结果:a=60

       b=20

       c=30

三.程序阅读题

1.以下程序的输出结果为     相等      

class StringTest1

{

public static void main(String[] args) 

{

String s1="hello";

String s2=new String("hello");

if(s1.equals(s2)){

System.out.println("相等");

}else{

System.out.println("不相等");

}

}

}

2.以下程序段的输出结果为     5  6  7  8  9      

public class TestArray

{

    public static void main(String  args[ ]){  

     int  i , j ;

int  a[ ] = { 5,9,6,8,7};

    for  ( i = 0 ; i < a.length-1; i ++ ) {

    int  k = i;

    for  ( j = i ; j < a.length ;  j++ )

    if  ( a[j]<a[k] )  k = j;

   int  temp =a[i];

    a[i] = a[k];

    a[k] = temp;

    }

    for  ( i =0 ; i<a.length; i++ )

   System.out.print(a[i]+"  ");

       System.out.println( );

   }

}

3.写出以下程序的功能。

import java.io.*;

public class TestFile

{

   public static void main(String args[]) throws Exception

    {

    BufferedReader br = new BufferedReader(

new InputStreamReader(System.in));

    BufferedWriter bw = new BufferedWriter(new FileWriter(“input.txt"));

    String s;

    while (true)

 {

      System.out.print("请输入一个字符串: ");

      System.out.flush();

      s=br.readLine();

      if (s.length()==0) break;

      bw.write(s);

      bw.newLine();

    }

    bw.close();

  }

}

功能:从键盘逐一输入字符串,逐一输出至input.txt文件中,直至输入空行。

4.阅读以下程序,写出输出结果。

class  Animal {

  Animal() {

    System.out.print ("Animal  ");  }

}

public  

class  Dog  extends  Animal {

 Dog() {

    System.out.print ("Dog ");  }

  public static void main(String[] args) {

    Dog  snoppy= new  Dog();  }

}

输出结果:Animal   Dog

、程序阅读

1以下程序的输出结果为_ Peter is 17 years old!___。

public class Person {

String name;

int age;

public Person(String name, int age) {

this.name = name;

this.age = age;

}

public static void main(String[] args) {

Person c = new Person("Peter", 17);

System.out.println(c.name + " is " + c.age + " years old!");

}

}

2以下程序的输出结果为__课程号:101 课程名:ASP 学分:3___。

public class Course {

private String cNumber;

private String cName;

private int cUnit;

public Course(String number, String name, int unit) {

cNumber = number;

cName = name;

cUnit = unit;

}

public void printCourseInfo() {

System.out.println("课程号:" + cNumber + " 课程名:" + cName + " 学分:" + cUnit);

}

}

class CourseTest {

public static void main(String[] args) {

Course c;

c = new Course("101""ASP", 3);

c.printCourseInfo();

}

}

3以下程序的输出结果为__汤姆猫体重:20.0斤___。

public class Tom {

private float weight;

private static String name;

public void setWeight(float weight) {

this.weight = weight;

}

private void out() {

System.out.println(name + "体重:" + weight + "斤");

}

public static void main(String[] args) {

Tom.name = "汤姆猫";

Tom cat = new Tom();

cat.setWeight(20);

cat.out();

}

}

4以下程序的输出结果_姓名:Tom 年龄:15 家庭住址:金水区 电话:66123456 学校:九中_。

public class Father {

String nameaddresstel;

int age;

public Father(String name, int age) {

this.name = name;

this.age = age;

}

void out() {

System.out.print("姓名:" + name);

System.out.print(" 年龄:" + age);

}

void outOther() {

System.out.print(" 家庭住址:" + address);

System.out.print(" 电话:" + tel);

}

}

class Son extends Father {

String school;

public Son(String name, int age) {

super(name, age);

}

void out() {

super.out();

super.outOther();

System.out.println(" 学校:" + school);

}

public static void main(String args[]) {

Son son = new Son("Tom", 15);

son.address = "金水区";

son.school = "九中";

son.tel = "66123456";

son.out();

}

}

5下列程序的运行结果是__12345____。

public class MyClass {

int a[] = { 1, 2, 3, 4, 5 };

void out() {

for (int j = 0; j < a.length; j++)

System.out.print(a[j] + "");

}

public static void main(String[] args) {

MyClass my = new MyClass();

my.out();

}

}

三、程序阅读题

1阅读下面的程序,回答问题(u问3分,v问3分,共6分)。

 import java.awt.*;

    import javax.swing.*;

    public class T extends JFrame {

        public T ( ) {

            super("GridLayout");

            Container  con=this.getContentPane();

            con.setLayout(new GridLayout(2,3));

            con.add(new JButton("a"));  

            con.add(new JButton("b"));

            con.add(new JButton("c"));  

            con.add(new JButton("d"));

            con.add(new JButton("e"));

            con.add(new JButton("f"));

            setSize(200, 80);

            setVisible(true);

        }

        public static void main(String args[]) {

           new T();        

        }

}

u画图表示程序运行后的图形界面。

v如果程序通过实现某个接口处理按钮的动作事件,则该接口名为何?接口中的方法头声明如何?

答案:

u

v接口名:ActionListener

  接口中的方法:public void actionPerformed(ActionEvent e)

2阅读下面的程序,回答问题(u问3分,v问3分,共6分)。。

import  java.util.*;

public class T   {    

        public static void main(String args[])  {

            Set set = new TreeSet();

            set.add(new Integer(10));        

            set.add(new Integer(5));   

            set.add(new Integer(15));

            set.add(new Integer(5));   

            set.add(new Integer(10));

            System.out.println("size = " + set.size());

            Iterator   it=set.iterator();

            while(it.hasNext()){

                System.out.print(it.next()+"   ");

            }

        }    

}

u程序运行后输出的结果如何?

v说明java中的集合(Set接口)和映射(Map接口)的主要区别。

答案:

u

size = 3

5   10   15

vSet接口是不包含重复元素的集合;Map接口将键映射到值,键可以重复,但每个键最多只能映射一个值。

3阅读下面的程序,并回答问题(u问3分,v问3分,共6分)。

import java.io.*;

public class Test {

    public static void main(String args[]) throws IOException {

        BufferedReader buf=new BufferedReader(

                    new InputStreamReader(System.in));        

        while(true) {

            String  str=buf.readLine();

            if(str.equals("quit"))

                break;

            int x=Integer.parseInt(str);

            System.out.println(x*x);

        }

    }  

}

编译运行上面的程序:

u从键盘输入5,回车后输出的结果如何?

v从键盘输入quit,回车后程序执行情况如何?

答案:

u25 

v终止应用程序的运行。

、程序阅读题

1阅读下面的程序代码,并回答问题(u问3分,v问3分,共6分)。

String s1 = new String("abcde");

String s2 = new String("abcde"); 

boolean b1= s1.equals(s2);

boolean b2 = s1== s2;            

System.out.print(b1+"   "+b2);           

u程序段执行后,在命令行的输出结果如何?

v解释输出(1)的结果的原因?

答案:

u true  false

vequals方法比较两个字符串的内容是否相等;运算符“==”判断两个对象是否指向同一个引用,即是否为同一个对象。

2阅读下面的程序,并回答问题(u问3分,v问3分,共6分)。

import java.io.*;

public class Test {

    public static void main(String args[]) throws IOException {

        BufferedReader buf=new BufferedReader(

                    new InputStreamReader(System.in));        

        while(true) {

            String str = buf.readLine();

            if(str.equals("quit"))

                break;

            int x=Integer.parseInt(str);

            System.out.println(x*x);

        }

    }  

}

编译运行上面的程序:

u从键盘输入10,回车后输出的结果如何?

v从键盘输入exit,回车后程序能正确执行吗?为什么?

答案:

u100  

v不能;因为方法Integer.parseInt(str)不能将字符串“exit”转化为整数,抛出异常。

3写出下面的程序编译、运行后的结果(6分)。

public class Test{

     public static void main(String args[]) {

         new Student("Tom", 'm', 90, 88);

         new Student("Jack", 'm', 66, 89);

         new Student("Mary", 'f', 76, 86);

         System.out.println("name\tsex\tchinese\tenglish");

         Student.print();

     }

 }

 

 class Student {

     protected  String   name;

     protected  char     sex;

     protected  int      chinese;

     protected  int      english;

     protected  Student  next;

     static Student list;

     

     Student (String name, char sex, int chinese, int english)   {

         this.name=name;

         this.sex=sex;

         this.chinese=chinese;

         this.english=english;

         this.next=list;

         list=this;

     }

     static void print() {

         Student friend=list;

         if (friend==null)

             System.out.println("The list is empty.");

         else {

             do{

                 System.out.println(friend.toString());

                 friend=friend.next;

             }while(friend!=null);

         }

     }

     public String toString()  {

         return new String(name+"\t"+sex+"\t"+chinese+"\t"+english);

     }

 }

答案:

name    sex     chinese  english

Mary    f       76      86

Jack     m      66      89

Tom     m      90      88

三、程序填空题

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) 10。

2. 按要求填空

abstract class SuperAbstract{

void a(){…}

abstract void b();

abstract int c(int i);

}

interface AsSuper

{

void x();

}

abstract class SubAbstract extends SuperAbstract implements 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); //创建Leaf类的对象x

x.increment().increment().increment().print();

}//多次调用方法increment(),返回的都是x的地址,i 值表示调用次数

}

输出结果为 i =  (3)      

答案:

  1. this;
  2. new Leaf();
  3. 3

4.  按注释提示完成文件复制的程序

 //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。

二.读程序题

1. 读下列代码,说出这段程序的功能。 

import java.io.*; 

public class Test{ 

public static void main( String [] argv) { 

try { 

BufferedReader is = 

new BufferedReader( new InputStreamReader(System.in)); 

String inputLine; 

While ((inputLine = is.readLine ())!= null) { 

System.out.println(inputLine); 

is.close(); 

}catch (IOException e) { 

System.out.println("IOException: " + e); 

答案:读取键盘输入,显示到屏幕上,直到键盘输入为空为止。

2 读下列程序,写出正确的运行结果。 

class test { 

public static void main (String [] args ){ 

int x=9, y; 

if (x>=0) 

if (x>0)

y=1; 

else y=0; 

else y=-1; 

System.out.println(y); 

答案:1

3 读程序,写出正确的运行结果。 

public class Father{ 

int a=100; 

public void miner(){

 a--; 

public static void main(String[] args){ 

Father x = new Father(); 

Son y = new Son(); 

System.out.println(y.a); 

System.out.println( y.getA()); 

y.miner(); 

System.out.println(y.a); 

System.out.println(y.getA()); 

class Son extends Father{ 

int a = 0; 

public void plus(){

   a++; 

public int getA() { 

return super.a; 

答案:

0
100
0
99

猜你喜欢

转载自blog.csdn.net/CE00001/article/details/130163336