Java面向对象程序设计期末题库读程序题

Java 读程序

结果

1、class V1{

 int a=1;

 void m1(){ System.out.println(a); }

}

class E1 extends V1{

 int a=2,b=3,c;

 void m2(){ System.out.println(a); }

}

class H{

 public static void main(String[] s){

   E1 e1=new E1();

   e1.m1();  e1.m2(); }

}

结果:

1

2

2、class V1{

 public int x=2;

 V1(){ System.out.println("V1"); }

}

class V2 extends V1{

 public int x=3;

 V2(){ System.out.println("V2"); }

}

class H{

 public static void main(String[] s){

   V1 e1=new V2();

   System.out.println(e1.x); } }

结果:

V1

V2

2

//*V1 e1=new V2();等价成:

  V1 e1;

e1=newV2(); 这样就很明显了

3、import    java.io.*;public  class  abc{         public  static  void  main(String args[ ])          {    AB  s = new  AB("Hello!");               System.out.println(s.toString( ));          }} class   AB {  String   s1;  String   s2;  AB( String  str1 , String  str2 )  {  s1 = str1;  s2 = str2; }  public   String   toString( )  { return  s1+s2;}}

以上程序编译错误:参数不匹配

改为:

import    java.io.*;

public  class  Test

{         public  static  void  main(String args[ ])

          {    AB  s = new  AB("Hello","world!");

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

          }

}

class   AB {

  String   s1;

  String   s2;

  AB( String  str1 , String  str2 )

  {  s1 = str1;  s2 = str2; }

  public   String   toString( )

  { return  s1+s2;}

}

结果为:Helloworld!

4、 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);}           }

结果:180

5、 class V1{

 public int x=4;

 V1(){ System.out.println("V1"); }

}

class V2 extends V1{

 public int x=7;

 V2(){ System.out.println("V2"); }

}

class H{

 public static void main(String[] s){

   V1 e1=new V2();

   System.out.println(e1.x); }

 }

结果:

V1

V2

4

6、class V1{

 int a=3;

 void m1(){ System.out.println(a); }

}

class E1 extends V1{

 int a=5,b=3,c;

 void m2(){ System.out.println(a); }

}

class H{

 public static void main(String[] s){

   E1 e1=new E1();

   e1.m1();  e1.m2(); }

}

结果:

3

5

1、int i= 1, j= 10 ;

do{

if (i++> --j) continue;

} while (i<5);

System.out.println(“i=”+i+”j=”+j);  简单的java程序的考查

答:i=5  j=6

2、 import  java.io.*;

public class abc

{

    public static void main(String args[])

    {

        String  s1 = "Hello!";

        String  s2 = new String("I  like  Java!");

        System.out.println(s1+"  "+s2);

    }

}  简单的java中的类的考查。

答: Hello! I like Java!

3、int i=9;

switch(i){

default:System.out.println(“default”);

case 0: System.out.println(“zero”);

break;

case 1: System.out.println(“one”);

case 2: System.out.println(“two”);

}  switch语句的考查

答:default

zero

4.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 ( i%3 = = 0 )  s += a[i] ;

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

           }

     }

     答:s = 120

5、public class Waiter {

int var;

Waiter(int var) {

this("Welcome");

}

Waiter(String s) {

this();

System.out.println(s);

}

Waiter() {

System.out.println("Good-bye");

}

public static void main(String[] args) {

Waiter t = new Waiter(1);

}

}

答:Good-bye

Welcome

功能

1、public class H{

 public static void main(String args[]){

  Fact N=new Fact(4);

  System.out.print(N.fact());

} }

class Fact{

  int n;

  Fact(int nn) {n=nn;}

   int fact(){

   int  i,f=1;

   for(i=1;i<=n;i++)

   f=f*i;

   return f;

} }

功能:计算n!(n的阶乘)

2、public class H{

 public static void main(String args[]){

   int i,Max,Min;

   int a[]={12,67,8,98,23,56,124,55,99,100};

   Max=a[0];Min=a[0];

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

    if(a[i]<Min) Min=a[i];

    if(a[i]>Max) Max=a[i];

   }

 System.out.print(Max+" "+Min);

} }

功能:查找数组a的最大值和最小值

3.import    java.io.*;   public    class   abc   {   public   static   void   main(String args[])         {   SubClass    sb = new   SubClass( );                     System.out.println(sb.max( ));         }     }    class    SuperClass   {   int  a = 10 , b = 20 ;  }   class  SubClass  extends  SuperClass   {   int  max( ) {  return   ((a>b)?a:b);  }  }

功能:输出两个数中较大的

3、import   java.io.* ;

public   class   abc {   public  static  void   main(String  args[ ])    {  int   i , n = 10 , max = 0 , min = 0 ,  temp = 0;          try {                 BufferedReader  br = new BufferedReader(                            new  InputStreamReader(System.in));                max = min = Integer.parseInt(br.readLine( ));          } catch ( IOException  e ) { } ;      for  ( i = 2 ; i <= n ; i ++ )  {          try {                 BufferedReader  br = new BufferedReader(                            new  InputStreamReader(System.in));                temp = Integer.parseInt(br.readLine( ));          if  (temp > max ) max=temp;         if  (temp < min) min=temp;         } catch ( IOException  e ) { } ;    }      System.out.println("max="+max+"\nmin="+min);    }}

功能:从控制台输入10个数,程序选出最大的和最小的

5、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 ;      System.out.println( "sum="+sum );   }

功能:计算1/1+1/2+……+1/100

6、import  java.io.* ;    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(System.in));                 a[i] = Integer.parseInt(br.readLine( ));  // 输入一个整数            } catch ( IOException  e ) { } ;            for  ( i = n-1 ; i >= 0 ; i ―― )                System.out.print(a[i]+"  ");        System.out.println( );        }   }

功能:实现对输入的10个数排序

回答问题

1.现有类说明如下,请回答问题:

public class A

{

      String  str1=" Hello! \t";

      String  str2=" How  are  you? ";

      public String  toString( )

      { return  str1+str2;  }

}

public class B extends A

{

      String  str1="\b\b,Bill.";

      public String toString( )

      { return  super.str1+str1;  }

}

问题:

1)类A和类B是什么关系?(继承关系)

2)类A和类B都定义了str1属性和方法toString( ), 这种现象分别称为什么?(属性的隐藏;方法的覆盖)

3)若a是类A的对象,则a.toString( )的返回值是什么?

   (Hello! How  are  you?)

4)若b是类B的对象,则b.toString( )的返回值是什么?(Hello,Bill.)

2.现有一个类定义如下,请回答问题:

    class Employee

    {

        String name;

        int age;

        double wage;

        static int No=0;

        Employee(String a1,int a2,double a3)

        {

           name=a1; age=a2; wage=a3;

           No++;  

        }

    }

在使用该类时,已使用下面语句生成了该类的对象:

Employee e1,e2;

e1=new Employee("王劲",26,6300);

e2=new Employee("张山",30,3800);

问题:

1)e2.name,e2.age,e2.wage的值各是什么?(张山;30;3800.0)

2)生成对象e1、e2后,e1.No值为多少?能否通过类名做前缀引用属性No?(2; 能)

3.阅读程序,回答问题。

public class InheritTest1

{

  public static void main (String[] args)

  {

     A  aa;           B  bb;

     aa=new  A( );    bb=new  B( );

     aa.show( );       bb.show();

  }

}

class A

{

  int a=1;

  double d=2.0;

  void show( )

  {   System.out.println("Class A: "+"\ta="+a +"\td="+d);   }

}

class B extends A

{

  float a=3.0f;

  String d="Java program.";

  int b=4;

  void show( )

  {

     System.out.println("Class A: "+"\ta="+super.a +"\td="+super.d);

     super.show( );

     System.out.println("Class B: "+"\ta="+a +"\td="+d+"\tb="+b);

  }

}

问题:1)这是哪一类java程序?(java应用程序)

      2)类A和类B是什么关系?(类B是类A的子类)

3)按程序输出的格式写出程序运行后的结果.

(程序运行结果如下:

Class A:        a=1     d=2.0

Class A:        a=1     d=2.0

Class A:        a=1     d=2.0

Class B:        a=3.0    d=Java program. b=4  )

4.现有类说明如下,请回答问题:

class A

{

   int x=10;

   int getA(){return x;}

}

class B extends A

{

   int x=100;

   int getB(){return x;}

}

问题:1)类B是否能继承类A的属性x?(能)

      2)若b是类B的对象,则b.getB()的返回值是什么?(100)

      3)若b是类B的对象,则b.getA()的返回值是什么?(10)

4)类A和类B都定义了x属性,这种现象称为什么?

(属性的隐藏)

5.有如下源程序,请回答问题:

class A

{     String s="class A";    }

class B extends A

{     String s="class B";    }

public class TypeConvert

{

public static void main(String args[])

{

B b1,b2=new B();

A a1,a2;

a1=(A)b2;

a2=b2;

System.out.println(a1.s);

System.out.println(a2.s);

b1=(B)a1;

System.out.println(b1.s);

System.out.println(b2.s);

}

}

问题:  该程序的四行输出各是什么?

(class A

class A

class B

class B)

6.现有类说明如下,请回答问题:

public class A

{

      int x=888;

      String str="I like: ";

      public String toString()

      {   return  str+x;  }

}

public class B extends A

{

      String x="java";

      public String toString()

      { return  str+x+" and "+super.x;  }

}

问题:1)类A和类B是什么关系?(类B是类A的子类)

      2)类A和类B都定义了x属性和方法toString(),这种现象分别称为什么?(属性的隐藏和方法的覆盖)

      3)若a是类A的对象,则a.toString( )的返回值是什么?(I like: 888)

4)若b是类B的对象,则b.toString( )的返回值是什么?

  (I like: java and 888)

7.运行类C的输出结果是什么?

class A

{

public A()

{

System.out.println(“The default constructor of A is invoked”);

}

}

class B extends A

{

public B()

{

}

}

public class C

{

public static void main(String[] args)

{

B b = new B();

}

}

8.阅读下列程序写出输出结果:

class A

{     String s="class A";  

          void show()

          {

             System.out.println(s);

}

  }

class B extends A

{     String s="class B";    

           void show()

          {

             System.out.println(s);

}

}

public class TypeConvert

{

public static void main(String args[])

{

B b1;

B b2=new B();

A a1,a2;

a1=(A)b2;

a2=b2;

System.out.println(a1.s);

            a1.show();

System.out.println(a2.s);

            a2.show();

b1=(B)a1;

System.out.println(b1.s);

            b1.show();

System.out.println(b2.s);

            b2.show();

}

}

答案:

class A

class B

class A

class B

class B

class B

class B

class B

1.下面是一个类的定义,根据题目要求回答以下问题.

class  B

{

private int x;

   private char y;

public B(int i,char j)

{

x=i; y=j;

}

public void show()

{

System.out.println("x="+x+"; y="+y);

}

public void methodC(int x)

{

this.x=this.x+x;

      y++;

show();

}

}

(1)定义类B的一个对象b,将类中的变量x初始化为10、变量y初始化为’A’,请写出相应的语句。(B  b=new B(10,’A’);)

(2)若在(1)问基础上有方法调用语句:

b.show();

则输出如何?(x=10; y=A)

(3)若在(1)问基础上增加语句: b.methodC(1); 则输出为何?(x=11; y=B)

(x=11; y=B)

2.阅读程序,回答问题。

 public  class  Test52

   {

     String  static  str1="Hello, Java world! \t";

     String  static  str2="Hello, students! ";

     

     public  static  void  main(String  args[])

     {   System.out.print(str1);  System.out.println(str2);   }

   }

问题:1)这是哪种形式的 java 程序 ?(java应用程序)

      2)程序的输出是什么?

       (Hello, Java world! Hello, students!  )

3. 写出下列程序的输出结果

public class Test

{

public static void main(String[] args)

{

Count myCount = new Count();

int times = 0;

for(int i=0;i<100;i++)

increment(myCount , times);

System.out.println(“count is” + myCount.count);

System.out.println(“time is”+ times);

}

public static void increment(Count c , int times)

{

c.count++;

times++;

}

}

class Count

{

public int count;

Count(int c)

{

count =c;

}

Count()

{

count =1;

}

}

答案:

count 101

times 0

5.阅读下面程序,回答问题:

public class Foo

{

int i;

static String s;

void imethod()

{

}

static void smethod()

{

}

}

设f是Foo的一个实例,下列语句正确吗?

System.out.println(f.i);

System.out.println(f.s);

f.imethod();

f.smethod();

System.out.println(Foo.i);

System.out.println(Foo.s);

Foo.imethod();

Foo.smethod();

答案:

   System.out.println(f.i);

答案: 正确

System.out.println(f.s);

答案: 正确

f.imethod();

答案: 正确

f.smethod();

答案: 正确

System.out.println(Foo.i);

答案: 错误

System.out.println(Foo.s);

答案: 正确

Foo.imethod();

答案: 错误

Foo.smethod();

答案: 正确

6.下列程序的输出结果是什么?

public class Foo

{

static int i=0;

static int j=0;

public static void main(String[] args)

{

int i=2;

int k=3;

{

int j=3;

System.out.println(“i + j is ”+ i + j);

}

k = i +j;

System.out.println(“k is ”+k);

System.out.println(“j is ”+ j);

}

}

答案:

i + j is 23

k is 2

j is 0

7. 根据下面的程序,指出下面每个元素的作用域(类作用域或块作用域)

变量x

变量y

方法cude

变量 i

变量 yPos

public class CubeTest

{

int x;

public void print()

{

int yPos = 10;

for(x=1;x<=10;x++)

  {

   System.out.println(cude(x));

     for(int i=1;i<=yPos;i++)

          System.out.println(" ");

  }

     }

     

     public int cude (int y)

     {

      return y*y*y;

     }

}

答案:

变量x         : 类作用域

变量y         : 块作用域

方法cude      : 类作用域

变量 I         : 块作用域

变量 yPos      : 块作用域

1.阅读下面程序回答问题:

import java.io.*;

public class Class1

{

public static void main(String args[])

{

int a=5;

int b=0;

System.out.println(a/b);

try

{

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

System.out.println(a/b);

System.out.println("a*a="+a*a);

}

catch(ArithmeticException e)

{ System.out.println("除数为0,这是不行的!"); }

finally

{ System.out.println("finally被执行!");       }

System.out.println("异常已发生,但不影响程序的执行!");

}

}

1)运行上述程序,输出结果是什么?(异常提示信息除外)

答案:除提示异常提示信息外,无其他输出

2)将变量b的初值改成5后,输出结果是什么?

答案:

1

a=5

1

a*a=25

finally被执行!

异常已发生,但不影响程序的执行!

  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
java程序设计》课程的题库资料,由贺州学院整理,可供学生期末课程复习使用,也可以供相关任课教师出卷使用。 内容示例为: 9. 阅读下列程序片段,写出运行结果。 public class Test { public static void main(String[] args) { int percent = 10; tripleValue(percent); System.out.println(percent); } public static void tripleValue(int x) { x = 3 * x; } } 代码执行后输出的结果是______。 答案:10 [解析] static 关键字应用的场合有:①用来修饰类中定义的变量,这样的变量称为类变量或静态变量。②可以用来修饰类中定义的方法,这样的方法称为静态方法。③用来修饰初始化语句块,这样的语句块常称为静态初始化语句块。static 在这里表示这个方法为类方法,不属于任何对象实例,而是类所有,描述对象的共有动作,可以用类名直接调用。在调用了tripleValue函数之后,函数的值没有返回,所以percent的值还是10。 10. 阅读下列程序片段,写出运行结果。 class Shape { public Shape() { System.out.print("Shape"); } } class Circle extends Shape { public Circle() { System.out.print("Circle"); } } public class Test { public static void main(String[] args) { Shape d = new Circle(); } } 代码执行后输出的结果是______。 答案:ShapeCircle [解析] 继承是而向对象编程的一个主要优点之一,它对如何设计Java类有着直接的影响。继承有如下几点好处: ①它可以利用已有的类来创建自己的类,只需要指出自己的类和已有的其他类有什么不同即可,而且还可以动态访问其他有 关类中的信息。 ②通过继承,可以利用Java类库所提供的丰富而有用的类,这些类都已经被很好地实现。 ③当设计很大的程序时,继承可以使程序组织得层次清晰,有利于程序设计相减少错误的发生。该程序首先编写了一个Shape的类,然后又编写一个类Circle去继承Shape类。由于子类拥有父类所有的属性和方法,所以输出的是ShappeCircle。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值