JAVA复习题继承.doc

上传人:s****u 文档编号:12749567 上传时间:2020-05-22 格式:DOC 页数:24 大小:735.52KB
返回 下载 相关 举报
JAVA复习题继承.doc_第1页
第1页 / 共24页
JAVA复习题继承.doc_第2页
第2页 / 共24页
JAVA复习题继承.doc_第3页
第3页 / 共24页
点击查看更多>>
资源描述
类的继承习题一、 选择题1. Java语言的类间的继承关系是( B )。 A) 多重的 B) 单重的 C) 线程的 D) 不能继承 2. 以下关于Java语言继承的说法正确的是( C )。 A)Java中的类可以有多个直接父类 B)抽象类不能有子类 C)Java中的接口支持多继承 D)最终类可以作为其它类的父类3. 现有两个类A、B,以下描述中表示B继承自A的是(D)。 A) class A extends B B) class B implements A C) class A implements B D) class B extends A4. 下列选项中,用于定义接口的关键字是( A)。 A)interface B) implements C) abstract D) class5. 下列选项中,用于实现接口的关键字是( B)。 A)interface B) implements C) abstract D) class6. Java语言的类间的继承的关键字是( B )。 A) implements B) extends C) class D) public 7. 以下关于Java语言继承的说法错误的是( A )。 A)Java中的类可以有多个直接父类 B)抽象类可以有子类 C)Java中的接口支持多继承 D)最终类不可以作为其它类的父类8. 现有两个类M、N,以下描述中表示N继承自M的是(D)。 A) class M extends N B) class N implements M C) class M implements N D) class N extends M9. 现有类A和接口,以下描述中表示类实现接口的语句是(A)。 A) class implements B B) class B implements C) class extends B D) class B extends 10. 下列选项中,定义抽象类的关键字是( C)。 A)interface B) implements C) abstract D) class11. 下列选项中,定义最终类的关键字是(D)。A)interface B) implements C) abstract D) final12. 下列选项中,哪个是java语言所有类的父类(C)A)String B) Vector C) Object D) KeyEvent13. java语言中,用于判断某个对象是否是某个类的实例的运算符是(A)A)instanceof B) + C) isinstance D) &14. 下列选项中,表示数据或方法可以被同一包中的任何类或它的子类访问,即使子类在不同的包中也可以的修饰符是(B)A)public B) protected C) private D) final15. 下列选项中,表示数据或方法只能被本类访问的修饰符是(C)A)public B) protected C) private D) final16. 下列选项中,接口中方法的默认可见性修饰符是(A)A)public B) protected C) private D) final17. 下列选项中,表示终极方法的修饰符是:(B)A)interface B) final C) abstract D) implements18. 下列选项中,定义接口MyInterface的语句正确的是:(A)A)interface MyInterface B) implements MyInterface C) class MyInterface D) implements interface My 19. 如果子类中的方法mymethod()覆盖了父类中的方法mymethod(),假设父类方法头部定义如下:void mymethod(int a),则子类方法的定义不合法的是:(C)A) public void mymethod(int a) B) protected void mymethod(int a)C) private void mymethod(int a) D) void mymethod(int a)二、 填空题1. 如果子类中的某个变量的变量名与它的父类中的某个变量完全一样,则称子类中的这个变量_了父类的同名变量。(隐藏)2. 属性的隐藏是指子类重新定义从父类继承来的_。(同名变量或属性)3. 如果子类中的某个方法的名字、返回值类型和_与它的父类中的某个方法完全一样,则称子类中的这个方法覆盖了父类的同名方法。(参数列表) 4. Java 仅支持类间的_重继承。(单重)5. 抽象方法只有方法头,没有_。 (方法体)6. Java 语言的接口是特殊的类,其中包含_常量和_方法。(static(静态) ; abstract(抽象)7. 接口中所有属性均为_、_和_的。 (public、static、final)8. 如果接口中定义了一个方法methodA(),一个属性attA,那么一个类ClassA要实现这个接口的话,就必须实现其中的_方法。(methodA()9.一个类如果实现一个接口,那么它就必须实现接口中定义的所有方法,否则该类就必须定义成_的。(抽象的) 10. 如果子类中的某个方法的名字、_和参数列表与它的父类中的某个方法完全一样,则称子类中的这个方法覆盖了父类的同名方法。(返回值类型) 11. Java 仅支持类间的单重继承,接口可以弥补这个缺陷,支持_重继承(多重)12. 在方法头用abstract修饰符进行修饰的方法叫做_方法。(抽象)13.Java语言中用于表示类间继承的关键字是_。(extends)14. 接口中所有方法均为_和_的。 (public、abstract)15. Java语言中,表示一个类不能再被继承的关键字是_。(final)16. Java语言中,表示一个类A继承自父类B,并实现接口C的语句是_。 (class A extends B implements C)。17. 如果子类中的方法compute()覆盖了父类中的方法compute(),假设父类的compute()方法头部有可见性修饰符public,则methodS()的可见性修饰符必须是_。(public)三、 程序阅读题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=bb,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.0Class A: a=1 d=2.0Class A: a=1 d=2.0Class 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 TypeConvertpublic 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 Aclass Aclass Bclass 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 Apublic A()System.out.println(“The default constructor of A is invoked”);class B extends Apublic B()public class Cpublic 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 TypeConvertpublic 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 Aclass Bclass Aclass Bclass Bclass Bclass Bclass B四、 程序填空题1.下面是一个类的定义,完成程序填空。(Youwrite this.x=x;)public class Youwrite int x;_( ) x=0; Youwrite ( int x) _; 2.下面是定义一个接口ITF的程序,完成程序填空。 (interface abstract )public _ ITF public static final double PI=Math.PI; public _ double area(double a, double b);3.下面是定义一个接口A的程序,完成程序填空。(final ”;” )public interface A public static _ double PI=3.14159; public abstract double area(double a, double b)_五、 编程题简单类的继承:1.定义一个类,描述一个矩形,包含有长、宽两种属性,和计算面积方法。编写一个类,继承自矩形类,同时该类描述长方体,具有长、宽、高属性,和计算体积的方法。编写一个测试类,对以上两个类进行测试,创建一个长方体,定义其长、宽、高,输出其底面积和体积。public class TestExtendspublic static void main(String args)LiFangTi f=new LiFangTi();f.length=2;f.width=3;f.height=7;System.out.println(立方体的底面积为:+f.findArea()+立方体的体积为:+f.findVolume();class JuXingint length;int width;int findArea()return length*width;class LiFangTi extends JuXingint height;int findVolume()return findArea()*height;3. 定义一个Person类,它包括的属性有“姓名”和“性别”,为Person类派生出一个子类Student类,为Student子类添加两个属性年龄和成绩等级(用A,B,C,D,E表示),在子类中打印出学生的姓名、性别、年龄及成绩等级。class Personprotected String name;protected String sex;Person(String name,String sex)this.name=name;this.sex=sex;public String toString()String s=new String();s=name+t+sex;return s;class Student extends Personprotected int age;protected char grade;Student(String name,String sex,int age,char grade)super(name,sex);this.age=age;this.grade=grade;void print()String s=new String();s=super.toString();s=s+t+age+t+grade;System.out.println(s);public class TestPpublic static void main(String args)Student st1=new Student(zhangfei,male,23,E);Student st2=new Student(liubei,male,25,A);System.out.println(name+tset+tage+tgrade);st1.print();st2.print();4.定义一个类Rectangle代表矩形,其中包括计算面积的方法。再定义一个它的子类Square代表正方形,其中也包含计算面积的方法。编写一程序,测试新建子类对象的属性和方法。class Rectanglefloat length;float width;Rectangle(float len,float wh)length=len;width=wh;float getArea()return length*width;class Square extends Rectanglefloat length;Square(float len)super(len,len);length=len;float getArea()return super.getArea();public class TestRectanglepublic static void main(String args)Square sq=new Square(5.2f);System.out.println(side is +sq.length+,area is +sq.getArea();类与对象习题一、选择题1 定义类头时,不可能用到的关键字是( B)。 A) class B)private C)extends D)public2.下列类定义中,不正确的是(C)。A) class x . B) class x extends y . C) static class x implements y1,y2 . D) public class x extends Applet . 3.下列类头定义中,错误的是( A)。 A)public x extends y . B) public class x extends y . C)class x extends y implements y1 . D)class x .4.设 A为已定义的类名,下列声明A类的对象a的语句中正确的是( D)。 A) float A a; B) public A a=A( ); C) A a=new int( ); D) static A a=new A( );5.设 A为已定义的类名,下列声明A类的对象a的语句中正确的是(A)。 A) public A a=new A( ); B) public A a=A( ); C) A a=new class( ); D) a A;6.设 X 、Y 均为已定义的类名,下列声明类X的对象x1的语句中正确的是(C)。 A) public X x1= new Y( ); B) X x1= X ( ); C) X x1=new X( ); D) int X x1;7. 设X 、Y为已定义的类名,下列声明X类的对象x1的语句中正确的是(A)。 A) static X x1; B) public X x1=new X(int 123); C) Y x1; D) X x1= X( );8.通过类MyClass中的不含参数的构造方法,生成该类的一个对象obj,可通过以下语句实现: _。 (MyClass obj=new MyClass( );)9. 设i , j为类X中定义的int型变量名,下列X类的构造方法中不正确的是( A)。 A) void X(int k ) i=k; B) X(int k ) i=k; C) X(int m, int n ) i=m; j=n; D) X( )i=0;j=0; 10. 有一个类A,以下为其构造方法的声明,其中正确的是( A )。 A)public A(int x). B)static A(int x). C)public a(int x). D)void A(int x).11. 有一个类A,以下为其构造方法的声明,其中正确的是( B )。 A)void A(int x). B)A(int x). C)a(int x). D)void a(int x).12. 设i、j、k为类X中定义的int型变量名,下列类X的构造方法中不正确的是( B )。 A) X( int m) . B) void X( int m) . C) X( int m, int n) . D) X( int h,int m,int n) . 13. 设i , j为类X中定义的double型变量名,下列X类的构造方法中不正确的是(A )。 A) double X(double k ) i=k; return i; C) X( )i=6;j=8; B) X(double m, double n ) i=m; j=n; D) X(double k ) i=k; 14. 设a , b为类MyClass中定义的int型变量名,下列MyClass类的构造方法中不正确的是(A)。double MyClass(double k ) a=k; return a; MyClass( )a=6;b=8; MyClass (double m, double n ) a=m; b=n; MyClass (double k ) a=k; 15 定义类头时,不可能用到的关键字是(A)。 A)protected B) class C)extends D)public16.下列类头定义中,错误的是(A)。 A)public TestClass extends y . B) public class TestClass extends y . C)class TestClass extends y implements y1 . class TestClass .17.设 B为已定义的类名,下列声明B类的对象b的语句中正确的是(D)。 A) float B b; B) public B b=B( ); C) B b=new int( ); D) static B b=new B( );18.设 ClassA为已定义的类名,下列声明ClassA类的对象ca的语句中正确的是(A)。 A)public ClassA ca=new ClassA( ); B) public ClassA ca=ClassA( ); C) ClassA ca=new class( ); D) ca ClassA;19.设 A 、B 均为已定义的类名,下列声明类A的对象a1的语句中正确的是(C)。 A) public A a1= new B( ); B) A a1= A ( ); C) A a1=new A( ); D) int A a1;20. 设A 、B为已定义的类名,下列声明A类的对象a1的语句中正确的是(A)。 A) static A a1; B) public A a1=new A(int 123); C) B a1; D) A a1= A( );21.通过类A中的不含参数的构造方法,生成该类的一个对象a,可通过以下语句实现: _。 (A a=new A( );)22. 设m , n为类A中定义的int型变量名,下列A类的构造方法中不正确的是( A)。 A) void A(int k ) m=k; B) A(int k ) m=k; C) A(int m, int n )m=i; n=j; D) A( )m=0;n=0; 23. 有一个类Person,以下为其构造方法的声明,其中正确的是( A )。 A)public Person (int x). B)static Person (int x). C)public a(int x). D)void Person (int x).24. 有一个类Student,以下为其构造方法的声明,其中正确的是( B )。 A)void Student (int x). B) Student (int x). C)s(int x). D)void s(int x).25. 设i、j、k为类School中定义的int型变量名,下列类School的构造方法中不正确的是( B )。 A) School ( int m) . B) void School ( int m) . C) School ( int m, int n) . D) School ( int h,int m,int n) . 二、填空题1.下面是一个类的定义,请完成程序填空。(Myclass int j)public class _ int x, y; Myclass ( int i, _) / 构造方法 x=i; y=j; 2. 下面是一个类的定义,请将其补充完整。(Student String)class _ String name;int age;Student( _ s, int i) name=s; age=i; 3.下面是一个类的定义,请将其补充完整。(myclass static)class _ / 定义名为myclass的类 _ int var=666; static int getvar() return var; 4.下面程序的功能是通过调用方法max()求给定的三个数的最大值,请将其补充完整。(max(i1,i2,i3) static)public class Class1 public static void main( String args ) int i1=1234,i2=456,i3=-987; int MaxValue; MaxValue=_; System.out.println(三个数的最大值:+MaxValue); public _ int max(int x,int y,int z) int temp1,max_value; temp1=xy?x:y; max_value=temp1z?temp1:z; return max_value; 5.下面是一个类的定义,请将其补充完整。(class static)_ A String s; _ int a=666; A(String s1) s=s1; static int geta( ) return a; 6.下面是一个类的定义,请完成程序填空。(Room int n)public class _ int a, b; Room ( int m, _) / 构造方法 a=m; b=n; 7. 下面是一个类的定义,请将其补充完整。(class String)_ Teacher String name;int age;Teacher ( _ s, int i) name=s; age=i; 8.下面是一个类的定义,请将其补充完整。(Desk static)class _ / 定义名为Desk的类 _ int var=666; static int getvar() return var; 9.下面程序的功能是通过调用方法max()求给定的二个整数的最大值,请将其补充完整。(max(a1,a2) int)public class Class1 public static void main( String args ) int a1=1234,a2=456; int MaxValue; MaxValue=_; System.out.println(二个数的最大值:+MaxValue); public static _ max(int x,int y) int max_value; max_value =xy?x:y; return max_value; 10.下面是一个类的定义,请将其补充完整。(class static)_ Bank String s; _ int a=666; Bank(String s1) s=s1; static int geta( ) return a; 三、程序阅读题1.下面是一个类的定义,根据题目要求回答以下问题.class Bprivate 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 Testpublic static void main(String args)Count myCount = new Count();int times = 0;for(int i=0;i100;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 Countpublic int count;Count(int c)count =c;Count()count =1;答案:count 101times 04.写出下列程序的输出结果:public class Testpublic static void main(String args)Cirecle circle1 = new Circle(1);Cirecle circle2 = new Circle(2);/ Attempt to swap circle 1 with circle2System.out.println(“Before swap:circle1 = ”+circle1.radius+”circle2 = ”+circle2.radius);swap(circle1,circle2);System.out.println(“After swap: circle1 = ”+circle1.radius+”circle2 = ”+circle2.radius);public static void swap(Circle x , Circle y)System.out.println(“Before swap: x = ”+x.radius+”y = ”+y.radius);Circle temp = x;x = y;y = temp;System.out.println(“Before swap: x = ”+x.radius+”y = ”+y.radius);答案:5.阅读下面程序,回答问题:public class Fooint 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 Foostatic 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 23k is 2j is 07. 根据下面的程序,指出下面每个元素的作用域(类作用域或块作用域)变量x变量y方法cude变量 i变量 yPospublic 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. 面向对象的软件开发方法用什么把数据和基于数据的操作封装在一起?(类)2. 在一个类定义中,用什么描述对象的状态? 用什么描述对象的行为?(属性;方法)3. 什么方法是一个特殊的方法,用于对对象进行初始化? (构造方法)4. 一个源程序文件中,能有多于一个的 public 类吗?(不能)5. 构造方法的方法名可由编程人员任意命名吗? (不能,必须与类名同名)6. 类的构造方法名必须和类名相同吗?(必须)7. 构造函数有返回值吗?(没有)8. 构造函数可以重载吗? (可以)9. 如果一个类定义中没有定义构造方法,该类有构造函数吗? (有,编译器会自动生成一个缺省的不带参数的构造函数)10. 如果一个类定义中已经定义了构造方法,java还会给它定义缺省的构造方法吗?(不会)11. 类的访问控制权有哪两种?(public 和 缺省的(即没有访问控制修饰符))12. 用什么修饰符修饰的方法称为静态方法? 用什么修饰符修饰的属性称为静态属性?(static)13. 静态属性和静态方法仅属于类的一个具体对象吗?它属于谁? (不是;它是类的所有对象公有的)14. static 变量能是 private 变量吗?(可以)15. 使用静态成员都能以什么做前缀?(可以用类名或对象名做前缀)16. static方法中能有this引用吗?(不能)
展开阅读全文
相关资源
相关搜索

当前位置:首页 > 图纸专区 > 考试试卷


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!