1)Analyze the following code:

public class Test {
public static void main(String[ ] args) {
B b = new B();
b.m(5);
System.out.println("i is " + b.i);
}
}
class A {
int i;
public void m(int i) {
this.i = i;
}
}
class B extends A {
public void m(String s) {
}
}

A)The program has a compilation error, because m is overridden with a different signature in B.
B)The method m is not overridden in B. B inherits the method m from A and defines an overloaded method m in B.  
C)The program has a runtime error on b.i, because i is not accessible from b.
D)The program has a compilation error, because b.m(5) cannot be invoked since the method m(int) is hidden in B.

B中没有重写方法m。B继承了A中的方法m,并在B中定义了一个重载的方法m。

2)Analyze the following code.

// Program 1
public class Test {
public static void main(String[ ] args) {
Object a1 = new A();
Object a2 = new A();
System.out.println(((A)a1).equals((A)a2));
}
}
class A {
int x;
public boolean equals(A a) {
return this.x == a.x;
}
}
// Program 2
public class Test {
public static void main(String[ ] args) {
A a1 = new A();
A a2 = new A();
System.out.println(a1.equals(a2));
}
}
class A {
int x;
public boolean equals(A a) {
return this.x == a.x;
}
}

A)Program 1 displays true and Program 2 displays true
B)Program 1 displays false and Program 2 displays true
C)Program 1 displays false and Program 2 displays false
D)Program 1 displays true and Program 2 displays false

3)Invoking ________ removes all elements in an ArrayList x. 3) _______
A)x.clear() B)x.delete() C)x.remove() D)x.empty() E)x.clean()

4)Analyze the following code:
Cylinder cy = new Cylinder(1, 1);
Circle c = cy; 4) _______
A)The code has a runtime error. B)The code has a compile error. C)The code is fine.

5)Which of the following statements are true? (Choose all that apply.) 5) _______
A)Overloading a method is to provide more than one method with the same name but with different signatures to distinguish them.  
B)A private method cannot be overridden. If a method defined in a subclass is private in its superclass, the two methods are completely unrelated.
C)It is a compilation error if two methods differ only in return type in the same class.
D)A static method cannot be overridden. If a static method defined in the superclass is redefined in a subclass, the method defined in the superclass is hidden.  
E)To override a method, the method must be defined in the subclass using the same signature and compatible return type as in its superclass.  

1、重载一个方法是提供多个具有相同名称但具有不同签名的方法来区分它们。
2、不能覆盖私有方法,因为私有方法在他的类本身以外是不能被访问的。如果在子类中定义的方法在父类中是私有的,那么这两个方法是完全不相关的。
3、如果两个方法在同一个类中仅在返回类型上不同,则为编译错误。
4、静态方法不能覆盖。如果父类中定义的静态方法在子类中被重新定义,那么定义在父类中的静态方法将被隐藏

5、要重写方法,必须在子类中使用与其超类相同的签名和兼容的返回类型来定义方法。

6)Encapsulation means ________. 6) _______
A)that a variable of supertype can refer to a subtype object
B)that a class can contain another class
C)that a class can extend another class
D)that data fields should be declared private

封装

7)Analyze the following code: (Choose all that apply.)

public class Test extends A {
public static void main(String[ ] args) {
Test t = new Test();
t.print();
}
}
class A {
String s;
A(String s) {
this.s = s;
}
public void print() {
System.out.println(s);
}
}

A)The program does not compile because Test does not have a default constructor Test().
B)The program would compile if a default constructor A(){ } is added to class A explicitly.
C)The program compiles, but it has a runtime error due to the conflict on the method name print.
D)The program has an implicit default constructor Test(), but it cannot be compiled, because its super class does not have a default constructor. The program would compile if the constructor in the class A were removed.

如果父类是一个无缺省参数的构造函数,那么对于派生类一旦没有构造函数,那么就不会自动的先构造父类的构造函数,这是不允许的。

解决方案:

1、给父类增加一个含有缺省的构造函数

2、删除掉父类的无缺省参数的构造函数,使用隐式的缺省构造函数

8)Suppose you create a class Cylinder to be a subclass of Circle. Analyze the following code:

class Cylinder extends Circle {
double length; Cylinder(double radius) {
Circle(radius);
}
}

A)The program compiles fine, but it has a runtime error because of invoking the Circle class's constructor illegally.
B)The program compiles fine, but you cannot create an instance of Cylinder because the constructor does not specify the length of the cylinder.
C)The program has a compile error because you attempted to invoke the Circle class's constructor illegally.

9)What is the output of the following code:

public class Test {
public static void main(String[ ] args) {
Object o1 = new Object();
Object o2 = new Object();
System.out.print((o1 == o2) + " " + (o1.equals(o2)));
}
}

A)true true B) false true C)true false D) false false

o1和o2值不相同,引用也不相同

10)Invoking ________ returns the first element in an ArrayList x. 10) ______
A)x.get() B) x.get(1) C) x.first() D) x.get(0)

11)Analyze the following code:

public class Test {
public static void main(String[ ] args) {
String s = new String("Welcome to Java");
Object o = s;
String d = (String)o;
}
}

A)When casting o to s in String d = (String)o, a new object is created.
B)When casting o to s in String d = (String)o, the contents of o is changed.
C)s, o, and d reference the same String object.
D)When assigning s to o in Object o = s, a new object is created.

s、o和d引用相同的字符串对象。

12)Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following method will cause runtime errors? (Choose all that apply.) 12) ______
A)x.get(2) B)x.set(2, "New York"); C)x.remove(2) D)x.size() E)x.get(1)

两个String元素,下标范围0,1。

13)Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following method will cause the list to become [Beijing]? (Choose all that apply.) 13) ______
A)x.remove(1) B) x.remove(0) C)x.remove("Singapore") D) x.remove(2)

14)Given the following code, find the compile error. (Choose all that apply.)

public class Test {
public static void main(String[ ] args) {
m(new GraduateStudent());
m(new Student());
m(new Person());
m(new Object());
}
public static void m(Student x) {
System.out.println(x.toString());
}
}
class GraduateStudent extends Student {
}
class Student extends Person {
public String toString() {
return "Student";
}
}
class Person extends Object {
public String toString() {
return "Person";
}
}

A)m(new GraduateStudent()) causes an error
B)m(new Person()) causes an error
C)m(new Object()) causes an error
D)m(new Student()) causes an error

m(new Person()) 、m(new Object())

相当于

Student x = new Person();

Student x = new Object();

是非法的,因为Person和Object的实例不是Student的实例

15)Which of the following classes cannot be extended? 15) ______
A)final class A { } B) class A { private A();} C)class A { protected A();} D) class A {  }

16)Analyze the following code: (Choose all that apply.)

public class Test {
public static void main(String[ ] args) {
Object a1 = new A();
Object a2 = new Object();
System.out.println(a1);
System.out.println(a2);
}
}
class A {
int x;
public String toString() {
return "A's x is " + x;
}
}

A)When executing System.out.println(a2), the toString() method in the Object class is invoked.
B)When executing System.out.println(a1), the toString() method in the Object class is invoked.
C)When executing System.out.println(a1), the toString() method in the A class is invoked.
D)The program cannot be compiled, because System.out.println(a1) is wrong and it should be replaced by System.out.println(a1.toString());

Object是A的父类,A类重写了toString()方法

17)Inheritance means ________. 17) ______
A)that a variable of supertype can refer to a subtype object
B)that a class can extend another class
C)that a class can contain another class
D)that data fields should be declared private

18)Composition means ________. 18) ______
A)that a class can extend another class
B)that a variable of supertype can refer to a subtype object
C)that data fields should be declared private
D)that a class can contain another class

19)Which of the following statements are true? (Choose all that apply.) 19) ______
A)Dynamic binding can apply to instance methods.
B)Dynamic binding can apply to static methods.
C)The compiler finds a matching method according to parameter type, number of parameters, and order of the parameters at compilation time.  
D)A method may be implemented in several subclasses. The Java Virtual Machine dynamically binds the implementation of the method at runtime.
E)You can always pass an instance of a subclass to a parameter of its superclass type. This feature is known as polymorphism.

1、动态绑定可以应用于实例方法。
2、动态绑定不能应用于静态方法。
3、编译器根据参数类型、参数数量和编译时参数的顺序找到匹配方法。
4、一个方法可以在几个子类中实现。Java虚拟机在运行时动态绑定方法的实现。

5、总是可以将子类的实例传递给父类类型的参数。这个特性称为多态性。

20)Which of the following statements are true? (Choose all that apply.) 20) ______
A)If a method overrides another method, these two methods must have the same signature.
B)A method can be overridden in the same class.
C)If a method overloads another method, these two methods must have the same signature.
D)A method can be overloaded in the same class.

覆盖必然有相同的函数签名,重载的函数签名必然不同

一个类中可以有被重载的方法,但不能出现覆盖,覆盖关系必然是出现在继承的父子类中。

21)Analyze the following code: (Choose all that apply.)

ArrayList list = new ArrayList();
list.add("Beijing");
list.add("Tokyo");
list.add("Shanghai");
list.set(3, "Hong Kong");

A)If you replace the last line by list.add(4, "Hong Kong"), the code will compile and run fine.
B)The last line in the code causes a runtime error because there is no element at index 3 in the array list.
C)The last line in the code has a compile error because there is no element at index 3 in the array list.
D)If you replace the last line by list.add(3, "Hong Kong"), the code will compile and run fine.

22)Analyze the following code.

// Program 1:
public class Test {
public static void main(String[ ] args) {
Object a1 = new A();
Object a2 = new A();
System.out.println(a1.equals(a2));
}
}
class A {
int x;
public boolean equals(A a) {
return this.x == a.x;
}
}
// Program 2:
public class Test {
public static void main(String[ ] args) {
A a1 = new A();
A a2 = new A();
System.out.println(a1.equals(a2));
}
}
class A {
int x;
public boolean equals(A a) {
return this.x == a.x;
}
}

A)Program 1 displays true and Program 2 displays true
B)Program 1 displays true and Program 2 displays false
C)Program 1 displays false and Program 2 displays true
D)Program 1 displays false and Program 2 displays false

23)Which of the following are Java keywords? 23) ______
A)instanceOf B) cast
C)casting D) instanceof

24)Analyze the following code.

// Program 1:
public class Test {
public static void main(String[ ] args) {
Object a1 = new A();
Object a2 = new A();
System.out.println(a1.equals(a2));
}
}
class A {
int x;
public boolean equals(Object a) {
return this.x == ((A)a)x;
}
}
// Program 2:
public class Test {
public static void main(String[ ] args) {
Object a1 = new A();
Object a2 = new A();
System.out.println(a1.equals(a2));
}
}
class A {
int x;
public boolean equals(A a) {
return this.x == a.x;
}
}

A)Program 1 displays false and Program 2 displays false
B)Program 1 displays false and Program 2 displays true
C)Program 1 displays true and Program 2 displays false
D)Program 1 displays true and Program 2 displays true

25)Which of the statements regarding the super keyword is incorrect? 25) ______
A)You cannot invoke a method in superclass's parent class.
B)You can use super to invoke a super class method.
C)You can use super.super.p to invoke a method in superclass's parent class.
D)You can use super to invoke a super class constructor.

26)Given the following code:

class C1 {}
class C2 extends C1 { }
class C3 extends C2 { }
class C4 extends C1 {}
C1 c1 = new C1();
C2 c2 = new C2();
C3 c3 = new C3();
C4 c4 = new C4();

Which of the following expressions evaluates to false?
A)c4 instanceof C2 B) c2 instanceof C1 C)c1 instanceof C1 D) c3 instanceof C1

A instanceof B 用来检测A是否为B的一个实例

27)A class design requires that a particular member variable must be accessible by any subclasses of this class, but otherwise not by classes which are not members of the same package. What should be done to achieve this? 27) ______
A)The variable should be marked private and an accessor method provided.
B)The variable should have no special access modifier.  
C)The variable should be marked protected.
D)The variable should be marked public.
E)The variable should be marked private.

类设计要求特定的成员变量必须能被该类的任何子类访问,否则不能被不是同一包成员的类访问。这要求类的数据成员被标记为protected

28)Polymorphism means ________. 28) ______
A)that a variable of supertype can refer to a subtype object
B)that a class can extend another class
C)that data fields should be declared private
D)that a class can contain another class

父类变量可以引用子类类型对象

29)Analyze the following code: (Choose all that apply.)

import java.util.StringTokenizer;
public class A extends StringTokenizer {
}

A)The program would compile fine if you add the following constructor into A: A(String s) { super(s); }
B)The program has a compilation error because the default constructor of A invokes the default constructor of StringTokenizer, but StringTokenizer does not have a default constructor.
C)The program has a compilation error because A does not have a default constructor.
D)The program would compile fine if you add the following constructor into A: A(String s) { }

30)Analyze the following code:

Circle c = new Circle (5);
Cylinder c = cy;

30) ______
A)The code has a runtime error. B)The code has a compile error. C)The code is fine.

31)Given the following classes and their objects:

class C1 {};
class C2 extends C1 {};
class C3 extends C1 {};
C2 c2 = new C2();
C3 c3 = new C3();

Analyze the following statement:
c2 = (C2)((C1)c3);
A)You will get a runtime error because you cannot cast objects from sibling classes.
B)You will get a runtime error because the Java runtime system cannot perform multiple casting in nested form.
C)c3 is cast into c2 successfully.
D)The statement is correct.

运行时错误,不能从兄弟类型转换对象

32)Which of the following statements are true? (Choose all that apply.) 32) ______
A)"class A extends B" means A is a subclass of B.
B)A subclass is usually extended to contain more functions and more detailed information than its superclass.
C)"class A extends B" means B is a subclass of A.
D)A subclass is a subset of a superclass.

33)The getValue() method is overridden in two ways. Which one is correct?

I:
public class Test {
public static void main(String[ ] args) {
A a = new A();
System.out.println(a.getValue());
}
}
class B {
public String getValue() {
return "Any object";
}
}
class A extends B {
public Object getValue() {
return "A string";
}
}
II:
public class Test {
public static void main(String[ ] args) {
A a = new A();
System.out.println(a.getValue());
}
}
class B {
public Object getValue() {
return "Any object";
}
}
class A extends B {
public String getValue() {
return "A string";
}
}

A)I B) II C)Both I and II D) Neither

重写的方法必须与被重写的方法具有一样的签名,以及一样或者兼容的返回类型。兼容的含义是重写方法的返回类型可以是被重写方法的返回类型的子类型。

34)What modifier should you use on the members of a class so that they are not accessible to another class in a different package, but are accessible to any subclasses in any package?
A)public B) protected C)private D) Use the default modifier.

protected允许位于任何包中的子类或者同一包中的类访问该类的成员。

35)You can create an ArrayList using ________. 35) ______
A)ArrayList() B) new ArrayList[100] C)new ArrayList() D) new ArrayList[ ]

ArrayList用来存储不限定个数的对象,初始化时没有个数参数。

36)The equals method is defined in the Object class. Which of the following is correct to override it in the String class? 36) ______
A)public boolean equals(String other)
B)public static boolean equals(Object other)
C)public static boolean equals(String other)
D)public boolean equals(Object other)

37)You can assign ________ to a variable of Object[ ] type. (Choose all that apply.) 37) ______
A)new double[100]
B)new int[100]  
C)new char[100]
D)new java.util.Date[100]
E)new String[100]

double、int和char等基本数据类型不是对象,不能被new创建

38)Invoking ________ returns the number of the elements in an ArrayList x. 38) ______
A)x.getSize() B) x.getLength(0) C)x.size() D) x.length(1)

39)Which of the following statements is false? 39) ______
A)A public class can be accessed by a class from a different package.
B)A protected method can be accessed by a subclass in a different package.
C)A private method cannot be accessed by a class in a different package.  
D)A method with no visibility modifier can be accessed by a class in a different package.

40)What is the output of the following code:

public class Test {
public static void main(String[ ] args) {
String s1 = new String("Java");
String s2 = new String("Java");
System.out.print((s1 == s2) + " " + (s1.equals(s2)));
}
}

A)true false B) false true C)true true D) false false

41)The visibility of these modifiers increases in this order: 41) ______
A)none (if no modifier is used), protected, private, and public.
B)none (if no modifier is used), private, protected, and public.  
C)private, protected, none (if no modifier is used), and public.  
D)private, none (if no modifier is used), protected, and public.  

修饰符的可见性递增排序

42)What is the output of running class C?

class A {
public A() {
System.out.println(
"The default constructor of A is invoked");
}
}
class B extends A {
public B() {
System.out.println(
"The default constructor of B is invoked");
}
}
public class C {
public static void main(String[ ] args) {
B b = new B();
}
}

A)"The default constructor of B is invoked"
B)"The default constructor of B is invoked""The default constructor of A is invoked"
C)"The default constructor of A is invoked"
D)"The default constructor of A is invoked""The default constructor of B is invoked"
E)Nothing displayed

先创建父类对象再创建子类对象

43)Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following methods will cause the list to become [Beijing, Chicago, Singapore]? 43) ______
A)x.add(2, "Chicago") B) x.add(0, "Chicago")
C)x.add("Chicago") D) x.add(1, "Chicago")

44)Object-oriented programming allows you to derive new classes from existing classes. This is called ________. 44) ______
A)abstraction B) encapsulation C)inheritance D) generalization

面向对象编程允许从现有类中派生新类,这种方式称为继承。

45)What modifier should you use on a class so that a class in the same package can access it but a class in a different package cannot access it? 45) ______
A)public B) protected C)private D) Use the default modifier.

Java题库——Chapter11 继承和多态的更多相关文章

  1. Java的接口、继承与多态

    接口 java只支持单继承,即一个类只能有一个父类,因此需要接口来实现多重继承. 接口的定义 类和接口的区别:一个类通过继承接口的方式,从而来继承接口的抽象方法.类描述对象的属性和方法,接口则包含类要 ...

  2. [java核心技术01]__继承与多态、重载与重写、抽象类与接口

    前言 前面简单学习了面向对象的知识,知道了其两个重要的特性,继承与多态,今天就围绕着面向对象的这两个特性,将继承与多态及相关的几个几个定义重载与重写,抽象类与接口的相关知识具体学习一下. 类的继承 关 ...

  3. Java之封装,继承,多态

    一,前言 ​ 今天总结一下关于Java的三大特性,封装,继承,多态.其实关于三大特性对于从事编程人员来说都是基本的了,毕竟只要接触Java这些都是先要认识的,接下来就系统总结一下. 二,封装 ​ 先来 ...

  4. java中封装,继承,多态,接口学习总结

    ### 一:封装java中封装是指一种将抽象性函式接口的实现细节部分包装.隐藏起来的方法. 封装可以被认为是一个保护屏障,防止该类的代码和数据被外部类定义的代码随机访问.要访问该类的代码和数据,必须通 ...

  5. 编写Java程序,以继承和多态思想模拟饲养员喂养不同动物的不同行为

    返回本章节 返回作业目录 需求说明: 以继承和多态思想模拟饲养员喂养不同动物的不同行为 动物园有饲养员和动物,其中动物有老虎.马.猴子.羊.狼等. 饲养员对不同的动物有不同的喂养行为. 实现思路: 以 ...

  6. Java中封装、继承和多态

    封装: 封装实际上使用方法将类的数据隐藏起来,控制用户对类的修改和访问数据的程度. 适当的封装可以让程式码更容易理解和维护,也加强了程式码的安全性. 访问修饰符有public,private,prot ...

  7. Java学习--封装、继承、多态

    接下来几天会根据http://www.cnblogs.com/chenssy/category/525010.html中讲解的java内容做个学习笔记,在此感谢一下这位大仙!! 一.封装 对于封装而言 ...

  8. Java基础再复习(继承、多态、方法内部类**、HashMap用法**、参数传递**)

    ###继承: package com.shiyan; public class Animal { public int legNum; //动物四肢的数量 //类方法 public void bark ...

  9. Java求职实战之继承和多态

    1.final修饰变量时,是引用不能变,还是引用的对象不能变? 是指引用变量不能变,引用对象的内容可以变. 2.==和equals有什么区别? 网上搜索一下,发现很多人解释的都比较清楚了.自己简单概括 ...

随机推荐

  1. ClassNotFoundException------IDEA下的一种原因

    由于直接复制文件而未经过IDE造成次异常,需要修改程序入口:

  2. Spring Cloud第十一篇 | 分布式配置中心高可用

    ​ 本文是Spring Cloud专栏的第十一篇文章,了解前十篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Cl ...

  3. .Net Core2.2升级到3.1小记

    .NET Core 3.1 作为LTS长期支持版本,会提供3年的支持(明年就出.net5),值得升级(吗). 目前主流的第三方包大多都已经提供了支持,2.x => 3.1还是变化不是特别多,EF ...

  4. Things 3 for Mac是什么?如何使用?

    为大家介绍一款实用的效率管理软件“Things 3 for Mac”,它通过使用标签和智能过滤条,东西结合了强大的功能和简单性,Leopard风格的来源列表可以快速轻松地进行对焦.与一个美丽的用户界面 ...

  5. VS2019 开发Django(三)------连接MySQL

    导航:VS2019开发Django系列 下班回到家,洗漱完毕,夜已深.关于Django这个系列的博文,我心中的想法就是承接之前的微信小程序的内容,做一个服务端的管理中心,上新菜单,调整价格啊!之类的, ...

  6. weblogic的linux静默搭建

    前言: Weblogic与Tomcat都是java应用的容器,而这两者有什么大的不同呢?Tomcat是Apache基金会提供的Servlet容器,它支持JSP, Servlet和JDBC等J2EE关键 ...

  7. js实现弹出框跟随鼠标移动

    又是新的一天网上冲浪,在bing的搜索页面下看到这样一个效果: 即弹出框随着鼠标的移动而移动.思路大概为: 调用onmousemove函数,将鼠标的当前位置赋予弹出框即可 //html <div ...

  8. MySQL数据库基础笔记

    数据库 数据库就是存储和管理数据的仓库,用户可以对数据库中的数据进行增删改查等操作. 数据库的分类 关系型数据库(Oracle.MySQL.SQLite等) 非关系型数据库(Redis.MongoDB ...

  9. Maven发布封装到中央仓库时候报错:no default secret key: No secret key

    今天因为发布swagger-spring-boot-starter做一个问题的修复,然后碰到了下面这个问题,记录一下解决过程,帮助后续碰到类似问题的童鞋: *gpg: WARNING: "- ...

  10. Eureka+SpringBoot2.X结合Security注册中心安全验证

    当我们直接配置完SpringCloudEureka的时候,任何人直接就可以访问,这样是极不安全的!!外网的情况下是绝对不允许的! 好在有提供了解决方案,下面拿为我们提供的Security解决 Spri ...