Java题库——Chapter13抽象类和接口
)What is the output of running class Test?
public class Test {
public static void main(String[ ] args) {
new Circle9();
}
}
public abstract class GeometricObject {
protected GeometricObject() {
System.out.print("A");
}
protected GeometricObject(String color, boolean filled) {
System.out.print("B");
}
}
public class Circle9 extends GeometricObject {
/** Default constructor */
public Circle9() {
this(1.0);
System.out.print("C");
}
/** Construct circle with a specified radius */
public Circle9(double radius) {
this(radius, "white", false);
System.out.print("D");
}
/** Construct a circle with specified radius, filled, and color */
public Circle9(double radius, String color, boolean filled) {
super(color, filled);
System.out.print("E");
}
}
A)CBAE B)BACD C)ABCD D)BEDC E)AEDC
调用过程:先初始化一个Circle9对象时,需要调用Circle9的缺省参数的构造函数Circle9() ,而这个缺省参数的构造函数调用Circle9(double radius) ,而Circle9(double radius) 又调用Circle9(double radius, String color, boolean filled) ,而Circle9(double radius, String color, boolean filled)需要调用其父类构造函数GeometricObject(String color, boolean filled)这样层层调用,在调用栈中就是BEDC
2)Which of the following class definitions defines a legal abstract class? 4) _______
A)abstract class A { abstract void unfinished(); }
B)class A { abstract void unfinished() { } }
C)public class abstract A { abstract void unfinished(); }
D)class A { abstract void unfinished(); }
abstract要写在 class或void之前
3)Which of the following statements are correct? (Choose all that apply.) 6) _______
A)Number i = 4.5; B) Double i = 4.5; C)Object i = 4.5; D) Integer i = 4.5;
Number类是数值包装类的抽象父类,Object是所有类的父类
4)The printout from the following code is ________.
java.util.ArrayList list = new java.util.ArrayList();
list.add("New York");
java.util.ArrayList list1 = list;
list.add("Atlanta");
list1.add("Dallas");
System.out.println(list1);
A)[New York, Atlanta, Dallas] B) [New York, Atlanta] C)[New York, Dallas] D) [New York]
注意这里的list1不是new出来的,与list指向相同的内存区域
5)Analyze the following code.
public class Test {
public static void main(String[ ] args) {
Number x = new Integer(3);
System.out.println(x.intValue());
System.out.println((Integer)x.compareTo(new Integer(4)));
}
}
A)The program has a compile error because the member access operator (.) is executed before the casting operator.
B)The program has a compile error because an Integer instance cannot be assigned to a Number variable.
C)The program has a compile error because x cannot be cast into Integer.
D)The program has a compile error because intValue is an abstract method in Number.
E)The program compiles and runs fine.
该题本想现将Number对象x转换为子类,使用子类中才能使用的compareTo方法,但是这里是先使用访问操作符(.)在进行向子类型的转换,所以在没有完成转换的情况下,Number对象x没有compareTo方法
6)Which of the following declares an abstract method in an abstract Java class? 11) ______
A)public abstract method();
B)public void method() {}
C)public abstract void method();
D)public abstract void method() {}
E)public void abstract Method();
7)Which of the following is a correct interface? 12) ______
A)interface A { void print();}
B)abstract interface A { abstract void print() { };}
C)abstract interface A { print(); }
D)interface A { void print() { }; }
接口中的变量是public static final
接口中的方法是public abstract
或者可以省略不写
8)What is the output of Integer.parseInt("10", 2)? 13) ______
A)1; B) 2; C)Invalid statement; D) 10;
parseInt(String s , int radix)有两个参数,一个参数是需要转换的数值字符串,另一个是指定的进制的基数
parseInt("10", 2)就表示将“10”转换为2进制下的数
9)To create an instance of BigInteger for 454, use 14) ______
A)new BigInteger("454"); B) new BigInteger(454); C)BigInteger("454"); D) BigInteger(454);
10)Suppose A is an interface, B is a concrete class with a default constructor that implements A. Which of the following is correct? (Choose all that apply.) 15) ______
A)A a = new A(); B) B b = new A(); C)A a = new B(); D) B b = new B();
可以看成,B是A的一个子类,A是接口,不能够实例化!
11)The java.lang.Number and its subclasses are introduced in Chapter 11. Analyze the following code.
Number numberRef = new Integer(0);
Double doubleRef = (Double)numberRef;
A)The program runs fine, since Integer is a subclass of Double.
B)There is no such class named Integer. You should use the class Int.
C)The compiler detects that numberRef is not an instance of Double.
D)You can convert an int to double, so you can cast an Integer instance to a Double instance.
E)A runtime class casting exception occurs, since numberRef is not an instance of Double.
Number是Double的父类,父类对象不是子类对象的实例
12)Which of the following statements regarding abstract methods are true? (Choose all that apply.)
A)An abstract method cannot be contained in a nonabstract class.
B)Abstract classes have constructors.
C)It is possible to declare an abstract class that contains no abstract methods.
D)A class that contains abstract methods must be abstract.
E)A data field can be declared abstract.
F)The constructors in an abstract class should be protected.
G)You may declare a final abstract class.
抽象方法不能包含在非抽象类中。
抽象类有构造函数。
可以声明一个不包含抽象方法的抽象类。
包含抽象方法的类必须是抽象的。
抽象类的数据字段不能声明为抽象的!!
抽象类的构造方法应该定义为protected,因为它只被子类使用。
13)Which of the following statements are true? (Choose all that apply.) 19) ______
A)If you compile an interface without errors, but with warnings, a .class file is created for the interface.
B)If you compile a class without errors but with warnings, a .class file is created.
C)If you compile an interface without errors, a .class file is created for the interface.
D)If you compile a class with errors, a .class file is created for the class.
没有错误,即使有警告也能编译通过
14)The java.lang.Cloneable interface is introduced in Chapter 11. Analyze the following code.
public class Test {
public static void main(String[ ] args) {
java.util.Date x = new java.util.Date();
java.util.Date y = x.clone();
System.out.println(x = y);
}
}
A)x = y in System.out.println(x = y) causes a runtime error because you cannot have an assignment statement inside a statement.
B)The program has a compile error because the return type of the clone() method is java.lang.Object.
C)x = y in System.out.println(x = y) causes a compile error because you cannot have an assignment statement inside a statement.
D)A java.util.Date object is not cloneable.
15)Analyze the following code.
1. public class Test {
2. public static void main(String[ ] args) {
3. Fruit[ ] fruits = {new Fruit(2), new Fruit(3), new Fruit(1)};
4. java.util.Arrays.sort(fruits);
5. }
6. }
class Fruit {
private double weight;
public Fruit(double weight) {
this.weight = weight;
}
}
A)The program has a compile error on Line 4 because the Fruit class does not implement the java.lang.Comparable interface and the Fruit objects are not comparable.
B)The program has a runtime error on Line 3 because the Fruit class does not have a default constructor.
C)The program has a compile error because the Fruit class does not have a default constructor.
D)The program has a runtime error on Line 4 because the Fruit class does not implement the java.lang.Comparable interface and the Fruit objects are not comparable.
想要使用sort方法,必须先要实现Comparable接口
16)Assume Calendar calendar = new GregorianCalendar(). ________ returns the week of the year.
A)calendar.get(Calendar.WEEK_OF_YEAR)
B)calendar.get(Calendar.WEEK_OF_MONTH)
C)calendar.get(Calendar.MONTH)
D)calendar.get(Calendar.MONTH_OF_YEAR)
17)Suppose A is an abstract class, B is a concrete subclass of A, and both A and B have a default constructor. Which of the following is correct? (Choose all that apply.) 27) ______
A)B b = new B(); B) A a = new B(); C)B b = new A(); D) A a = new A();
18)Assume Calendar calendar = new GregorianCalendar(). ________ returns the number of days in a month. 29) ______
A)calendar.get(Calendar.WEEK_OF_MONTH)
B)calendar.getActualMaximum(Calendar.DAY_OF_MONTH)
C)calendar.get(Calendar.MONTH)
D)calendar.get(Calendar.WEEK_OF_YEAR)
E)calendar.get(Calendar.MONTH_OF_YEAR)
19)The printout from the following code is ________.
java.util.ArrayList list = new java.util.ArrayList();
list.add("New York");
java.util.ArrayList list1 = (java.util.ArrayList)(list.clone());
list.add("Atlanta");
list1.add("Dallas");
System.out.println(list1);
A)[New York, Atlanta, Dallas] B) [New York] C)[New York, Atlanta] D) [New York, Dallas]
注意这里使用的是克隆,使用克隆后list1和list是内容相同的两个不同对象了
20)What is the output of the following code?
public class Test {
public static void main(String[ ] args) {
java.math.BigInteger x = new java.math.BigInteger("3");
java.math.BigInteger y = new java.math.BigInteger("7");
x.add(y);
System.out.println(x);
}
}
A)10 B) 11 C) 3 D) 4
注意:这里x.add(y)之后并没有将结果赋值给x
21)________ is a reference type. (Choose all that apply.) 32) ______
A)A class type B) An array type C)A primitive type D) An interface type
33)Assume Calendar calendar = new GregorianCalendar(). ________ returns the month of the year. 33) ______
A)calendar.get(Calendar.WEEK_OF_YEAR)
B)calendar.get(Calendar.WEEK_OF_MONTH)
C)calendar.get(Calendar.MONTH_OF_YEAR)
D)calendar.get(Calendar.MONTH)
没有MONTH_OF_YEAR这种说法,每年一定是12个月!!!
22)The java.lang.Comparable interface is introduced in Chapter 11. Analyze the following code: (Choose all that apply.)
public class Test1 {
public Object max(Object o1, Object o2) {
if ((Comparable)o1.compareTo(o2) >= 0) {
return o1;
}
else {
return o2;
}
}
}
A)The program would compile if ((Comparable)o1.compareTo(o2) >= 0) is replaced by (((Comparable)o1).compareTo(o2) >= 0).
B)The program has a compile error because Test1 does not have a main method.
C)The program has a compile error because o1 is an Object instance and it does not have the compareTo method.
D)The program has a compile error because you cannot cast an Object instance o1 into Comparable.
23)Show the output of running the class Test in the following code lines:
interface A {
}
class C {
}
class B extends D implements A {
}
public class Test extends Thread {
public static void main(String[ ] args) {
B b = new B();
if (b instanceof A)
System.out.println("b is an instance of A");
if (b instanceof C)
System.out.println("b is an instance of C");
}
}
class D extends C {
}
A)b is an instance of C.
B)b is an instance of A followed by b is an instance of C.
C)b is an instance of A.
D)Nothing
24) The java.util.Calendar and java.util.GregorianCalendar classes are introduced in Chapter 11. Analyze the following code.
1. import java.util.*;
2. public class Test {
3. public static void main(String[ ] args) {
4. Calendar[ ] calendars = new Calendar[10];
5. calendars[0] = new Calendar();
6. calendars[1] = new GregorianCalendar();
7. }
8. }
A)The program has a compile error on Line 5 because java.util.Calendar is an abstract class.
B)The program has no compile errors.
C)The program has a compile error on Line 4 because java.util.Calendar is an abstract class.
D)The program has a compile error on Line 6 because Calendar[1] is not of a GregorianCalendar type.
抽象类对象Calendar不能使用new来实例化
25)The GeometricObject and Circle classes are defined in Chapter 11. Analyze the following code. (Choose all that apply.)
public class Test {
public static void main(String[ ] args) {
GeometricObject x = new Circle(3);
GeometricObject y = (Circle)(x.clone());
System.out.println(x);
System.out.println(y);
}
}
A)After you override the clone() method and make it public in the Circle class, the problem can compile and run just fine, but y is null if Circle does not implement the Cloneable interface.
B)If GeometricObject implements Cloneable and Circle overrides the clone() method, the clone() method will work fine to clone Circle objects.
C)To enable a Circle object to be cloned, the Circle class has to override the clone() method and implement the java.lang.Cloneable interface.
D)The program has a compile error because the clone() method is protected in the Object class.
26)Analyze the following code.
public class Test {
public static void main(String[ ] args) {
Number x = new Integer(3);
System.out.println(x.intValue());
System.out.println(x.compareTo(new Integer(4)));
}
}
A)The program has a compile error because an Integer instance cannot be assigned to a Number variable.
B)The program has a compile error because x does not have the compareTo method.
C)The program compiles and runs fine.
D)The program has a compile error because intValue is an abstract method in Number.
x为Number的实例对象,没有compareTo 的实例方法
27)Which of the following statements regarding abstract methods are true? (Choose all that apply.)
A)A subclass of a non-abstract superclass can be abstract.
B)An abstract class can be extended.
C)A subclass can override a concrete method in a superclass to declare it abstract.
D)An abstract class can be used as a data type.
E)An abstract class can have instances created using the constructor of the abstract class.
非抽象父类的子类可以是抽象的,比如一个抽象类其父类是Object,并不是一个抽象类
子类可以覆盖父类中的具体方法来声明它是抽象的。
抽象类不能被实例化!
Java题库——Chapter13抽象类和接口的更多相关文章
- 牛客网Java刷题知识点之抽象类与接口
不多说,直接上干货! 接口和内部类为我们提供了一种将接口与实现分离的更加结构化的方法. 抽象类与接口是Java语言中对抽象概念进行定义的两种机制,正是由于它们的存在才赋予java强大的面向对象的能力. ...
- java提高篇(五)-----抽象类与接口
接口和内部类为我们提供了一种将接口与实现分离的更加结构化的方法. 抽象类与接口是java语言中对抽象概念进行定义的两种机制,正是由于他们的存在才赋予java强大的面向对象的能力.他们两者之间对抽象概念 ...
- 十、Java基础---------面向对象之抽象类与接口
抽象类(abstract) 当编写一个类时,时常会为该类定义一些方法,这些方法的使用用以描述该类的行为方式,那么这些方法都有具体的方法体.但是在某些情况下,某个父类只是知道子类应该包含怎样的方 ...
- Java中多态、抽象类和接口
1:final关键字(掌握) (1)是最终的意思,可以修饰类,方法,变量. (2)特点: A:它修饰的类,不能被继承. B:它修饰的方法,不能被重写. C:它修饰的变量,是一个常量. (3)面试相关: ...
- Java学习日记-7 抽象类和接口
一.抽象类 abstract修饰:类和类中的方法 抽象方法:abstract type name(parameter-list);(abstract不能修饰static方法和构造函数) 引用:抽象类有 ...
- (转)java提高篇(五)-----抽象类与接口
接口和内部类为我们提供了一种将接口与实现分离的更加结构化的方法. 抽象类与接口是java语言中对抽象概念进行定义的两种机制,正是由于他们的存在才赋予java强大的面向对象的能力.他们两者之间对抽象概念 ...
- JAVA 继承基本类、抽象类、接口
Java是一个面向对象的语言,java面向对象一般有三大特征:封装.继承.多态. 封装:就是把一些属性和方法封装到一个类里. 继承:就如子类继承父类的一些属性和方法. 多态:就如一个父类有多个不同特色 ...
- Java基础系列--06_抽象类与接口概述
抽象类 (1)如果多个类中存在相同的方法声明,而方法体不一样,我们就可以只提取方法声明. 如果一个方法只有方法声明,没有方法体,那么这个方法必须用抽象修饰. 而一个类中如果有抽象方法,这个类必须定义为 ...
- Java提高篇之抽象类与接口
接口和内部类为我们提供了一种将接口与实现分离的更加结构化的方法. 抽象类与接口是java语言中对抽象概念进行定义的两种机制,正是由于他们的存在才赋予java强大的面向对象的能力.他们两者之间对抽象概念 ...
随机推荐
- CCF-CSP题解 201512-4 送货
求字典序最小欧拉路. 似乎不能用\(Fluery\)算法(\(O(E^2)\)).\(Fluery\)算法的思路是:延申的边尽可能不是除去已走过边的图的桥(割).每走一步都要判断是否是割,应当会超时. ...
- Python的小整数池
此处经常会作为面试题!!! 小整数池目的:节省内存,提高执行效率 需要注意的是:Python实现int的时候有个小整数池.为了避免因创建相同的值而重复申请内存空间所带来的效率问题, Python解释器 ...
- So Easy - 在Linux服务器上部署 .NET Core App
.NET Core 是微软提供的免费.跨平台和开源的开发框架,可以构建桌面应用程序.移动端应用程序.网络应用程序.物联网应用程序和游戏应用程序等.如果你是 Windows 平台下的 dotnet 开发 ...
- git 知识,适合新手 滤清思路
1,密钥 (公钥和私钥) @ 公钥放在服务器上(说白了这里的服务器就是远程仓库, 就是谁建立的远程仓库这个公钥就放在他的ssh设置那) @ 私钥 放在本地就行,不用动,就是你生产密钥的.ssh 文件里 ...
- 2016/09/27 Hadoop Yarn
1.1 YARN基本架构 YARN是Hadoop2.0中的资源管理系统,它的基本设计思想是将MRv1中的JobTracker拆分成了两个独立的服务:一个全局的资源管理器ResourceMana ...
- GHOST CMS - Package.json
Package.json The package.json file is a set of meta data about a theme. package.json 文件是一组关于主题的元数据. ...
- Elasticsearch 监控指标解析
1.集群监控 集群监控主要包括两个方面的内容,分别是集群健康情况和集群的运行状态. 集群健康状态可以通过以下api获取: http://ip:9200/_cluster/health?pretty 关 ...
- 【转载】Opening Robot Framework log failed
问题: 两种方法可以解决: 1.临时解决方案 jenkins系统管理—>运行命令行,在文本里输入 System.setProperty("hudson.model.DirectoryB ...
- C# ling to sql 左表连接
var begin_daily = from a in _postgreDbContext.tab1 join b in _postgreDbContext.tab2 on a.id equals b ...
- Mapbox轨迹回放
轨迹回放是webgis中的常见功能,是一种被客户喜闻乐见的GIS动画. 动画是一种短时间内不停重绘达到不断运动的效果.本文中轨迹回放就是事先计算好所需要的点,后面再进行播放. ...