)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抽象类和接口的更多相关文章

  1. 牛客网Java刷题知识点之抽象类与接口

    不多说,直接上干货! 接口和内部类为我们提供了一种将接口与实现分离的更加结构化的方法. 抽象类与接口是Java语言中对抽象概念进行定义的两种机制,正是由于它们的存在才赋予java强大的面向对象的能力. ...

  2. java提高篇(五)-----抽象类与接口

    接口和内部类为我们提供了一种将接口与实现分离的更加结构化的方法. 抽象类与接口是java语言中对抽象概念进行定义的两种机制,正是由于他们的存在才赋予java强大的面向对象的能力.他们两者之间对抽象概念 ...

  3. 十、Java基础---------面向对象之抽象类与接口

    抽象类(abstract)     当编写一个类时,时常会为该类定义一些方法,这些方法的使用用以描述该类的行为方式,那么这些方法都有具体的方法体.但是在某些情况下,某个父类只是知道子类应该包含怎样的方 ...

  4. Java中多态、抽象类和接口

    1:final关键字(掌握) (1)是最终的意思,可以修饰类,方法,变量. (2)特点: A:它修饰的类,不能被继承. B:它修饰的方法,不能被重写. C:它修饰的变量,是一个常量. (3)面试相关: ...

  5. Java学习日记-7 抽象类和接口

    一.抽象类 abstract修饰:类和类中的方法 抽象方法:abstract type name(parameter-list);(abstract不能修饰static方法和构造函数) 引用:抽象类有 ...

  6. (转)java提高篇(五)-----抽象类与接口

    接口和内部类为我们提供了一种将接口与实现分离的更加结构化的方法. 抽象类与接口是java语言中对抽象概念进行定义的两种机制,正是由于他们的存在才赋予java强大的面向对象的能力.他们两者之间对抽象概念 ...

  7. JAVA 继承基本类、抽象类、接口

    Java是一个面向对象的语言,java面向对象一般有三大特征:封装.继承.多态. 封装:就是把一些属性和方法封装到一个类里. 继承:就如子类继承父类的一些属性和方法. 多态:就如一个父类有多个不同特色 ...

  8. Java基础系列--06_抽象类与接口概述

    抽象类 (1)如果多个类中存在相同的方法声明,而方法体不一样,我们就可以只提取方法声明. 如果一个方法只有方法声明,没有方法体,那么这个方法必须用抽象修饰. 而一个类中如果有抽象方法,这个类必须定义为 ...

  9. Java提高篇之抽象类与接口

    接口和内部类为我们提供了一种将接口与实现分离的更加结构化的方法. 抽象类与接口是java语言中对抽象概念进行定义的两种机制,正是由于他们的存在才赋予java强大的面向对象的能力.他们两者之间对抽象概念 ...

随机推荐

  1. CCF-CSP题解 201403-4 无线网络

    新建不超过\(k\)个无线路由器,求使路由器1.2连通最少的中间路由器. 首先常规建图,将相距不超过\(r\)的路由器(包括新建的)相连. 想到了分层\(dijkstra\).类似的,作\(bfs\) ...

  2. 五分钟学会conda常用命令

    文章目录 conda常用命令 1. 获取版本号 2. 获取帮助 3. 环境管理 4. 分享环境 5. 包管理 conda常用命令 1. 获取版本号 conda --version 或 conda -V ...

  3. JS---案例:完整的轮播图---重点!

    案例:完整的轮播图 思路: 分5部分做 1. 获取所有要用的元素 2. 做小按钮,点击移动图标部分 3. 做右边焦点按钮,点击移动图片,小按钮颜色一起跟着变 (克隆了第一图到第六图,用索引liObj. ...

  4. php 弱类型总结

    0x01 前言 最近CTF比赛,不止一次的出了php弱类型的题目,借此想总结一下关于php弱类型以及绕过方式 0x02 知识介绍 php中有两种比较的符号 == 与 === <?php $a = ...

  5. 4.Android-adt安卓打包过程、adb指令学习

    本章学习adt安卓打包过程.adb指令学习.并通过adb将打包的APK发给设备 1.打包 在eclipse中已经帮我们实现打包了. 具体打包流程如下: 最终一个APK包含了如下: classes.de ...

  6. Ansible-playbook之循环判断

    1.循环 (loop) # 使用循环创建硬连接:x连接到y:z连接到k: - hosts: web - name: Create two hard links file: src: "{{ ...

  7. C语言笔记 04_运算符

    运算符 运算符是一种告诉编译器执行特定的数学或逻辑操作的符号.C 语言内置了丰富的运算符,并提供了以下类型的运算符: 算术运算符 关系运算符 逻辑运算符 位运算符 赋值运算符 杂项运算符 算术运算符 ...

  8. Ligg.EasyWinApp-102-Ligg.EasyWinForm:Function--ControlBox、Tray、Resize、Menu

    首先请在VS里打开下面的文件,我们将对源码分段进行说明: Function(功能):一个应用的功能界面,一个应用对应多个Function(功能):如某应用可分为管理员界面.用户界面. 首先我们来看一下 ...

  9. Vue 中的keep-alive 什么用处?

    keep-alive keep-alive是Vue提供的一个抽象组件,用来对组件进行缓存,从而节省性能,由于是一个抽象组件,所以在v页面渲染完毕后不会被渲染成一个DOM元素 <keep-aliv ...

  10. 利用 OpenCC 工具进行文字的简繁转换

    前言 近日在公司遇到一个需求,因为准备要推出海外版产品,所以需要将所有的简体文字转换为繁体文字.一开始是改了表面的文字,但是后面发现很多提示语也需要去改,所以找了一个工具去对所有 .m 文件进行批量文 ...