超级无敌神奇的java期末考试题库2(2)

2019-09-02 13:58

x=10;y=’A’

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

6、下面是一个类的定义,请完成程序填空。 public class ____Myclass________ { int x, y;

Myclass (_int i,int j_____) // 构造方法 { x=i; y=j; } }

6、 以下方法fun的功能是求两参数之积。

int fun ( int a, int b ) { __return_a*b_;_____ }

7、 以下方法fun的功能是求两参数之积。 float fun ( int a, double b )

{ __return (float)(a*b);__________ } 8、 以下方法fun的功能是求两参数的最大值。 int fun ( int a, int b ) {___if(a>b) return a;

else return b;_ }

9、 以下方法m的功能是求两参数之积的整数部分。 int m ( float x, float y )

{ __return (int)(x*y);__________ }

10、 下面方法的功能是判断一个整数是否为偶数,将程序补充完整。 public _boolean____ isEven(int a) { if(a%2==0)

return _true___; else

return _false____; }

11、现有类说明如下,请回答问题: public class A {

String str1=\

String str2=\ public String toString( ) { return str1+str2; } }

public class B extends A {

String str1=\,Bill.\ public String toString( ) { return super.str1+str1; } }

问题:

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

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

6

属性的隐藏;(答:3)若a是类A的对象,则a.toString( )的返回值是什么? (答:Hello! How are you?)

4)若b是类B的对象,则b.toString( )的返回值是什么?(答:Hello,Bill.) 12.现有一个类定义如下,请回答问题? 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(\王劲\ e2=new Employee(\张山\问题:

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

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

补充:e1.No=e2.No=Employee.No=2

13.阅读程序,回答问题。 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 B extends A {

float a=3.0f;

String d=\

7

int b=4;

void show( ) {

System.out.println(\ super.show( );

System.out.println(\ } }

问题: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 14.现有类说明如下,请回答问题: 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属性,这种现象称为什么?(答:属性的隐藏)

15.有如下源程序,请回答问题: class A

{ String s=\ class B extends A

{ String s=\ 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);

8

System.out.println(b2.s); } }

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

class A class B class B

16.现有类说明如下,请回答问题: public class A {

int x=888;

String str=\ public String toString() { return str+x; } }

public class B extends A {

String x=\

public String toString()

{ return str+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) 17.运行类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) {

9

属性的隐 B b = new B(); } }

结果:The default constructor of A is invoked 8.阅读下列程序写出输出结果? class A

{ String s=\ void show() {

System.out.println(s);

}

}

class B extends A

{ String s=\ 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

10


超级无敌神奇的java期末考试题库2(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:技术问答

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: