实用教程习题集-第4章.doc

上传人:rrsccc 文档编号:9350797 上传时间:2021-02-21 格式:DOC 页数:20 大小:86.50KB
返回 下载 相关 举报
实用教程习题集-第4章.doc_第1页
第1页 / 共20页
实用教程习题集-第4章.doc_第2页
第2页 / 共20页
实用教程习题集-第4章.doc_第3页
第3页 / 共20页
实用教程习题集-第4章.doc_第4页
第4页 / 共20页
实用教程习题集-第4章.doc_第5页
第5页 / 共20页
点击查看更多>>
资源描述

《实用教程习题集-第4章.doc》由会员分享,可在线阅读,更多相关《实用教程习题集-第4章.doc(20页珍藏版)》请在三一文库上搜索。

1、第4章 类的封装性、继承性、多态性及接口1、 判断题1. 如果类A和类B在同一个包中,则除了私有成员外,类A可以访问类B中所有的成员。()2. 接口中的成员变量全部为常量,方法为抽象方法。()3. 抽象类可以有构造方法,所以能直接用来生成实例。()4. Java的类不允许嵌套定义。()5. 包含抽象方法的类一定是抽象类,但有abstract修饰的类不一定包含抽象方法()6. 泛型只能用于类的定义中,不能用于接口的定义中。()7. 用final修饰的类不能被继承。()8. 接口无构造器,不能有实例,也不能定义常量。()9. 一个具体类实现接口时,必须要实现接口中的所有方法。()10. 类具有封装

2、性,但可以通过类的公共接口访问类中的数据。()11. 子类能继承或覆盖(重写)父类的方法,但不能重载父类的方法。()12. 用final修饰的方法不能被子类覆盖(重写)。()13. abstract是抽象修饰符,可以用来修饰类、属性和方法。()14. 父类的静态方法不能被子类覆盖为非静态的方法,反之亦然。()15. 子类实例化时,子类的构造方法一定会先调用父类的构造方法。()16. 用final修饰的方法不能被覆盖(重写),也不能有重载的方法。()17. 接口也可以继承接口,且可以继承多个接口,体现了多重继承性。()18. 假设类B继承类A,类C继承类B,则在类C中可用super访问类A的方法

3、。()19. 类和接口都可以继承另外一个类。()20. 抽象类中不能包含final修饰的方法。()二、选择题1. Java实现动态多态性是通过()实现的。 A. 重载 B. 覆盖 C. 接口 D. 抽象类2. 下列哪一种描述是正确的?() A. 动态多态性只针对静态成员方法 B. 动态多态性只针对非静态成员方法 C. 动态多态性只针对静态成员域 D. 动态多态性只针对非静态成员域3. 下列关于重载方法哪一个是正确的描述?() A. 重载方法的参数形式(类型、参数个数或参数顺序)必须不同 B. 重载方法的参数名称必须不同 C. 重载方法的返回值类型必须不同 D. 重载方法的修饰词必须不同4. 接

4、口的所有成员方法都具有()修饰的特性。 A. private,final B. public,abstract C. static,protected D. static 5. Java的封装性是通过()实现的。 A. 访问权限控制 B. 设计内部类 C. 静态域和静态方法 D. 包6. 下列说法哪个是正确的?() A. 子类不能定义和父类同名同参数的方法请预览后下载! B. 子类只能继承父类的方法,而不能重载 C. 重载就是一个类中有多个同名但有不同形参(类型、参数个数或参数顺序)和方法体的方法 D. 子类只能覆盖父类的方法,而不能重载7. 对于下列代码: public class Pare

5、nt public int addValue (int a,int b) int s; s=a+b; return s; class Child extends Parent 下列哪个方法不可以加入类Child?()A. public int addValue(int a, int b, int c) return a+b+c;B. int addValue(int a, int b) return a+b;C. public int addValue(int a) return a+1;D. public int addValue(int a, int b) return a+b+1;8.

6、对于下列代码:1. class Person2. public void printValue(int i, int j) /.3. public void printValue(int i) /.4. 5. public classTeacher extends Person6. public void printValue() /. 7. public void printValue(int i) /.8. public static void main(String args)9. Peson t=new Teacher();10. t. printValue(10);11. 12. 第

7、10行语句将调用哪行语句?() A. 第2行 B. 第3行 C. 第6行 D. 第7行9. 以下程序段输出结果的是()。 public class A implements B public static void main(String args) int=i; A c1=new A(); i=c1.k; System.out.println(“i=”+i); interface B int k=10;请预览后下载! A. i=0 B. i=10 C. 程序有编译错误 D. i=true10. 阅读下面的程序,输出结果是()? public class TestDemo int m=5; p

8、ublic void some(int x) m=x; public static void main(String args) new Demo().some(7);class Demo extends TestDemo int m=8; public void some(int x) super.some(x); System.out.println(m); A. 5 B. 8 C. 7 D. 编译错误11. 下述哪个方法不可以加入类 Subclass ?()class SupClasspublic void methodOne (int i ) public void methodTwo

9、 (int i ) public static void methodThree (int i ) public static void methodForth (int i ) class SubClass extends supClass.A. public static void methodOne (int i ) B. public void methodTwo (int i ) C . public static void methodThree (int i , int j ) D. public static void methodForth(int i) 12. 关于下面的程

10、序,说法正确的是()。 class Baseint m;public Base(int m) this.m=m+1; public class Test extends Base public Test() m=m+1; 请预览后下载! public static void main(String args) Test t=new Test(); System.out.print(t. m);A. 输出结果为0 B. 输出结果为1 C. 输出结果为2 D. 编译出错13. 关于下面的程序,编译和运行后输出结果是()class Baseint m=0;public int getM()retur

11、n m; public class Test extends Baseint m=1;public int getM()return m;public static void main(String args)Test t=new Test();System. out. print(t.m);System. out. print(t.getM() ); A. 00 B. 01 C. 10 D. 1114. 设有下面的两个类定义:class Avoid Show()System.out. println(“我喜欢Java!”); class B extends A void Show()Syst

12、em.out. println(“我喜欢C+!”); 则顺序执行如下语句后输出结果为()。A a=new A();B b=new B();a.show();b.show();A. 我喜欢Java! B. 我喜欢C+! 我喜欢C+! 我喜欢Java!C. 我喜欢Java! D. 我喜欢C+!请预览后下载! 我喜欢Java! 我喜欢C+!15. 现有两个类A和B,以下描述中表示B继承A的是()。 A. class A extends B B. class B implements A C. class A implements D. class B extends A16. 定义类B和类C如下,并

13、将其保存为B. java文件,得到的结果是()。 class Bint b; B(int i)b=i;class C extends Bdouble c=7. 8; A. 代码能够成功编译运行 B. 代码无法编译因为类B不是一个应用程序或小程序 C. 代码无法编译,因为类C没有定义一个带参数的构造方法 D. 代码无法编译,因为类B没有定义一个不带参数的构造方法17. 类Teacher和Student都是类Person的子类,t、s、p分别是上述三个类的非空引用变量,关于以下语句说法正确的()。if(t instanceof Person)s=(Student) t; A. 将构造一个Stude

14、nt对象 B. 表达式合法 C. 编译时非法 D. 编译时合法而在运行时可能非法18. 在/ point x处的哪个声明是合法的?() class Person privaate int a; public int change(int m) return m; public class Teacher extends Person public int b; public static void main(String args) Person p=new Person(); Teacher t=new Teacher(); int i; /point x A. i=m; B. i=b; C

15、. i=p.a D. i=p.change(30);19. 下面关于继承的叙述哪些是正确的?()A. 在Java中的类只允许继承一个类B. 在Java中一个类允许继承多个类请预览后下载!C. 在Java中一个类不能同时继承一个类和实现一个接口D. 在Java中接口可以继承一个或多个接口20. 下列哪些方法与方法public void add(int a) 构成重载方法?()A. public int add(int a) B. public long add(long a)C. public void add(int a, int b) D. public voidadd(float a)21

16、. 在Java语言中,类Cat是类Animal的子类,Cat的构造方法中有一句super(),该语句表达了什么含义?()A. 调用类Cat中定义的super()方法 B. 调用类Animal中定义的super()方法C. 调用类Animal的构造方法 D. 语法错误22. 定义一个类名为Myclass. java的类,并且该类可被一个工程中的所有类访问,那么该类的正确声明应为()。A. private class MyClass extends ObjectB. class Myclass extends ObjectC. public class MyClass extends Object

17、D. Protected class MyClass extends Object23. 关于下面的程序,以下哪个结论是正确的?()1. public class Test2. public Test()3. System.out.print(“3”);4. 5. public void Test()6. System.out.print(“2”);7. 8. public static void main(String args)9. Test t=new Test();10. t.Test();11. System.out.print(“1”);12. 13. A. 程序可以通过编译并正常

18、运行,输出结果为21 B. 程序可以通过编译并正常运行,输出结果为31 C. 程序可以通过编译并正常运行,输出结果为321 D. 程序无法通过编译24. 给定如下Java程序: class A public A() System.out.print(“A”); class B extends A public B() System.out.print(“B”)请预览后下载! public static void main(String args) B b=new B(); 上述程序将()。A. 不能通过编译 B. 通过编译,执行后输出为AB C. 通过编译,执行后输出为BD. 通过编译,执行后

19、输出为A25. 已知MyInterface是一个接口,ClassA是实现了MyInterface的一个类,ClassB是ClassA 的子类,则下列哪个语句是正确的?()A. ClassB obj=new ClassA();B. Myinterface obj=new ClassB();C. ClassA obj=new MyInterface();D. MyInterface obj=new MyInterface();26. A派生出子类B,B派生出子类C,并且在Java源代码中有如下声明: A a0=new A(); A a1=new B(); A a2=new C();以下哪个说法是正

20、确的?()A. 只有第1行能通过编译B. 第1行和第2行能通过编译,但第3行编译出错C. 第13行能通过编译,但第2行和第3行运行时出错D. 第1行、第2行和第3行的声明都是正确27. 考虑以下代码: class C1 interface C2 class C3 extends C1 implements C2 class C4 则下列哪个语句是正确的?()A. C4 c41=new C4();B. C4 c42=new C4();C. C4 c43=new C4();D. C4 c44=new C4();28. 对于以下类: class A class B extends A class C

21、 extends A public class Test public static void main(String args) A x=new A(); B y=new B(); C z=new C();请预览后下载! /此处插入一条语句 下面哪个语句可以放到插入行?()A. x=y; B. z=x; C . z=(c)y; D. y=(a)y;29. 设有一个类的代码如下: class Outer public class Inner1 public static class Inner2 则在另一个类的代码中,下列哪个语句是正确的?()A. Outer.Inner1 obj=new O

22、uter.Inner1();B. Outer.Inner2 obj=new Outer.Inner2();C. Outer.Inner1 obj=new Outer.Inner1() .new Inner1();D. Outer.Inner2 obj=new Outer(). new Inner2();30. 以下关于泛型的说法哪个是错误的?()A. 泛型是通过类型参数来提高代码复用性的一种技术B. 通过在类名后增加类型参数可以定义具有泛型特点的类C. 通过在接口名后增加类型参数可以定义具有泛型特点的接口D. 一个泛型类只能有一个类型参数2、 程序阅读题1. 仔细阅读下面的程序代码,若经编译和

23、运行后,请写出打印结果。 class Overloadvoid testOverload(int i) System.out.println(“int”); void testOverload(String s) System.out.println(“String”); public static void main(String args) Overload a=new Overload(); char ch=x; a.testOverload(ch);2. 仔细阅读下面的程序代码,请将划线上(1)(5)的语句补充完整。abstract class Person private Strin

24、g name; public Person(String n) name=n;请预览后下载!public(1)String getMajor();public String(2)() return name; class Student(3)Person private(4); public Student(String n, String m) super(n); major=m; public String(5)() return”专业是:”+major; public class TestPerson public static void main(String args) Person

25、 p=new Student(“张三”,”软件工程”); System.out.println(p.getName()+”,”+p.getMajor(); 3. 写出下列程序代码的运行结果。 public class Test int m=1; public void some(int x) m=x;public static void main(String args ) new Demo().some(2); class Demo extends Test int m=3; public void some(int x) super.some(x); System.out.print(m)

26、; System.out.print(super.m); 4. 写出下列程序代码的运行结果。 class A int m=0, n=0; long f()请预览后下载! return m+n; class B extends A int m=1,n=1; long f() long result=0; super.m=10; super.n=30; result=super.f()+(m+n); return result; long g()long result=0; result=super.f(); return result/2; class Examplepublic static

27、void main(String args) B b=new B(); b.m=6; b.n=2;long resultOne=b.g();long resultTwo=b.f();long resultThree=b.g(); System.out.println(“resultOne=”+resultOne); System.out.println(“resultTwo=”+resultTwo); System.out.println(“resultThree=”+resultThree); 5. 下面的程序运行结果是什么? class Tree() class Pine extends

28、Tree ( ) class Oak extends Tree ( ) public class Forest public static void main ( String args ) Tree tree = new Pine ( ) ; if ( tree instanceof Pine) System . out . println ( Pine ) ; if ( tree instanceof Tree) System . out . println ( Tree ) ; if ( tree instanceof Oak) System . out . println ( Oak

29、) ;请预览后下载! else System . out . println ( Oops ) ; 6. 下面的程序运行结果是什么? abstract class Base abstract public void myfunc ( ) ; public void another ( ) System . out . println ( Another method ) ; public class Abs extends Base public static void main ( String args ) Base b=new Abs ( ) ; b . another ( ) ; pu

30、blic void myfunc ( ) System . out . println ( My Func ); public void another () myfunc ();7. 下面的程序运行结果是什么?class Super public int i=0 ; public Super () i=1; public class Sub extends Super public Sub ( ) i=2; public static void main ( String args ) Sub s = new Sub ( ) ; System . out . println ( s . i)

31、 ;8. 下面的程序运行结果是什么?class Person请预览后下载!public Person ( ) System . out . println ( hi ! ) ;public Person ( String s ) this ( ) ;System . out . println ( I am +s ); public class Who extends Person public Who ( ) this ( I am Tony ) ; public Who ( String s ) super ( s ) ; System .out .println ( How do you

32、 do? ) ; public static void main ( String args) Who w=new Who ( Tom ) ; 9. 阅读下面的程序,修改程序中错误的地方(提示:共三处错误)。1 . interface Shape2 . double PI();3. double area ();4. 5 . class Cycle extends Shape6. private double r;7. public Cycle ( double r ) 8. this .r = r;9. 10. double area ( )11. return PI* r *r ;12.

33、13. 14 . public class Test 15 . public static void main ( String args )16. Cycle c = new Cycle ( 1 . 5 ) ;17. System . out . println ( 面积为 : + c. area ( ) ) ;18. 19. 10.仔细阅读下面的程序代码,若经编译和运行后,请写出打印结果。请预览后下载! class GenericsFoo private T x ; public GenericsFoo ( T x ) this .x=x; public T getX( ) return

34、x; public void setX( T x ) this .x =x; public class GeneriesFooDemo public static void main ( String args ) GenericsFoo strFoo=new GenericsFoo ( He1lo ! ) ; GenericsFoo douFoo = new GenericsFoo Double ( new Double ( 1 ) ) ;System . out . println ( strFoo. getX + strFoo . getX( ) ) ;System . out . pr

35、intln ( douFoo. getX + douFoo. getX ( ) ) ;11. 阅读下面的程序代码,写出程序运行的输出结果。class ParentClass int x=0; int sum ( int a , int b , int c ) return a + b+c ; int sum ( int a , int b ) return a + b; class childClass extends ParentClass public ChildClass ( ) x=10;int sum ( int a , int b ) return a + b + 1 ; clas

36、s Test public static void main ( String args ) ParentClass p=new ChildClass ( ); System . out . println ( p .sum ( 5 , 5 , 5 ) ) ;System . out . println ( p .sum ( 5 , 5 ) ) ;请预览后下载! System . out . println ( p . x ) ; 12. 仔细阅读下面的程序代码,写出程序运行的输出结果。public class TestSample public static void main ( Stri

37、ng args ) Sub obj1 = new Sub ( );Super obj2=new Sub ( ) ;Super obj3=new Super();System . out . println ( obj1 . method1( ) ) ;System .out . println ( obj2.method1 ( ) );System .out . println ( obj3 .method1 ( ) ); class Super int x=1 , y = 2 ;int method1 ( ) return ( x y ) ? x : y ) ; 13. 仔细阅读下面的程序代

38、码,写出程序运行的输出结果。class Test1 private int i=1 ;public class Test11private int i = 2 ;public void methodI ( int i) i + + ; this . i + + ;Test1 . this . i + + ;System . out . println ( i of methodI ( ) : + i ) ;System . out . println ( i of Test11 : + this . i ) ;System . out . println ( i of Test1 :”+Tes

39、t1.this.i); 请预览后下载!Test11 ic = new Test11( ) ;public void increaseI ( int k ) ic . methodI( k ) ; public static void main ( String args ) Test1 oc=new Test1( );oc .increaseI( 20 ) ;14. 阅读下面的程序代码,判断2635行(带划线部分)各语句编译是否通过,如果编译通过,直接写出该行的打印结果。1 . class Parentclass 2. int x;3. int sum ( int a , int b ) ;4 . return a+b ;5. 6. int sub ( int a , int b )7. return a-b;8. 9. 10. class Childclass extends ParentClass11. int x=1; 12. int y = 2;13. int sum ( int a , int b ) 14. ret

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1