Java题库——Chapter11 继承和多态
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 继承和多态的更多相关文章
- Java的接口、继承与多态
接口 java只支持单继承,即一个类只能有一个父类,因此需要接口来实现多重继承. 接口的定义 类和接口的区别:一个类通过继承接口的方式,从而来继承接口的抽象方法.类描述对象的属性和方法,接口则包含类要 ...
- [java核心技术01]__继承与多态、重载与重写、抽象类与接口
前言 前面简单学习了面向对象的知识,知道了其两个重要的特性,继承与多态,今天就围绕着面向对象的这两个特性,将继承与多态及相关的几个几个定义重载与重写,抽象类与接口的相关知识具体学习一下. 类的继承 关 ...
- Java之封装,继承,多态
一,前言 今天总结一下关于Java的三大特性,封装,继承,多态.其实关于三大特性对于从事编程人员来说都是基本的了,毕竟只要接触Java这些都是先要认识的,接下来就系统总结一下. 二,封装 先来 ...
- java中封装,继承,多态,接口学习总结
### 一:封装java中封装是指一种将抽象性函式接口的实现细节部分包装.隐藏起来的方法. 封装可以被认为是一个保护屏障,防止该类的代码和数据被外部类定义的代码随机访问.要访问该类的代码和数据,必须通 ...
- 编写Java程序,以继承和多态思想模拟饲养员喂养不同动物的不同行为
返回本章节 返回作业目录 需求说明: 以继承和多态思想模拟饲养员喂养不同动物的不同行为 动物园有饲养员和动物,其中动物有老虎.马.猴子.羊.狼等. 饲养员对不同的动物有不同的喂养行为. 实现思路: 以 ...
- Java中封装、继承和多态
封装: 封装实际上使用方法将类的数据隐藏起来,控制用户对类的修改和访问数据的程度. 适当的封装可以让程式码更容易理解和维护,也加强了程式码的安全性. 访问修饰符有public,private,prot ...
- Java学习--封装、继承、多态
接下来几天会根据http://www.cnblogs.com/chenssy/category/525010.html中讲解的java内容做个学习笔记,在此感谢一下这位大仙!! 一.封装 对于封装而言 ...
- Java基础再复习(继承、多态、方法内部类**、HashMap用法**、参数传递**)
###继承: package com.shiyan; public class Animal { public int legNum; //动物四肢的数量 //类方法 public void bark ...
- Java求职实战之继承和多态
1.final修饰变量时,是引用不能变,还是引用的对象不能变? 是指引用变量不能变,引用对象的内容可以变. 2.==和equals有什么区别? 网上搜索一下,发现很多人解释的都比较清楚了.自己简单概括 ...
随机推荐
- SQL- SQL查询检索阶段一
一 说明 如果是初学者,建议去网上寻找安装Mysql的文章安装,以及使用navicat连接数据库,以后的示例基本是使用mysql数据库管理系统: 二 准备前提 需要建立一张学生表,列分别是id,名称, ...
- 《Java练习题》习题集三
编程合集: https://www.cnblogs.com/jssj/p/12002760.html Java总结:https://www.cnblogs.com/jssj/p/11146205.ht ...
- 十分钟学会Markdown基本语法
文章目录 Markdown 语法 一.标题 这是一级标题 这是二级标题 这是三级标题 这是四级标题 这是五级标题 这是六级标题 二.字体 三.引用 四.分割线 五.图片 六.超链接 七.列表 八.表格 ...
- Task.Factory.StartNew 测试
到底该用多少线程?线程数.CPU核心数.本地计算时间.等待时间的关系 线程数 = CPU核心数 * ( 本地计算时间 + 等待时间 ) / 本地计算时间 下面是Task.Factory.StartNe ...
- c++-面向对象:类和对象
类和对象 #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string.h> using names ...
- injected stylesheet 谷歌扩展插件,造成样式异常
今天在开发的时候,遇到一个问题,就是我们我在写发送广告的功能,然后我用了一个textare文本框,这个时候,发现了一个问题.这个文本框凭空消失了.不见了,我以为是自己的那个样式不小心把这个隐藏掉了后来 ...
- Android 进度对话框 ProgressDialog
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentV ...
- docker快速部署本地项目到服务器(tomcat8+mysql8)
目标是:将本地运行的spring项目,部署到服务器上 为什么使用docker? 环境隔离 服务器上,各种环境交杂,使用docker,能清楚的把各个项目进行隔离,不单维护的人员方便,也会省去很多维护这些 ...
- Redis—数据备份与恢复
https://www.cnblogs.com/shizhengwen/p/9283973.html https://blog.csdn.net/w2393040183/article/details ...
- MYSQL 游标学习及使用实例
who?(游标是什么?)游标(cursor)官方定义:是系统为用户开通的一个数据缓冲区,存放sql执行结果.每个游标区都有一个名字,用户可以通过sql语句逐一从游标中获取记录,并赋值给变量,交由主语言 ...