)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. hdu 2586 How far away?(LCA模板题+离线tarjan算法)

    How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  2. Spring Boot 外部化配置(二) - @ConfigurationProperties 、@EnableConfigurationProperties

    目录 3.外部化配置的核心 3.2 @ConfigurationProperties 3.2.1 注册 Properties 配置类 3.2.2 绑定配置属性 3.1.3 ConfigurationP ...

  3. 《Java知识应用》Java Json说明和使用(fastjson)

    工具包下载:链接: https://pan.baidu.com/s/1dn5uNwiJ1ICkbPknlMmkHQ 提取码: ayzn 复制这段内容后打开百度网盘手机App,操作更方便哦 1.JSON ...

  4. 《Dotnet9》系列-开源C# WPF控件库强力推荐

    时间如流水,只能流去不流回! 点赞再看,养成习惯,这是您给我创作的动力! 本文 Dotnet9 https://dotnet9.com 已收录,站长乐于分享dotnet相关技术,比如Winform.W ...

  5. CSS 选择器、字体/文本、背景

    CSS的基本使用 直接写在标签内 <p style="color: red; font-size: 40px;">段落</p> 写在 style 标签内 & ...

  6. FCC---CSS Flexbox: Apply the flex-direction Property to Create a Column in the Tweet Embed

    The tweet embed header and footer used the flex-direction property earlier with a row value. Similar ...

  7. php 弱类型总结

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

  8. docker学习笔记---基本命令

    [root@docker ~]# docker Usage: docker [OPTIONS] COMMAND A self-sufficient runtime for containers Opt ...

  9. js 从两道面试题加深理解闭包与箭头函数中的this

     壹 ❀ 引 在本文之前我已经花了两个篇幅专门介绍了JavaScript中的闭包与this,正好今早地铁上看到了两道面试题,试着做了下发现挺有意思,所以想单独写一篇文章来记录解析过程.若你对于闭包与t ...

  10. HTML DOM的创建,删除及替换

    创建HTML元素 document.appendChild() 将新元素作为父元素的最后一个子元素进行添加 如需向HTML DOM添加新元素,首先必须创建该元素,然后把它追加到已有的元素上 var n ...